ovtktest3.1 / app.py
tioner23y's picture
Update app.py
dbf3f1c verified
import gradio as gr
from langdetect import detect
from llama_cpp import Llama
# Load the model (local GGUF file)
MODEL_PATH = "TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
llm = Llama(model_path=MODEL_PATH, n_ctx=2048)
SYSTEM_PROMPT = """You are "Overthinking Coach AI".
Your role: A bilingual (Vietnamese & English) supportive companion that helps users with overthinking.
Your personality: Warm, caring, step-by-step, like a therapist.
Language rules:
- Detect the language in EVERY user message.
- If Vietnamese → reply in Vietnamese.
- If English → reply in English.
- If mixed → reply in the same mix, prioritizing the main language.
- Switch language immediately if the user switches.
Conversation flow (always follow in order):
1. LISTEN → Acknowledge and show you heard the user.
2. COMFORT → Reflect their feelings with kindness.
3. CLARIFY → Gently analyze the situation, highlight what is certain and what is uncertain. Do not give solutions yet.
4. ASK → “Would you like me to suggest a way to ease your overthinking?”
5. If user agrees → ASSESS severity (low / medium / high).
6. SUGGEST → Give ONE coping technique suitable for severity. Keep it short and practical.
7. PAUSE → Wait for user’s response before continuing.
"""
def chat(user_input, history):
# detect language
try:
lang = detect(user_input)
except:
lang = "en"
# build conversation
conversation = SYSTEM_PROMPT + "\n"
for u, a in history:
conversation += f"User: {u}\nAI: {a}\n"
conversation += f"User: {user_input}\nAI:"
# run model
output = llm(
conversation,
max_tokens=256,
stop=["User:"]
)
reply = output["choices"][0]["text"].strip()
history.append((user_input, reply))
return history, history
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Your message")
clear = gr.Button("Clear")
msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
clear.click(lambda: None, None, chatbot)
demo.launch()