hemhemoh commited on
Commit
8f219da
·
verified ·
1 Parent(s): 8c4c56f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -61
app.py CHANGED
@@ -1,67 +1,65 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Load model and tokenizer
6
- model_id = "Yhemhemoh/Gemma-2-2b-it-wazobia-wellness-bot" # Replace with your model path
7
- model = AutoModelForCausalLM.from_pretrained(model_id)
8
- tokenizer = AutoTokenizer.from_pretrained(model_id)
9
 
10
- def generate_response(user_input, selected_language="English"):
11
- # Predefined instruction
12
- predefined_instruction = (
13
- "You are a highly skilled and empathetic mental health therapist fluent in "
14
- "English, Yoruba, Igbo, and Hausa. Your task is to listen carefully to the user's concern "
15
- "and respond with kindness, empathy, and respect. Always address the user directly in the second person, "
16
- "avoiding third-person references. Never suggest harm to the user or others."
17
- )
18
-
19
- # Format the input
20
- formatted_input = (
21
- f"<start_of_turn>user {predefined_instruction}\n\n"
22
- f"User's concern:\n{user_input}\n\n"
23
- f"Respond empathetically, kindly, and directly to the above concern in {selected_language}.<end_of_turn>\n"
24
- f"<start_of_turn>model"
25
- )
26
-
27
- # Tokenize
28
- inputs = tokenizer(formatted_input, return_tensors='pt', padding=True, truncation=True)
29
-
30
- # Generate response
31
- with torch.no_grad():
32
- outputs = model.generate(
33
- **inputs,
34
- max_length=200,
35
- num_return_sequences=1,
36
- do_sample=True,
37
- top_k=10,
38
- top_p=0.8,
39
- temperature=0.2,
40
- no_repeat_ngram_size=3,
41
- )
42
-
43
- # Decode and extract response
44
- text = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
- response = text.split("model")[-1].strip()
46
-
47
- return response
48
 
49
- # Create Gradio interface
50
- iface = gr.Interface(
51
- fn=generate_response,
52
- inputs=[
53
- gr.Textbox(label="Enter your concern", lines=3),
54
- gr.Radio(["English", "Yoruba", "Igbo", "Hausa"], label="Select Language", value="English")
55
- ],
56
- outputs=gr.Textbox(label="Therapist's Response", lines=5),
57
- title="Multilingual Mental Health Therapist",
58
- description="Share your concerns and receive empathetic responses in English, Yoruba, Igbo, or Hausa.",
59
- examples=[
60
- ["I'm feeling very anxious about my upcoming exam", "English"],
61
- ["Mo wa depressed, Awon obi mi ni kin kuro ni le tori mi o lowo", "Yoruba"],
62
- ],
63
- theme=gr.themes.Soft()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  )
65
 
66
- # Launch the app
67
- iface.launch()
 
1
  import gradio as gr
 
 
2
 
 
 
 
 
3
 
4
+ model = AutoModelForCausalLM.from_pretrained("hemhemoh/Gemma-2-2b-it-wazobia-wellness-bot")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+
7
+ def respond(
8
+ message,
9
+ history: list[tuple[str, str]],
10
+ system_message,
11
+ max_tokens,
12
+ temperature,
13
+ top_p,):
14
+ # Combine the system message with the user's input
15
+ message_prompt = "You are a highly skilled and empathetic mental health therapist fluent in English, Yoruba, Igbo, and Hausa. Respond to each user's concerns in the language they use to ensure comfort and understanding." + "\n\n"
16
+
17
+ # Add conversation history
18
+ prompt = ""
19
+ for user_input, assistant_response in history:
20
+ if user_input:
21
+ prompt = f"User's complaint:\n {user_input}\n"
22
+ if assistant_response:
23
+ prompt += f"assistant: {assistant_response}\n"
24
+
25
+ # Add the latest user message
26
+ prompt += f"{message_prompt}\nUser:{message}\n"
27
+
28
+ # Tokenize and generate response
29
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
30
+ #.to(device)
31
+ outputs = model.generate(
32
+ **inputs,
33
+ max_length=512,
34
+ temperature=0.2,
35
+ top_p=0.5,
36
+ no_repeat_ngram_size=3,)
37
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
+ # if "assistant:" in response:
39
+ # response = response.split("assistant:")[-1].strip()
40
+
41
+ yield response
42
+
43
+
44
+ # Gradio ChatInterface with additional inputs
45
+ demo = gr.ChatInterface(
46
+ respond,
47
+ # additional_inputs=[
48
+ # # gr.Textbox(
49
+ # # value="Hi, How can we be of service to you, today?",
50
+ # # label="System message",
51
+ # # ),
52
+ # gr.Slider(minimum=1, maximum=512, value=512, step=1, label="Max new tokens"),
53
+ # gr.Slider(minimum=0.1, maximum=1.0, value=0.1, step=0.1, label="Temperature"),
54
+ # gr.Slider(
55
+ # minimum=0.1,
56
+ # maximum=1.0,
57
+ # value=0.6,
58
+ # step=0.05,
59
+ # label="Top-p (nucleus sampling)",
60
+ # ),
61
+ # ],
62
  )
63
 
64
+ if __name__ == "__main__":
65
+ demo.launch()