yuantao-infini-ai commited on
Commit
fb0b883
·
verified ·
1 Parent(s): 862cb29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py CHANGED
@@ -143,6 +143,27 @@ def check_messages(history, message, audio):
143
 
144
  return history, gr.MultimodalTextbox(value=None, interactive=False), None
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def bot(
147
  history: list,
148
  top_p: float,
@@ -161,6 +182,24 @@ def bot(
161
 
162
  msgs = history2messages(history)
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  API_URL = "http://8.152.0.142:8000/v1/chat"
165
 
166
  payload = {
 
143
 
144
  return history, gr.MultimodalTextbox(value=None, interactive=False), None
145
 
146
+ def get_streaming_response(response: requests.Response):
147
+ for chunk in response.iter_lines():
148
+ if chunk:
149
+ data = chunk.decode("utf-8")
150
+ if data.startswith('data: '):
151
+ json_str = data[6:]
152
+
153
+ if json_str == '[DONE]':
154
+ break
155
+
156
+ try:
157
+ chunk = json.loads(json_str)
158
+ delta = chunk.get('choices', [{}])[0].get('delta', {})
159
+ new_text = delta.get('content', '')
160
+
161
+ if new_text:
162
+ yield new_text
163
+ except (json.JSONDecodeError, IndexError):
164
+ print(f"Skipping malformed SSE line: {json_str}")
165
+ continue
166
+
167
  def bot(
168
  history: list,
169
  top_p: float,
 
182
 
183
  msgs = history2messages(history)
184
 
185
+ headers = {
186
+ "Content-Type": "application/json"
187
+ }
188
+
189
+ payload = {
190
+ "model": "megrez-moe-waic",
191
+ "messages": [{'role': 'user', 'content': '你谁'}],
192
+ "max_tokens": 50,
193
+ "temperature": max(0.7, 0),
194
+ "top_p": 0.9,
195
+ "stream": True
196
+ }
197
+ API_URL = "http://8.152.0.142:1002/v1/chat/completions"
198
+ response = requests.post(API_URL, headers=headers, data=json.dumps(payload), timeout=60, stream=True)
199
+ for chunk in get_streaming_response(response):
200
+ print(chunk)
201
+ # yield chunk
202
+ time.sleep(0.01)
203
  API_URL = "http://8.152.0.142:8000/v1/chat"
204
 
205
  payload = {