MicrosoftDesigner.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import annotations
  2. import uuid
  3. import aiohttp
  4. import random
  5. import asyncio
  6. import json
  7. from ...image import ImageResponse
  8. from ...errors import MissingRequirementsError, NoValidHarFileError
  9. from ...typing import AsyncResult, Messages
  10. from ...requests.raise_for_status import raise_for_status
  11. from ...requests.aiohttp import get_connector
  12. from ...requests import get_nodriver
  13. from ..Copilot import get_headers, get_har_files
  14. from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
  15. from ..helper import get_random_hex
  16. from ... import debug
  17. class MicrosoftDesigner(AsyncGeneratorProvider, ProviderModelMixin):
  18. label = "Microsoft Designer"
  19. url = "https://designer.microsoft.com"
  20. working = True
  21. needs_auth = True
  22. default_image_model = "dall-e-3"
  23. image_models = [default_image_model, "1024x1024", "1024x1792", "1792x1024"]
  24. models = image_models
  25. @classmethod
  26. async def create_async_generator(
  27. cls,
  28. model: str,
  29. messages: Messages,
  30. prompt: str = None,
  31. proxy: str = None,
  32. **kwargs
  33. ) -> AsyncResult:
  34. image_size = "1024x1024"
  35. if model != cls.default_image_model and model in cls.image_models:
  36. image_size = model
  37. yield await cls.generate(messages[-1]["content"] if prompt is None else prompt, image_size, proxy)
  38. @classmethod
  39. async def generate(cls, prompt: str, image_size: str, proxy: str = None) -> ImageResponse:
  40. try:
  41. access_token, user_agent = readHAR("https://designerapp.officeapps.live.com")
  42. except NoValidHarFileError as h:
  43. debug.log(f"{cls.__name__}: {h}")
  44. try:
  45. access_token, user_agent = await get_access_token_and_user_agent(cls.url, proxy)
  46. except MissingRequirementsError:
  47. raise h
  48. images = await create_images(prompt, access_token, user_agent, image_size, proxy)
  49. return ImageResponse(images, prompt)
  50. async def create_images(prompt: str, access_token: str, user_agent: str, image_size: str, proxy: str = None, seed: int = None):
  51. url = 'https://designerapp.officeapps.live.com/designerapp/DallE.ashx?action=GetDallEImagesCogSci'
  52. if seed is None:
  53. seed = random.randint(0, 10000)
  54. headers = {
  55. "User-Agent": user_agent,
  56. "Accept": "application/json, text/plain, */*",
  57. "Accept-Language": "en-US",
  58. 'Authorization': f'Bearer {access_token}',
  59. "AudienceGroup": "Production",
  60. "Caller": "DesignerApp",
  61. "ClientId": "b5c2664a-7e9b-4a7a-8c9a-cd2c52dcf621",
  62. "SessionId": str(uuid.uuid4()),
  63. "UserId": get_random_hex(16),
  64. "ContainerId": "1e2843a7-2a98-4a6c-93f2-42002de5c478",
  65. "FileToken": "9f1a4cb7-37e7-4c90-b44d-cb61cfda4bb8",
  66. "x-upload-to-storage-das": "1",
  67. "traceparent": "",
  68. "X-DC-Hint": "FranceCentral",
  69. "Platform": "Web",
  70. "HostApp": "DesignerApp",
  71. "ReleaseChannel": "",
  72. "IsSignedInUser": "true",
  73. "Locale": "de-DE",
  74. "UserType": "MSA",
  75. "x-req-start": "2615401",
  76. "ClientBuild": "1.0.20241120.9",
  77. "ClientName": "DesignerApp",
  78. "Sec-Fetch-Dest": "empty",
  79. "Sec-Fetch-Mode": "cors",
  80. "Sec-Fetch-Site": "cross-site",
  81. "Pragma": "no-cache",
  82. "Cache-Control": "no-cache",
  83. "Referer": "https://designer.microsoft.com/"
  84. }
  85. form_data = aiohttp.FormData()
  86. form_data.add_field('dalle-caption', prompt)
  87. form_data.add_field('dalle-scenario-name', 'TextToImage')
  88. form_data.add_field('dalle-batch-size', '4')
  89. form_data.add_field('dalle-image-response-format', 'UrlWithBase64Thumbnail')
  90. form_data.add_field('dalle-seed', seed)
  91. form_data.add_field('ClientFlights', 'EnableBICForDALLEFlight')
  92. form_data.add_field('dalle-hear-back-in-ms', 1000)
  93. form_data.add_field('dalle-include-b64-thumbnails', 'true')
  94. form_data.add_field('dalle-aspect-ratio-scaling-factor-b64-thumbnails', 0.3)
  95. form_data.add_field('dalle-image-size', image_size)
  96. async with aiohttp.ClientSession(connector=get_connector(proxy=proxy)) as session:
  97. async with session.post(url, headers=headers, data=form_data) as response:
  98. await raise_for_status(response)
  99. response_data = await response.json()
  100. form_data.add_field('dalle-boost-count', response_data.get('dalle-boost-count', 0))
  101. polling_meta_data = response_data.get('polling_response', {}).get('polling_meta_data', {})
  102. form_data.add_field('dalle-poll-url', polling_meta_data.get('poll_url', ''))
  103. while True:
  104. await asyncio.sleep(polling_meta_data.get('poll_interval', 1000) / 1000)
  105. async with session.post(url, headers=headers, data=form_data) as response:
  106. await raise_for_status(response)
  107. response_data = await response.json()
  108. images = [image["ImageUrl"] for image in response_data.get('image_urls_thumbnail', [])]
  109. if images:
  110. return images
  111. def readHAR(url: str) -> tuple[str, str]:
  112. api_key = None
  113. user_agent = None
  114. for path in get_har_files():
  115. with open(path, 'rb') as file:
  116. try:
  117. harFile = json.loads(file.read())
  118. except json.JSONDecodeError:
  119. # Error: not a HAR file!
  120. continue
  121. for v in harFile['log']['entries']:
  122. if v['request']['url'].startswith(url):
  123. v_headers = get_headers(v)
  124. if "authorization" in v_headers:
  125. api_key = v_headers["authorization"].split(maxsplit=1).pop()
  126. if "user-agent" in v_headers:
  127. user_agent = v_headers["user-agent"]
  128. if api_key is None:
  129. raise NoValidHarFileError("No access token found in .har files")
  130. return api_key, user_agent
  131. async def get_access_token_and_user_agent(url: str, proxy: str = None):
  132. browser = await get_nodriver(proxy=proxy, user_data_dir="designer")
  133. page = await browser.get(url)
  134. user_agent = await page.evaluate("navigator.userAgent")
  135. access_token = None
  136. while access_token is None:
  137. access_token = await page.evaluate("""
  138. (() => {
  139. for (var i = 0; i < localStorage.length; i++) {
  140. try {
  141. item = JSON.parse(localStorage.getItem(localStorage.key(i)));
  142. if (item.credentialType == "AccessToken"
  143. && item.expiresOn > Math.floor(Date.now() / 1000)
  144. && item.target.includes("designerappservice")) {
  145. return item.secret;
  146. }
  147. } catch(e) {}
  148. }
  149. })()
  150. """)
  151. if access_token is None:
  152. await asyncio.sleep(1)
  153. await page.close()
  154. return access_token, user_agent