Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,6 @@ CHAT_URL = BASE_URL + "/conversation"
|
|
| 14 |
# Initialize the token and message history
|
| 15 |
token = ""
|
| 16 |
messHistory: list = []
|
| 17 |
-
|
| 18 |
async def chat(messList):
|
| 19 |
"""Async function to send and receive messages with the server."""
|
| 20 |
global token
|
|
@@ -31,29 +30,39 @@ async def chat(messList):
|
|
| 31 |
"stream": True
|
| 32 |
}
|
| 33 |
|
|
|
|
| 34 |
# Make the POST request to the chat API
|
| 35 |
async with session.post(CHAT_URL, json=body) as resp:
|
| 36 |
if resp.status != 200:
|
| 37 |
return "Error occurred during the chat process."
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
async for chunk in resp.content.iter_any():
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
else:
|
| 47 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
data_dict = json.loads(json_str)
|
| 49 |
-
fullmessage += data_dict
|
| 50 |
-
token = data_dict
|
| 51 |
-
except
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
messHistory.append({"role": "assistant", "content": fullmessage}) # Append assistant response
|
| 55 |
return fullmessage
|
| 56 |
|
|
|
|
| 57 |
def gradio_chat(user_input, mode):
|
| 58 |
"""Synchronous wrapper for the async chat function, integrated with Gradio."""
|
| 59 |
messHistory.append({"role": "user", "content": f"[{mode}] {user_input}"})
|
|
|
|
| 14 |
# Initialize the token and message history
|
| 15 |
token = ""
|
| 16 |
messHistory: list = []
|
|
|
|
| 17 |
async def chat(messList):
|
| 18 |
"""Async function to send and receive messages with the server."""
|
| 19 |
global token
|
|
|
|
| 30 |
"stream": True
|
| 31 |
}
|
| 32 |
|
| 33 |
+
fullmessage = ""
|
| 34 |
# Make the POST request to the chat API
|
| 35 |
async with session.post(CHAT_URL, json=body) as resp:
|
| 36 |
if resp.status != 200:
|
| 37 |
return "Error occurred during the chat process."
|
| 38 |
|
| 39 |
+
# Use a buffer to accumulate data chunks
|
| 40 |
+
buffer = ""
|
| 41 |
async for chunk in resp.content.iter_any():
|
| 42 |
+
buffer += chunk.decode("utf-8")
|
| 43 |
+
|
| 44 |
+
# Attempt to decode JSON objects from the buffer
|
| 45 |
+
while True:
|
|
|
|
| 46 |
try:
|
| 47 |
+
# Find the end of the JSON object
|
| 48 |
+
index = buffer.index('\n')
|
| 49 |
+
json_str = buffer[:index].strip()
|
| 50 |
+
buffer = buffer[index+1:]
|
| 51 |
+
|
| 52 |
+
if json_str.strip() == "[DONE]":
|
| 53 |
+
break
|
| 54 |
+
|
| 55 |
data_dict = json.loads(json_str)
|
| 56 |
+
fullmessage += data_dict.get("message", "")
|
| 57 |
+
token = data_dict.get("resp_token", token) # Update token
|
| 58 |
+
except (ValueError, json.JSONDecodeError):
|
| 59 |
+
# Handle incomplete or malformed JSON
|
| 60 |
+
break
|
| 61 |
|
| 62 |
messHistory.append({"role": "assistant", "content": fullmessage}) # Append assistant response
|
| 63 |
return fullmessage
|
| 64 |
|
| 65 |
+
|
| 66 |
def gradio_chat(user_input, mode):
|
| 67 |
"""Synchronous wrapper for the async chat function, integrated with Gradio."""
|
| 68 |
messHistory.append({"role": "user", "content": f"[{mode}] {user_input}"})
|