XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Restored provider (g4f/Provider/nexra/NexraDallE2.py)

ada5e8c2
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

1 个文件 +35 -47
Modified g4f/Provider/nexra/NexraDallE2.py +35 -47
@@ -1,74 +1,62 @@
1 1 from __future__ import annotations
2 2
3 from aiohttp import ClientSession
4 3 import json
5
6 from ...typing import AsyncResult, Messages
7 from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
4 import requests
5 from ...typing import CreateResult, Messages
6 from ..base_provider import ProviderModelMixin, AbstractProvider
8 7 from ...image import ImageResponse
9 8
10
11 class NexraDallE2(AsyncGeneratorProvider, ProviderModelMixin):
9 class NexraDallE2(AbstractProvider, ProviderModelMixin):
12 10 label = "Nexra DALL-E 2"
13 11 url = "https://nexra.aryahcr.cc/documentation/dall-e/en"
14 12 api_endpoint = "https://nexra.aryahcr.cc/api/image/complements"
15 working = False
16
17 default_model = 'dalle2'
13 working = True
14
15 default_model = "dalle2"
18 16 models = [default_model]
19 model_aliases = {
20 "dalle-2": "dalle2",
21 }
22 17
23 18 @classmethod
24 19 def get_model(cls, model: str) -> str:
25 if model in cls.models:
26 return model
27 elif model in cls.model_aliases:
28 return cls.model_aliases[model]
29 else:
30 return cls.default_model
31
20 return cls.default_model
21
32 22 @classmethod
33 async def create_async_generator(
23 def create_completion(
34 24 cls,
35 25 model: str,
36 26 messages: Messages,
37 proxy: str = None,
38 27 response: str = "url", # base64 or url
39 28 **kwargs
40 ) -> AsyncResult:
41 # Retrieve the correct model to use
29 ) -> CreateResult:
42 30 model = cls.get_model(model)
43 31
44 # Format the prompt from the messages
45 prompt = messages[0]['content']
46
47 32 headers = {
48 "Content-Type": "application/json"
33 'Content-Type': 'application/json'
49 34 }
50 payload = {
51 "prompt": prompt,
35
36 data = {
37 "prompt": messages[-1]["content"],
52 38 "model": model,
53 39 "response": response
54 40 }
41
42 response = requests.post(cls.api_endpoint, headers=headers, json=data)
55 43
56 async with ClientSession(headers=headers) as session:
57 async with session.post(cls.api_endpoint, json=payload, proxy=proxy) as response:
58 response.raise_for_status()
59 text_data = await response.text()
44 result = cls.process_response(response)
45 yield result
60 46
61 try:
62 # Parse the JSON response
63 json_start = text_data.find('{')
64 json_data = text_data[json_start:]
65 data = json.loads(json_data)
66
67 # Check if the response contains images
68 if 'images' in data and len(data['images']) > 0:
69 image_url = data['images'][0]
70 yield ImageResponse(image_url, prompt)
71 else:
72 yield ImageResponse("No images found in the response.", prompt)
73 except json.JSONDecodeError:
74 yield ImageResponse("Failed to parse JSON. Response might not be in JSON format.", prompt)
47 @classmethod
48 def process_response(cls, response):
49 if response.status_code == 200:
50 try:
51 content = response.text.strip()
52 content = content.lstrip('_')
53 data = json.loads(content)
54 if data.get('status') and data.get('images'):
55 image_url = data['images'][0]
56 return ImageResponse(images=[image_url], alt="Generated Image")
57 else:
58 return "Error: No image URL found in the response"
59 except json.JSONDecodeError as e:
60 return f"Error: Unable to decode JSON response. Details: {str(e)}"
61 else:
62 return f"Error: {response.status_code}, Response: {response.text}"