feat: Remove response function yield
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import AsyncOpenAI #Use the Async client
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
|
@@ -16,16 +17,7 @@ client = AsyncOpenAI(
|
|
| 16 |
timeout=120.0
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
message,
|
| 21 |
-
history,
|
| 22 |
-
system_message,
|
| 23 |
-
max_tokens,
|
| 24 |
-
temperature,
|
| 25 |
-
top_p,
|
| 26 |
-
):
|
| 27 |
-
|
| 28 |
-
def sanitize_messages(history):
|
| 29 |
sanitized = []
|
| 30 |
for m in history:
|
| 31 |
# Check if it's the new dictionary format
|
|
@@ -37,30 +29,36 @@ async def respond(
|
|
| 37 |
if m[1]: sanitized.append({"role": "assistant", "content": m[1]})
|
| 38 |
return sanitized
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
messages = [{"role": "system", "content": system_message}]
|
| 41 |
messages.extend(sanitize_messages(history))
|
| 42 |
messages.append({"role": "user", "content": message})
|
| 43 |
|
| 44 |
-
response_text = ""
|
| 45 |
-
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
stream = await client.chat.completions.create(
|
| 49 |
model="qwen/qwen3-32b",
|
| 50 |
messages=messages,
|
| 51 |
max_tokens=max_tokens,
|
| 52 |
temperature=temperature,
|
| 53 |
top_p=top_p,
|
| 54 |
-
stream=True,
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
response_text += chunk.choices[0].delta.content
|
| 60 |
-
yield response_text
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
|
| 65 |
|
| 66 |
chatbot = gr.ChatInterface(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import AsyncOpenAI #Use the Async client
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
|
|
|
| 17 |
timeout=120.0
|
| 18 |
)
|
| 19 |
|
| 20 |
+
def sanitize_messages(history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
sanitized = []
|
| 22 |
for m in history:
|
| 23 |
# Check if it's the new dictionary format
|
|
|
|
| 29 |
if m[1]: sanitized.append({"role": "assistant", "content": m[1]})
|
| 30 |
return sanitized
|
| 31 |
|
| 32 |
+
def strip_think(text: str) -> str:
|
| 33 |
+
return re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
|
| 34 |
+
|
| 35 |
+
async def respond(
|
| 36 |
+
message,
|
| 37 |
+
history,
|
| 38 |
+
system_message,
|
| 39 |
+
max_tokens,
|
| 40 |
+
temperature,
|
| 41 |
+
top_p,
|
| 42 |
+
):
|
| 43 |
+
|
| 44 |
messages = [{"role": "system", "content": system_message}]
|
| 45 |
messages.extend(sanitize_messages(history))
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
+
completion = await client.chat.completions.create(
|
|
|
|
| 50 |
model="qwen/qwen3-32b",
|
| 51 |
messages=messages,
|
| 52 |
max_tokens=max_tokens,
|
| 53 |
temperature=temperature,
|
| 54 |
top_p=top_p,
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
+
text = completion.choices[0].message.content
|
| 58 |
+
return strip_think(text)
|
|
|
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
+
return f"Error: {str(e)}"
|
| 62 |
|
| 63 |
|
| 64 |
chatbot = gr.ChatInterface(
|