Delete server.py
Browse files
server.py
DELETED
@@ -1,174 +0,0 @@
|
|
1 |
-
from flask import Flask, request, Response, jsonify, stream_with_context
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
-
import uuid
|
5 |
-
import time
|
6 |
-
|
7 |
-
app = Flask(__name__)
|
8 |
-
|
9 |
-
@app.route('/v1/chat/completions', methods=['POST'])
|
10 |
-
def chat_completions():
|
11 |
-
auth_header = request.headers.get('Authorization', '')
|
12 |
-
if auth_header.startswith('Bearer '):
|
13 |
-
token = auth_header[7:]
|
14 |
-
else:
|
15 |
-
return jsonify({"error": "Authorization header must be a Bearer token"}), 401
|
16 |
-
|
17 |
-
try:
|
18 |
-
openai_request = request.json
|
19 |
-
messages = openai_request.get('messages', [])
|
20 |
-
|
21 |
-
user_message = ""
|
22 |
-
for msg in reversed(messages):
|
23 |
-
if msg.get('role') == 'user':
|
24 |
-
user_message = msg.get('content', '')
|
25 |
-
break
|
26 |
-
|
27 |
-
if not user_message:
|
28 |
-
return jsonify({"error": "No user message found"}), 400
|
29 |
-
|
30 |
-
yun_api_url = 'https://ai.yun.139.com/api/outer/assistant/chat/add'
|
31 |
-
|
32 |
-
yun_headers = {
|
33 |
-
'User-Agent': 'Mozilla/5.0 (Linux; Android 14; 23049RAD8C Build/UKQ1.230804.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile Safari/537.36 MCloudApp/11.4.4',
|
34 |
-
'Accept': 'text/event-stream',
|
35 |
-
'Content-Type': 'application/json',
|
36 |
-
'sec-ch-ua': '"Chromium";v="118", "Android WebView";v="118", "Not=A?Brand";v="99"',
|
37 |
-
'x-yun-app-channel': '101',
|
38 |
-
'sec-ch-ua-mobile': '?1',
|
39 |
-
'authorization': f'Basic {token}',
|
40 |
-
'x-yun-api-version': 'v4',
|
41 |
-
'sec-ch-ua-platform': '"Android"',
|
42 |
-
'origin': 'https://yun.139.com',
|
43 |
-
'x-requested-with': 'com.chinamobile.mcloud',
|
44 |
-
'sec-fetch-site': 'same-site',
|
45 |
-
'sec-fetch-mode': 'cors',
|
46 |
-
'sec-fetch-dest': 'empty',
|
47 |
-
'referer': 'https://yun.139.com/',
|
48 |
-
'accept-language': 'zh,zh-CN;q=0.9,en-US;q=0.8,en;q=0.7'
|
49 |
-
}
|
50 |
-
|
51 |
-
yun_data = {
|
52 |
-
"userId": "",
|
53 |
-
"sessionId": "",
|
54 |
-
"content": {
|
55 |
-
"dialogue": user_message,
|
56 |
-
"prompt": "",
|
57 |
-
"commands": "",
|
58 |
-
"resourceType": "0",
|
59 |
-
"resourceId": "",
|
60 |
-
"dialogueType": "0",
|
61 |
-
"sourceChannel": "101",
|
62 |
-
"extInfo": "{\"h5Version\":\"1.7.4\"}"
|
63 |
-
},
|
64 |
-
"applicationType": "chat",
|
65 |
-
"applicationId": ""
|
66 |
-
}
|
67 |
-
|
68 |
-
stream_mode = openai_request.get('stream', False)
|
69 |
-
|
70 |
-
if stream_mode:
|
71 |
-
def generate():
|
72 |
-
response_id = f"chatcmpl-{str(uuid.uuid4())}"
|
73 |
-
start_time = time.time()
|
74 |
-
|
75 |
-
response = requests.post(yun_api_url, headers=yun_headers, json=yun_data, stream=True)
|
76 |
-
|
77 |
-
collected_text = ""
|
78 |
-
for line in response.iter_lines():
|
79 |
-
if line:
|
80 |
-
line_text = line.decode('utf-8')
|
81 |
-
if line_text.startswith('data:'):
|
82 |
-
data_json = json.loads(line_text[5:].strip())
|
83 |
-
if 'content' in data_json:
|
84 |
-
delta_text = data_json['content']
|
85 |
-
collected_text += delta_text
|
86 |
-
|
87 |
-
chunk = {
|
88 |
-
"id": response_id,
|
89 |
-
"object": "chat.completion.chunk",
|
90 |
-
"created": int(time.time()),
|
91 |
-
"model": "DeepSeek-R1",
|
92 |
-
"choices": [
|
93 |
-
{
|
94 |
-
"index": 0,
|
95 |
-
"delta": {
|
96 |
-
"content": delta_text
|
97 |
-
},
|
98 |
-
"finish_reason": None
|
99 |
-
}
|
100 |
-
]
|
101 |
-
}
|
102 |
-
|
103 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
104 |
-
|
105 |
-
final_chunk = {
|
106 |
-
"id": response_id,
|
107 |
-
"object": "chat.completion.chunk",
|
108 |
-
"created": int(time.time()),
|
109 |
-
"model": "DeepSeek-R1",
|
110 |
-
"choices": [
|
111 |
-
{
|
112 |
-
"index": 0,
|
113 |
-
"delta": {},
|
114 |
-
"finish_reason": "stop"
|
115 |
-
}
|
116 |
-
]
|
117 |
-
}
|
118 |
-
yield f"data: {json.dumps(final_chunk)}\n\n"
|
119 |
-
yield "data: [DONE]\n\n"
|
120 |
-
|
121 |
-
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
122 |
-
|
123 |
-
else:
|
124 |
-
response = requests.post(yun_api_url, headers=yun_headers, json=yun_data)
|
125 |
-
response_data = response.json()
|
126 |
-
|
127 |
-
assistant_message = ""
|
128 |
-
if 'data' in response_data and 'content' in response_data['data']:
|
129 |
-
assistant_message = response_data['data']['content']
|
130 |
-
|
131 |
-
openai_response = {
|
132 |
-
"id": f"chatcmpl-{str(uuid.uuid4())}",
|
133 |
-
"object": "chat.completion",
|
134 |
-
"created": int(time.time()),
|
135 |
-
"model": "DeepSeek-R1",
|
136 |
-
"choices": [
|
137 |
-
{
|
138 |
-
"index": 0,
|
139 |
-
"message": {
|
140 |
-
"role": "assistant",
|
141 |
-
"content": assistant_message
|
142 |
-
},
|
143 |
-
"finish_reason": "stop"
|
144 |
-
}
|
145 |
-
],
|
146 |
-
"usage": {
|
147 |
-
"prompt_tokens": len(user_message),
|
148 |
-
"completion_tokens": len(assistant_message),
|
149 |
-
"total_tokens": len(user_message) + len(assistant_message)
|
150 |
-
}
|
151 |
-
}
|
152 |
-
|
153 |
-
return jsonify(openai_response)
|
154 |
-
|
155 |
-
except Exception as e:
|
156 |
-
return jsonify({"error": str(e)}), 500
|
157 |
-
|
158 |
-
@app.route('/v1/models', methods=['GET'])
|
159 |
-
def list_models():
|
160 |
-
models = {
|
161 |
-
"object": "list",
|
162 |
-
"data": [
|
163 |
-
{
|
164 |
-
"id": "DeepSeek-R1",
|
165 |
-
"object": "model",
|
166 |
-
"created": int(time.time()),
|
167 |
-
"owned_by": "139.com"
|
168 |
-
}
|
169 |
-
]
|
170 |
-
}
|
171 |
-
return jsonify(models)
|
172 |
-
|
173 |
-
if __name__ == '__main__':
|
174 |
-
app.run(host='0.0.0.0', port=8080, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|