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

XFEstudio/gpt4free

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

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

代码差异

1 个文件 +35 -40
Modified g4f/Provider/nexra/NexraDallE.py +35 -40
@@ -1,66 +1,61 @@
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 NexraDallE(AsyncGeneratorProvider, ProviderModelMixin):
9 class NexraDallE(AbstractProvider, ProviderModelMixin):
12 10 label = "Nexra DALL-E"
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 = 'dalle'
13 working = True
14
15 default_model = "dalle"
18 16 models = [default_model]
19 17
20 18 @classmethod
21 19 def get_model(cls, model: str) -> str:
22 20 return cls.default_model
23
21
24 22 @classmethod
25 async def create_async_generator(
23 def create_completion(
26 24 cls,
27 25 model: str,
28 26 messages: Messages,
29 proxy: str = None,
30 response: str = "url", # base64 or url
31 27 **kwargs
32 ) -> AsyncResult:
33 # Retrieve the correct model to use
28 ) -> CreateResult:
34 29 model = cls.get_model(model)
35 30
36 # Format the prompt from the messages
37 prompt = messages[0]['content']
38
39 31 headers = {
40 "Content-Type": "application/json"
32 'Content-Type': 'application/json'
41 33 }
42 payload = {
43 "prompt": prompt,
34
35 data = {
36 "prompt": messages[-1]["content"],
44 37 "model": model,
45 "response": response
38 "response": "url"
46 39 }
40
41 response = requests.post(cls.api_endpoint, headers=headers, json=data)
47 42
48 async with ClientSession(headers=headers) as session:
49 async with session.post(cls.api_endpoint, json=payload, proxy=proxy) as response:
50 response.raise_for_status()
51 text_data = await response.text()
43 result = cls.process_response(response)
44 yield result # Повертаємо результат як генератор
52 45
53 try:
54 # Parse the JSON response
55 json_start = text_data.find('{')
56 json_data = text_data[json_start:]
57 data = json.loads(json_data)
58
59 # Check if the response contains images
60 if 'images' in data and len(data['images']) > 0:
61 image_url = data['images'][0]
62 yield ImageResponse(image_url, prompt)
63 else:
64 yield ImageResponse("No images found in the response.", prompt)
65 except json.JSONDecodeError:
66 yield ImageResponse("Failed to parse JSON. Response might not be in JSON format.", prompt)
46 @classmethod
47 def process_response(cls, response):
48 if response.status_code == 200:
49 try:
50 content = response.text.strip()
51 content = content.lstrip('_')
52 data = json.loads(content)
53 if data.get('status') and data.get('images'):
54 image_url = data['images'][0]
55 return ImageResponse(images=[image_url], alt="Generated Image")
56 else:
57 return "Error: No image URL found in the response"
58 except json.JSONDecodeError as e:
59 return f"Error: Unable to decode JSON response. Details: {str(e)}"
60 else:
61 return f"Error: {response.status_code}, Response: {response.text}"