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

XFEstudio/gpt4free

Add Qwen_Qwen_2_5M_Demo provider

246b86fe
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +123 -1
Added g4f/Provider/hf_space/Qwen_Qwen_2_5M_Demo.py +118 -0
@@ -0,0 +1,118 @@
1 from __future__ import annotations
2
3 import aiohttp
4 import json
5 import uuid
6
7 from ...typing import AsyncResult, Messages
8 from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
9 from ..helper import format_prompt
10 from ...providers.response import JsonConversation, Reasoning
11 from ... import debug
12
13 class Qwen_Qwen_2_5M_Demo(AsyncGeneratorProvider, ProviderModelMixin):
14 url = "https://qwen-qwen2-5-1m-demo.hf.space"
15 api_endpoint = f"{url}/run/predict?__theme=light"
16
17 working = True
18 supports_stream = True
19 supports_system_message = True
20 supports_message_history = False
21
22 default_model = "qwen-qwen2-5m-demo"
23 models = [default_model]
24 model_aliases = {"qwen-2-5m": default_model}
25
26 @classmethod
27 async def create_async_generator(
28 cls,
29 model: str,
30 messages: Messages,
31 proxy: str = None,
32 return_conversation: bool = False,
33 conversation: JsonConversation = None,
34 **kwargs
35 ) -> AsyncResult:
36 def generate_session_hash():
37 """Generate a unique session hash."""
38 return str(uuid.uuid4()).replace('-', '')[:12]
39
40 # Generate a unique session hash
41 session_hash = generate_session_hash() if conversation is None else getattr(conversation, "session_hash")
42 if return_conversation:
43 yield JsonConversation(session_hash=session_hash)
44
45 prompt = format_prompt(messages) if conversation is None else messages[-1]["content"]
46
47 headers = {
48 'accept': '*/*',
49 'accept-language': 'en-US',
50 'content-type': 'application/json',
51 'origin': cls.url,
52 'referer': f'{cls.url}/?__theme=light',
53 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
54 }
55
56 payload_predict = {
57 "data":[{"files":[],"text":prompt},[],[]],
58 "event_data": None,
59 "fn_index": 1,
60 "trigger_id": 5,
61 "session_hash": session_hash
62 }
63
64 async with aiohttp.ClientSession() as session:
65 # Send join request
66 async with session.post(cls.api_endpoint, headers=headers, json=payload_predict) as response:
67 data = (await response.json())['data']
68
69 join_url = f"{cls.url}/queue/join?__theme=light"
70 join_data = {"data":[[[{"id":None,"elem_id":None,"elem_classes":None,"name":None,"text":prompt,"flushing":None,"avatar":"","files":[]},None]],None,0],"event_data":None,"fn_index":2,"trigger_id":5,"session_hash":session_hash}
71
72 async with session.post(join_url, headers=headers, json=join_data) as response:
73 event_id = (await response.json())['event_id']
74
75 # Prepare data stream request
76 url_data = f'{cls.url}/queue/data?session_hash={session_hash}'
77
78 headers_data = {
79 'accept': 'text/event-stream',
80 'referer': f'{cls.url}/?__theme=light',
81 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
82 }
83 # Send data stream request
84 async with session.get(url_data, headers=headers_data) as response:
85 yield_response = ""
86 yield_response_len = 0
87 async for line in response.content:
88 decoded_line = line.decode('utf-8')
89 if decoded_line.startswith('data: '):
90 try:
91 json_data = json.loads(decoded_line[6:])
92
93 # Look for generation stages
94 if json_data.get('msg') == 'process_generating':
95 if 'output' in json_data and 'data' in json_data['output'] and json_data['output']['data'][0]:
96 output_data = json_data['output']['data'][0][0]
97 if len(output_data) > 2:
98 text = output_data[2].split("\n<summary>")[0]
99 if text == "Qwen is thinking...":
100 yield Reasoning(None, text)
101 elif text.startswith(yield_response):
102 yield text[yield_response_len:]
103 else:
104 yield text
105 yield_response_len = len(text)
106 yield_response = text
107
108 # Check for completion
109 if json_data.get('msg') == 'process_completed':
110 # Final check to ensure we get the complete response
111 if 'output' in json_data and 'data' in json_data['output']:
112 output_data = json_data['output']['data'][0][0][1][0]["text"].split("\n<summary>")[0]
113 yield output_data[yield_response_len:]
114 yield_response_len = len(text)
115 break
116
117 except json.JSONDecodeError:
118 debug.log("Could not parse JSON:", decoded_line)
Modified g4f/Provider/hf_space/__init__.py +5 -1
@@ -11,6 +11,7 @@ from .BlackForestLabsFlux1Schnell import BlackForestLabsFlux1Schnell
11 11 from .VoodoohopFlux1Schnell import VoodoohopFlux1Schnell
12 12 from .CohereForAI import CohereForAI
13 13 from .Qwen_QVQ_72B import Qwen_QVQ_72B
14 from .Qwen_Qwen_2_5M_Demo import Qwen_Qwen_2_5M_Demo
14 15 from .Qwen_Qwen_2_72B_Instruct import Qwen_Qwen_2_72B_Instruct
15 16 from .StableDiffusion35Large import StableDiffusion35Large
16 17
@@ -23,7 +24,10 @@ class HuggingSpace(AsyncGeneratorProvider, ProviderModelMixin):
23 24 default_model = Qwen_Qwen_2_72B_Instruct.default_model
24 25 default_image_model = BlackForestLabsFlux1Dev.default_model
25 26 default_vision_model = Qwen_QVQ_72B.default_model
26 providers = [BlackForestLabsFlux1Dev, BlackForestLabsFlux1Schnell, VoodoohopFlux1Schnell, CohereForAI, Qwen_QVQ_72B, Qwen_Qwen_2_72B_Instruct, StableDiffusion35Large]
27 providers = [
28 BlackForestLabsFlux1Dev, BlackForestLabsFlux1Schnell, VoodoohopFlux1Schnell,
29 CohereForAI, Qwen_QVQ_72B, Qwen_Qwen_2_5M_Demo, Qwen_Qwen_2_72B_Instruct, StableDiffusion35Large
30 ]
27 31
28 32 @classmethod
29 33 def get_parameters(cls, **kwargs) -> dict: