Spaces:
Sleeping
Sleeping
修复 chat()
Browse files
app.py
CHANGED
|
@@ -31,32 +31,46 @@ print("Model loaded successfully!")
|
|
| 31 |
|
| 32 |
def chat(message, history):
|
| 33 |
"""Handle chat interactions"""
|
| 34 |
-
|
| 35 |
-
conversation = ""
|
| 36 |
-
|
| 37 |
-
# Only use last 3 turns
|
| 38 |
recent_history = history[-3:] if len(history) > 3 else history
|
| 39 |
-
|
| 40 |
-
for
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
response = llm(
|
| 49 |
conversation,
|
| 50 |
-
max_tokens=128,
|
| 51 |
temperature=0.7,
|
| 52 |
top_p=0.9,
|
| 53 |
-
top_k=40,
|
| 54 |
repeat_penalty=1.1,
|
| 55 |
-
stop=["User:", "
|
| 56 |
-
echo=False
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
return response[
|
|
|
|
| 60 |
|
| 61 |
# Create interface WITHOUT example caching
|
| 62 |
demo = gr.ChatInterface(
|
|
|
|
| 31 |
|
| 32 |
def chat(message, history):
|
| 33 |
"""Handle chat interactions"""
|
| 34 |
+
|
| 35 |
+
conversation = "System: You are a helpful assistant.\n"
|
| 36 |
+
|
| 37 |
+
# Only use last 3 turns
|
| 38 |
recent_history = history[-3:] if len(history) > 3 else history
|
| 39 |
+
|
| 40 |
+
for msg in recent_history:
|
| 41 |
+
# ChatInterface passes dict messages
|
| 42 |
+
if isinstance(msg, dict):
|
| 43 |
+
role = msg.get("role")
|
| 44 |
+
content = msg.get("content", "")
|
| 45 |
+
|
| 46 |
+
if role == "user":
|
| 47 |
+
conversation += f"User: {content}\n"
|
| 48 |
+
elif role == "assistant":
|
| 49 |
+
conversation += f"Assistant: {content}\n"
|
| 50 |
+
|
| 51 |
+
# fallback: old tuple/list history
|
| 52 |
+
else:
|
| 53 |
+
human, assistant = msg
|
| 54 |
+
conversation += f"User: {human}\n"
|
| 55 |
+
conversation += f"Assistant: {assistant or ''}\n"
|
| 56 |
+
|
| 57 |
+
# Add current user message
|
| 58 |
+
conversation += f"User: {message}\nAssistant:"
|
| 59 |
+
|
| 60 |
+
# LLM inference
|
| 61 |
response = llm(
|
| 62 |
conversation,
|
| 63 |
+
max_tokens=128,
|
| 64 |
temperature=0.7,
|
| 65 |
top_p=0.9,
|
| 66 |
+
top_k=40,
|
| 67 |
repeat_penalty=1.1,
|
| 68 |
+
stop=["User:", "Assistant:"],
|
| 69 |
+
echo=False
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
return response["choices"][0]["text"].strip()
|
| 73 |
+
|
| 74 |
|
| 75 |
# Create interface WITHOUT example caching
|
| 76 |
demo = gr.ChatInterface(
|