devin15 commited on
Commit
46912f9
·
verified ·
1 Parent(s): 5a1591b

Add Dockerfile and configure app port to 8080

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -0
  2. README.md +6 -4
  3. server.py +180 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . /app/
6
+
7
+ RUN pip install --no-cache-dir flask requests && chmod 777 -R /app
8
+
9
+ EXPOSE 8080
10
+
11
+ CMD ["python", "server.py"]
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Driver Yidong Ds
3
- emoji: 🐢
4
- colorFrom: red
5
- colorTo: indigo
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: driver-yidong-ds
3
+ emoji: "🤗"
4
+ colorFrom: pink
5
+ colorTo: gray
6
  sdk: docker
7
  pinned: false
8
+ app_port: 8080
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
12
+
server.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ print(f"token is: {token}")
32
+
33
+ yun_headers = {
34
+ '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',
35
+ 'Accept': 'text/event-stream',
36
+ 'Content-Type': 'application/json',
37
+ 'sec-ch-ua': '"Chromium";v="118", "Android WebView";v="118", "Not=A?Brand";v="99"',
38
+ 'x-yun-app-channel': '101',
39
+ 'sec-ch-ua-mobile': '?1',
40
+ 'authorization': f'Basic {token}',
41
+ 'x-yun-api-version': 'v4',
42
+ 'sec-ch-ua-platform': '"Android"',
43
+ 'origin': 'https://yun.139.com',
44
+ 'x-requested-with': 'com.chinamobile.mcloud',
45
+ 'sec-fetch-site': 'same-site',
46
+ 'sec-fetch-mode': 'cors',
47
+ 'sec-fetch-dest': 'empty',
48
+ 'referer': 'https://yun.139.com/',
49
+ 'accept-language': 'zh,zh-CN;q=0.9,en-US;q=0.8,en;q=0.7'
50
+ }
51
+
52
+ yun_data = {
53
+ "userId": "",
54
+ "sessionId": "",
55
+ "content": {
56
+ "dialogue": user_message,
57
+ "prompt": "",
58
+ "commands": "",
59
+ "resourceType": "0",
60
+ "resourceId": "",
61
+ "dialogueType": "0",
62
+ "sourceChannel": "101",
63
+ "extInfo": "{\"h5Version\":\"1.7.4\"}"
64
+ },
65
+ "applicationType": "chat",
66
+ "applicationId": ""
67
+ }
68
+
69
+ stream_mode = openai_request.get('stream', False)
70
+
71
+ if stream_mode:
72
+ def generate():
73
+ response_id = f"chatcmpl-{str(uuid.uuid4())}"
74
+ start_time = time.time()
75
+
76
+ response = requests.post(yun_api_url, headers=yun_headers, json=yun_data, stream=True)
77
+
78
+ collected_text = ""
79
+ for line in response.iter_lines():
80
+ if line:
81
+ line_text = line.decode('utf-8')
82
+ print(f"line_text: {line_text}")
83
+ if line_text.startswith('data:'):
84
+ data_json = json.loads(line_text[5:].strip())
85
+ if 'data' in data_json and 'flowResult' in data_json['data'] and 'outContent' in data_json['data']['flowResult']:
86
+ delta_text = data_json['data']['flowResult']['outContent']
87
+
88
+ collected_text += delta_text
89
+
90
+ chunk = {
91
+ "id": response_id,
92
+ "object": "chat.completion.chunk",
93
+ "created": int(time.time()),
94
+ "model": "DeepSeek-R1",
95
+ "choices": [
96
+ {
97
+ "index": 0,
98
+ "delta": {
99
+ "content": delta_text
100
+ },
101
+ "finish_reason": None
102
+ }
103
+ ]
104
+ }
105
+
106
+ yield f"data: {json.dumps(chunk)}\n\n"
107
+
108
+ final_chunk = {
109
+ "id": response_id,
110
+ "object": "chat.completion.chunk",
111
+ "created": int(time.time()),
112
+ "model": "DeepSeek-R1",
113
+ "choices": [
114
+ {
115
+ "index": 0,
116
+ "delta": {},
117
+ "finish_reason": "stop"
118
+ }
119
+ ]
120
+ }
121
+ yield f"data: {json.dumps(final_chunk)}\n\n"
122
+ yield "data: [DONE]\n\n"
123
+
124
+ return Response(stream_with_context(generate()), content_type='text/event-stream')
125
+
126
+ else:
127
+ response = requests.post(yun_api_url, headers=yun_headers, json=yun_data)
128
+ if isinstance(response.content, bytes):
129
+ response = response.content.decode('utf-8')
130
+ print(f"content is {response}")
131
+ response_data = json.loads(response)
132
+
133
+ assistant_message = ""
134
+ if 'data' in response_data and 'content' in response_data['data']:
135
+ assistant_message = response_data['data']['content']
136
+
137
+ openai_response = {
138
+ "id": f"chatcmpl-{str(uuid.uuid4())}",
139
+ "object": "chat.completion",
140
+ "created": int(time.time()),
141
+ "model": "DeepSeek-R1",
142
+ "choices": [
143
+ {
144
+ "index": 0,
145
+ "message": {
146
+ "role": "assistant",
147
+ "content": assistant_message
148
+ },
149
+ "finish_reason": "stop"
150
+ }
151
+ ],
152
+ "usage": {
153
+ "prompt_tokens": len(user_message),
154
+ "completion_tokens": len(assistant_message),
155
+ "total_tokens": len(user_message) + len(assistant_message)
156
+ }
157
+ }
158
+
159
+ return jsonify(openai_response)
160
+
161
+ except Exception as e:
162
+ return jsonify({"error": str(e)}), 500
163
+
164
+ @app.route('/v1/models', methods=['GET'])
165
+ def list_models():
166
+ models = {
167
+ "object": "list",
168
+ "data": [
169
+ {
170
+ "id": "DeepSeek-R1",
171
+ "object": "model",
172
+ "created": int(time.time()),
173
+ "owned_by": "139.com"
174
+ }
175
+ ]
176
+ }
177
+ return jsonify(models)
178
+
179
+ if __name__ == '__main__':
180
+ app.run(host='0.0.0.0', port=8080, debug=True)