返回提交历史
Added
g4f/Provider/hf_space/BAAI_Ling.py
+104
-0
Modified
g4f/Provider/hf_space/__init__.py
+2
-0
XFEstudio/gpt4free
Add BAAI_Ling provider for Ling-1T model
Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>
c3644252
代码差异
2 个文件
+106
-0
@@ -0,0 +1,104 @@
1
from __future__ import annotations
2
3
import aiohttp
4
import json
5
import uuid
6
7
from ...typing import AsyncResult, Messages
8
from ...providers.response import JsonConversation, Reasoning
9
from ...requests.raise_for_status import raise_for_status
10
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
11
from ..helper import format_prompt, get_last_user_message
12
from ... import debug
13
14
class BAAI_Ling(AsyncGeneratorProvider, ProviderModelMixin):
15
label = "BAAI Ling"
16
url = "https://instspace-ling-playground.hf.space"
17
api_endpoint = f"{url}/gradio_api/queue/join"
18
19
working = True
20
supports_stream = True
21
supports_system_message = True
22
supports_message_history = False
23
24
default_model = "ling-1t"
25
models = [default_model]
26
model_aliases = {
27
"ling-1t": default_model,
28
"ling": default_model,
29
}
30
31
@classmethod
32
async def create_async_generator(
33
cls,
34
model: str,
35
messages: Messages,
36
proxy: str = None,
37
conversation: JsonConversation = None,
38
**kwargs
39
) -> AsyncResult:
40
if conversation is None or not hasattr(conversation, 'session_hash'):
41
conversation = JsonConversation(session_hash=str(uuid.uuid4()).replace('-', '')[:12])
42
43
prompt = format_prompt(messages) if conversation is None else get_last_user_message(messages)
44
45
headers = {
46
'accept': '*/*',
47
'accept-language': 'en-US,en;q=0.9',
48
'content-type': 'application/json',
49
'origin': cls.url,
50
'referer': f'{cls.url}/',
51
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
52
}
53
54
payload = {
55
"data": [prompt],
56
"event_data": None,
57
"fn_index": 0,
58
"trigger_id": 5,
59
"session_hash": conversation.session_hash
60
}
61
62
async with aiohttp.ClientSession() as session:
63
async with session.post(cls.api_endpoint, headers=headers, json=payload, proxy=proxy) as response:
64
await raise_for_status(response)
65
await response.json()
66
67
data_url = f'{cls.url}/gradio_api/queue/data?session_hash={conversation.session_hash}'
68
headers_data = {
69
'accept': 'text/event-stream',
70
'referer': f'{cls.url}/',
71
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
72
}
73
74
async with session.get(data_url, headers=headers_data, proxy=proxy) as response:
75
full_response = ""
76
async for line in response.content:
77
decoded_line = line.decode('utf-8')
78
if decoded_line.startswith('data: '):
79
try:
80
json_data = json.loads(decoded_line[6:])
81
82
if json_data.get('msg') == 'process_generating':
83
if 'output' in json_data and 'data' in json_data['output']:
84
output_data = json_data['output']['data']
85
if output_data and len(output_data) > 0:
86
text = output_data[0]
87
if isinstance(text, str) and text.startswith(full_response):
88
yield text[len(full_response):]
89
full_response = text
90
elif isinstance(text, str):
91
yield text
92
full_response = text
93
94
elif json_data.get('msg') == 'process_completed':
95
if 'output' in json_data and 'data' in json_data['output']:
96
output_data = json_data['output']['data']
97
if output_data and len(output_data) > 0:
98
final_text = output_data[0]
99
if isinstance(final_text, str) and len(final_text) > len(full_response):
100
yield final_text[len(full_response):]
101
break
102
103
except json.JSONDecodeError:
104
debug.log("Could not parse JSON:", decoded_line)
@@ -6,6 +6,7 @@ from ...typing import AsyncResult, Messages, MediaListType
6
6
from ...errors import ResponseError
7
7
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
8
8
9
from .BAAI_Ling import BAAI_Ling
9
10
from .BlackForestLabs_Flux1Dev import BlackForestLabs_Flux1Dev
10
11
from .BlackForestLabs_Flux1KontextDev import BlackForestLabs_Flux1KontextDev
11
12
from .CohereForAI_C4AI_Command import CohereForAI_C4AI_Command
@@ -27,6 +28,7 @@ class HuggingSpace(AsyncGeneratorProvider, ProviderModelMixin):
27
28
default_image_model = BlackForestLabs_Flux1Dev.default_model
28
29
default_vision_model = Microsoft_Phi_4_Multimodal.default_model
29
30
providers = [
31
BAAI_Ling,
30
32
BlackForestLabs_Flux1Dev,
31
33
BlackForestLabs_Flux1KontextDev,
32
34
CohereForAI_C4AI_Command,