Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,13 @@ def build_prompt(history, new_message):
|
|
| 27 |
return prompt
|
| 28 |
|
| 29 |
def chat(history, new_message):
|
| 30 |
-
# history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
prompt = build_prompt(history, new_message)
|
| 32 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
| 33 |
with torch.no_grad():
|
|
@@ -46,14 +52,13 @@ def chat(history, new_message):
|
|
| 46 |
response = output_text.strip()
|
| 47 |
return response
|
| 48 |
|
| 49 |
-
# CHỈ DÙNG Gradio Interface, input là [history, new_message]
|
| 50 |
iface = gr.Interface(
|
| 51 |
fn=chat,
|
| 52 |
inputs=[
|
| 53 |
-
gr.
|
| 54 |
-
gr.
|
| 55 |
],
|
| 56 |
-
outputs="
|
| 57 |
title="MindVR Therapy Chatbot",
|
| 58 |
allow_flagging="never"
|
| 59 |
)
|
|
|
|
| 27 |
return prompt
|
| 28 |
|
| 29 |
def chat(history, new_message):
|
| 30 |
+
# Nếu history là chuỗi (do nhập tay trên UI), thì parse sang list
|
| 31 |
+
if isinstance(history, str):
|
| 32 |
+
try:
|
| 33 |
+
import ast
|
| 34 |
+
history = ast.literal_eval(history)
|
| 35 |
+
except:
|
| 36 |
+
history = [history]
|
| 37 |
prompt = build_prompt(history, new_message)
|
| 38 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
| 39 |
with torch.no_grad():
|
|
|
|
| 52 |
response = output_text.strip()
|
| 53 |
return response
|
| 54 |
|
|
|
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=chat,
|
| 57 |
inputs=[
|
| 58 |
+
gr.Textbox(lines=8, label="History (JSON list, ví dụ: [\"User: Xin chào\"] )"),
|
| 59 |
+
gr.Textbox(label="New message")
|
| 60 |
],
|
| 61 |
+
outputs=gr.Textbox(label="AI Response"),
|
| 62 |
title="MindVR Therapy Chatbot",
|
| 63 |
allow_flagging="never"
|
| 64 |
)
|