ReplicateHome.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ...requests.aiohttp import get_connector
  8. from ...requests.raise_for_status import raise_for_status
  9. from ..helper import format_prompt
  10. from ...image import ImageResponse
  11. class ReplicateHome(AsyncGeneratorProvider, ProviderModelMixin):
  12. url = "https://replicate.com"
  13. api_endpoint = "https://homepage.replicate.com/api/prediction"
  14. working = False
  15. supports_stream = True
  16. default_model = 'google-deepmind/gemma-2b-it'
  17. default_image_model = 'stability-ai/stable-diffusion-3'
  18. image_models = [
  19. 'stability-ai/stable-diffusion-3',
  20. 'bytedance/sdxl-lightning-4step',
  21. 'playgroundai/playground-v2.5-1024px-aesthetic',
  22. ]
  23. text_models = [
  24. 'google-deepmind/gemma-2b-it',
  25. ]
  26. models = text_models + image_models
  27. model_aliases = {
  28. # image_models
  29. "sd-3": "stability-ai/stable-diffusion-3",
  30. "sdxl": "bytedance/sdxl-lightning-4step",
  31. "playground-v2.5": "playgroundai/playground-v2.5-1024px-aesthetic",
  32. # text_models
  33. "gemma-2b": "google-deepmind/gemma-2b-it",
  34. }
  35. model_versions = {
  36. # image_models
  37. 'stability-ai/stable-diffusion-3': "527d2a6296facb8e47ba1eaf17f142c240c19a30894f437feee9b91cc29d8e4f",
  38. 'bytedance/sdxl-lightning-4step': "5f24084160c9089501c1b3545d9be3c27883ae2239b6f412990e82d4a6210f8f",
  39. 'playgroundai/playground-v2.5-1024px-aesthetic': "a45f82a1382bed5c7aeb861dac7c7d191b0fdf74d8d57c4a0e6ed7d4d0bf7d24",
  40. # text_models
  41. "google-deepmind/gemma-2b-it": "dff94eaf770e1fc211e425a50b51baa8e4cac6c39ef074681f9e39d778773626",
  42. }
  43. @classmethod
  44. async def create_async_generator(
  45. cls,
  46. model: str,
  47. messages: Messages,
  48. prompt: str = None,
  49. proxy: str = None,
  50. **kwargs
  51. ) -> AsyncResult:
  52. model = cls.get_model(model)
  53. headers = {
  54. "accept": "*/*",
  55. "accept-language": "en-US,en;q=0.9",
  56. "content-type": "application/json",
  57. "origin": "https://replicate.com",
  58. "referer": "https://replicate.com/",
  59. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
  60. }
  61. async with ClientSession(headers=headers, connector=get_connector(proxy=proxy)) as session:
  62. if prompt is None:
  63. if model in cls.image_models:
  64. prompt = messages[-1]['content']
  65. else:
  66. prompt = format_prompt(messages)
  67. data = {
  68. "model": model,
  69. "version": cls.model_versions[model],
  70. "input": {"prompt": prompt},
  71. }
  72. async with session.post(cls.api_endpoint, json=data) as response:
  73. await raise_for_status(response)
  74. result = await response.json()
  75. prediction_id = result['id']
  76. poll_url = f"https://homepage.replicate.com/api/poll?id={prediction_id}"
  77. max_attempts = 30
  78. delay = 5
  79. for _ in range(max_attempts):
  80. async with session.get(poll_url) as response:
  81. await raise_for_status(response)
  82. try:
  83. result = await response.json()
  84. except ContentTypeError:
  85. text = await response.text()
  86. try:
  87. result = json.loads(text)
  88. except json.JSONDecodeError:
  89. raise ValueError(f"Unexpected response format: {text}")
  90. if result['status'] == 'succeeded':
  91. if model in cls.image_models:
  92. image_url = result['output'][0]
  93. yield ImageResponse(image_url, prompt)
  94. return
  95. else:
  96. for chunk in result['output']:
  97. yield chunk
  98. break
  99. elif result['status'] == 'failed':
  100. raise Exception(f"Prediction failed: {result.get('error')}")
  101. await asyncio.sleep(delay)
  102. if result['status'] != 'succeeded':
  103. raise Exception("Prediction timed out")