tioner23y commited on
Commit
dbf3f1c
Β·
verified Β·
1 Parent(s): 2d68970

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -42
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
- # Path to your GGUF model (make sure you uploaded it to the Space!)
6
- MODEL_PATH = "gemma-2b-it-Q4_K_M.gguf"
7
-
8
- # Load model
9
- llm = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=4, n_gpu_layers=20)
10
 
11
- # System prompt for Overthinking Coach AI
12
- SYSTEM_PROMPT = """
13
- You are "Overthinking Coach AI," a bilingual (Vietnamese & English) supportive companion who helps users with overthinking.
14
- Your personality: Warm, caring, calm, step-by-step, like a therapist.
15
 
16
- Language Rules (absolute priority):
17
- 1. Always reply in the SAME language as the user’s message.
18
- 2. If Vietnamese β†’ reply in Vietnamese.
19
- 3. If English β†’ reply in English.
20
- 4. If mixed β†’ reply using the same mix, prioritizing the dominant language.
21
- 5. If the user switches language mid-conversation β†’ switch immediately in your reply.
22
- 6. Do NOT default to English under any circumstances.
23
 
24
- Conversation Flow:
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; highlight what is certain vs uncertain. Do not give solutions yet.
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
- # Chat function
41
- def chat(user_input, history=[]):
42
- # Detect language
43
  try:
44
  lang = detect(user_input)
45
  except:
46
  lang = "en"
47
 
48
- # Build conversation prompt
49
  conversation = SYSTEM_PROMPT + "\n"
50
- for user, bot in history:
51
- conversation += f"User: {user}\nAI: {bot}\n"
52
  conversation += f"User: {user_input}\nAI:"
53
 
54
- # Run model
55
- output = llm(conversation, max_tokens=300, stop=["User:", "AI:"])
56
- response = output["choices"][0]["text"].strip()
 
 
 
 
57
 
58
- history.append((user_input, response))
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
- user_input = gr.Textbox(placeholder="Type here...")
66
- clear = gr.Button("Clear Chat")
67
 
68
- user_input.submit(chat, [user_input, chatbot], [chatbot, chatbot])
69
- clear.click(lambda: ([], []), None, [chatbot, chatbot])
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()