ReplicateHome.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from __future__ import annotations
  2. import json
  3. import asyncio
  4. from aiohttp import ClientSession, ContentTypeError
  5. from ..typing import AsyncResult, Messages
  6. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  7. from .helper import format_prompt
  8. from ..image import ImageResponse
  9. class ReplicateHome(AsyncGeneratorProvider, ProviderModelMixin):
  10. url = "https://replicate.com"
  11. api_endpoint = "https://homepage.replicate.com/api/prediction"
  12. working = True
  13. supports_stream = True
  14. supports_system_message = True
  15. supports_message_history = True
  16. default_model = 'meta/meta-llama-3-70b-instruct'
  17. text_models = [
  18. 'meta/meta-llama-3-70b-instruct',
  19. 'mistralai/mixtral-8x7b-instruct-v0.1',
  20. 'google-deepmind/gemma-2b-it',
  21. 'yorickvp/llava-13b',
  22. ]
  23. image_models = [
  24. 'black-forest-labs/flux-schnell',
  25. 'stability-ai/stable-diffusion-3',
  26. 'bytedance/sdxl-lightning-4step',
  27. 'playgroundai/playground-v2.5-1024px-aesthetic',
  28. ]
  29. models = text_models + image_models
  30. model_aliases = {
  31. "flux-schnell": "black-forest-labs/flux-schnell",
  32. "sd-3": "stability-ai/stable-diffusion-3",
  33. "sdxl": "bytedance/sdxl-lightning-4step",
  34. "playground-v2.5": "playgroundai/playground-v2.5-1024px-aesthetic",
  35. "llama-3-70b": "meta/meta-llama-3-70b-instruct",
  36. "mixtral-8x7b": "mistralai/mixtral-8x7b-instruct-v0.1",
  37. "gemma-2b": "google-deepmind/gemma-2b-it",
  38. "llava-13b": "yorickvp/llava-13b",
  39. }
  40. model_versions = {
  41. "meta/meta-llama-3-70b-instruct": "fbfb20b472b2f3bdd101412a9f70a0ed4fc0ced78a77ff00970ee7a2383c575d",
  42. "mistralai/mixtral-8x7b-instruct-v0.1": "5d78bcd7a992c4b793465bcdcf551dc2ab9668d12bb7aa714557a21c1e77041c",
  43. "google-deepmind/gemma-2b-it": "dff94eaf770e1fc211e425a50b51baa8e4cac6c39ef074681f9e39d778773626",
  44. "yorickvp/llava-13b": "80537f9eead1a5bfa72d5ac6ea6414379be41d4d4f6679fd776e9535d1eb58bb",
  45. 'black-forest-labs/flux-schnell': "f2ab8a5bfe79f02f0789a146cf5e73d2a4ff2684a98c2b303d1e1ff3814271db",
  46. 'stability-ai/stable-diffusion-3': "527d2a6296facb8e47ba1eaf17f142c240c19a30894f437feee9b91cc29d8e4f",
  47. 'bytedance/sdxl-lightning-4step': "5f24084160c9089501c1b3545d9be3c27883ae2239b6f412990e82d4a6210f8f",
  48. 'playgroundai/playground-v2.5-1024px-aesthetic': "a45f82a1382bed5c7aeb861dac7c7d191b0fdf74d8d57c4a0e6ed7d4d0bf7d24",
  49. }
  50. @classmethod
  51. def get_model(cls, model: str) -> str:
  52. if model in cls.models:
  53. return model
  54. elif model in cls.model_aliases:
  55. return cls.model_aliases[model]
  56. else:
  57. return cls.default_model
  58. @classmethod
  59. async def create_async_generator(
  60. cls,
  61. model: str,
  62. messages: Messages,
  63. proxy: str = None,
  64. **kwargs
  65. ) -> AsyncResult:
  66. model = cls.get_model(model)
  67. headers = {
  68. "accept": "*/*",
  69. "accept-language": "en-US,en;q=0.9",
  70. "cache-control": "no-cache",
  71. "content-type": "application/json",
  72. "origin": "https://replicate.com",
  73. "pragma": "no-cache",
  74. "priority": "u=1, i",
  75. "referer": "https://replicate.com/",
  76. "sec-ch-ua": '"Not;A=Brand";v="24", "Chromium";v="128"',
  77. "sec-ch-ua-mobile": "?0",
  78. "sec-ch-ua-platform": '"Linux"',
  79. "sec-fetch-dest": "empty",
  80. "sec-fetch-mode": "cors",
  81. "sec-fetch-site": "same-site",
  82. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
  83. }
  84. async with ClientSession(headers=headers) as session:
  85. if model in cls.image_models:
  86. prompt = messages[-1]['content'] if messages else ""
  87. else:
  88. prompt = format_prompt(messages)
  89. data = {
  90. "model": model,
  91. "version": cls.model_versions[model],
  92. "input": {"prompt": prompt},
  93. }
  94. async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
  95. response.raise_for_status()
  96. result = await response.json()
  97. prediction_id = result['id']
  98. poll_url = f"https://homepage.replicate.com/api/poll?id={prediction_id}"
  99. max_attempts = 30
  100. delay = 5
  101. for _ in range(max_attempts):
  102. async with session.get(poll_url, proxy=proxy) as response:
  103. response.raise_for_status()
  104. try:
  105. result = await response.json()
  106. except ContentTypeError:
  107. text = await response.text()
  108. try:
  109. result = json.loads(text)
  110. except json.JSONDecodeError:
  111. raise ValueError(f"Unexpected response format: {text}")
  112. if result['status'] == 'succeeded':
  113. if model in cls.image_models:
  114. image_url = result['output'][0]
  115. yield ImageResponse(image_url, "Generated image")
  116. return
  117. else:
  118. for chunk in result['output']:
  119. yield chunk
  120. break
  121. elif result['status'] == 'failed':
  122. raise Exception(f"Prediction failed: {result.get('error')}")
  123. await asyncio.sleep(delay)
  124. if result['status'] != 'succeeded':
  125. raise Exception("Prediction timed out")