Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from llama_cpp import Llama
|
| 3 |
from langdetect import detect
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
MODEL_PATH = "
|
| 7 |
-
|
| 8 |
-
# Load model
|
| 9 |
-
llm = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=4, n_gpu_layers=20)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Your personality: Warm, caring, calm, step-by-step, like a therapist.
|
| 15 |
|
| 16 |
-
Language
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
6. Do NOT default to English under any circumstances.
|
| 23 |
|
| 24 |
-
Conversation
|
| 25 |
1. LISTEN β Acknowledge and show you heard the user.
|
| 26 |
2. COMFORT β Reflect their feelings with kindness.
|
| 27 |
-
3. CLARIFY β Gently analyze the situation
|
| 28 |
4. ASK β βWould you like me to suggest a way to ease your overthinking?β
|
| 29 |
5. If user agrees β ASSESS severity (low / medium / high).
|
| 30 |
6. SUGGEST β Give ONE coping technique suitable for severity. Keep it short and practical.
|
| 31 |
-
7. PAUSE β Wait for user response before continuing.
|
| 32 |
-
|
| 33 |
-
Important:
|
| 34 |
-
- Never skip the clarify step.
|
| 35 |
-
- Keep responses calm, empathetic, and safe.
|
| 36 |
-
- Keep paragraphs short (1β4 sentences).
|
| 37 |
-
- If the user mentions self-harm or suicide β pause and share hotline (Vietnam 111 or local emergency).
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# Detect language
|
| 43 |
try:
|
| 44 |
lang = detect(user_input)
|
| 45 |
except:
|
| 46 |
lang = "en"
|
| 47 |
|
| 48 |
-
#
|
| 49 |
conversation = SYSTEM_PROMPT + "\n"
|
| 50 |
-
for
|
| 51 |
-
conversation += f"User: {
|
| 52 |
conversation += f"User: {user_input}\nAI:"
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
output = llm(
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
history.append((user_input,
|
| 59 |
return history, history
|
| 60 |
|
| 61 |
-
# Gradio UI
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
-
gr.Markdown("## π§ Overthinking Coach AI (Bilingual Vietnamese-English)")
|
| 64 |
chatbot = gr.Chatbot()
|
| 65 |
-
|
| 66 |
-
clear = gr.Button("Clear
|
| 67 |
|
| 68 |
-
|
| 69 |
-
clear.click(lambda:
|
| 70 |
|
| 71 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from langdetect import detect
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# Load the model (local GGUF file)
|
| 6 |
+
MODEL_PATH = "TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
|
| 7 |
+
llm = Llama(model_path=MODEL_PATH, n_ctx=2048)
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
SYSTEM_PROMPT = """You are "Overthinking Coach AI".
|
| 10 |
+
Your role: A bilingual (Vietnamese & English) supportive companion that helps users with overthinking.
|
| 11 |
+
Your personality: Warm, caring, step-by-step, like a therapist.
|
|
|
|
| 12 |
|
| 13 |
+
Language rules:
|
| 14 |
+
- Detect the language in EVERY user message.
|
| 15 |
+
- If Vietnamese β reply in Vietnamese.
|
| 16 |
+
- If English β reply in English.
|
| 17 |
+
- If mixed β reply in the same mix, prioritizing the main language.
|
| 18 |
+
- Switch language immediately if the user switches.
|
|
|
|
| 19 |
|
| 20 |
+
Conversation flow (always follow in order):
|
| 21 |
1. LISTEN β Acknowledge and show you heard the user.
|
| 22 |
2. COMFORT β Reflect their feelings with kindness.
|
| 23 |
+
3. CLARIFY β Gently analyze the situation, highlight what is certain and what is uncertain. Do not give solutions yet.
|
| 24 |
4. ASK β βWould you like me to suggest a way to ease your overthinking?β
|
| 25 |
5. If user agrees β ASSESS severity (low / medium / high).
|
| 26 |
6. SUGGEST β Give ONE coping technique suitable for severity. Keep it short and practical.
|
| 27 |
+
7. PAUSE β Wait for userβs response before continuing.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
def chat(user_input, history):
|
| 31 |
+
# detect language
|
|
|
|
| 32 |
try:
|
| 33 |
lang = detect(user_input)
|
| 34 |
except:
|
| 35 |
lang = "en"
|
| 36 |
|
| 37 |
+
# build conversation
|
| 38 |
conversation = SYSTEM_PROMPT + "\n"
|
| 39 |
+
for u, a in history:
|
| 40 |
+
conversation += f"User: {u}\nAI: {a}\n"
|
| 41 |
conversation += f"User: {user_input}\nAI:"
|
| 42 |
|
| 43 |
+
# run model
|
| 44 |
+
output = llm(
|
| 45 |
+
conversation,
|
| 46 |
+
max_tokens=256,
|
| 47 |
+
stop=["User:"]
|
| 48 |
+
)
|
| 49 |
+
reply = output["choices"][0]["text"].strip()
|
| 50 |
|
| 51 |
+
history.append((user_input, reply))
|
| 52 |
return history, history
|
| 53 |
|
|
|
|
| 54 |
with gr.Blocks() as demo:
|
|
|
|
| 55 |
chatbot = gr.Chatbot()
|
| 56 |
+
msg = gr.Textbox(label="Your message")
|
| 57 |
+
clear = gr.Button("Clear")
|
| 58 |
|
| 59 |
+
msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
|
| 60 |
+
clear.click(lambda: None, None, chatbot)
|
| 61 |
|
| 62 |
demo.launch()
|