Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from groq import Groq
|
3 |
+
|
4 |
+
# Groq API Configuration
|
5 |
+
client = Groq(api_key = "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")
|
6 |
+
|
7 |
+
# Function to handle responses from Groq
|
8 |
+
def chatbot(message, history, system_message, max_tokens, temperature, top_p):
|
9 |
+
messages = [{"role": "system", "content": system_message}]
|
10 |
+
|
11 |
+
# Add conversation history
|
12 |
+
for val in history:
|
13 |
+
if val[0]:
|
14 |
+
messages.append({"role": "user", "content": val[0]})
|
15 |
+
if val[1]:
|
16 |
+
messages.append({"role": "assistant", "content": val[1]})
|
17 |
+
|
18 |
+
# Append current user message
|
19 |
+
messages.append({"role": "user", "content": message})
|
20 |
+
|
21 |
+
# Call the Groq API to generate the response
|
22 |
+
response = ""
|
23 |
+
for chunk in client.chat.completions.create(
|
24 |
+
model="llama-3.3-70b-versatile",
|
25 |
+
messages=messages,
|
26 |
+
temperature=temperature,
|
27 |
+
max_tokens=max_tokens,
|
28 |
+
top_p=top_p,
|
29 |
+
stream=True,
|
30 |
+
):
|
31 |
+
response += chunk.choices[0].delta.content or ""
|
32 |
+
yield response
|
33 |
+
|
34 |
+
# System message to inform the AI about its purpose
|
35 |
+
system_message = "You are Jude Mental Health AI Chat. You are an experienced expert in mental health, psychology, and psychiatry. You have a wide range of knowledge, skills, and tools for dealing with mental health issues, and you are also a good educator on mental health issues. Provide concise, informed answers to various prompts on mental health"
|
36 |
+
|
37 |
+
# Define the Gradio interface
|
38 |
+
def create_interface():
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
# Display the main header
|
41 |
+
gr.Markdown("# Welcome to Jude Mental Health AI Chat\n # What can we help you with?")
|
42 |
+
|
43 |
+
# Chatbot output area (AI's response)
|
44 |
+
chatbot_output = gr.Chatbot(label="Chat with Jude Mental Health AI")
|
45 |
+
|
46 |
+
# Textbox for user input (prompt)
|
47 |
+
with gr.Row():
|
48 |
+
user_input = gr.Textbox(placeholder="Enter your question here...", label="Ask a question", interactive=True, elem_id="user_input", lines=3)
|
49 |
+
submit_button = gr.Button("Send", elem_id="submit_button") # Shorter button
|
50 |
+
|
51 |
+
# Mental Health Topics Buttons
|
52 |
+
with gr.Row():
|
53 |
+
topic_button_anxiety = gr.Button("Anxiety")
|
54 |
+
topic_button_depression = gr.Button("Depression")
|
55 |
+
topic_button_sleep = gr.Button("Impact of Sleep")
|
56 |
+
topic_button_drugs = gr.Button("Drugs & Mental Health")
|
57 |
+
topic_button_more = gr.Button("More")
|
58 |
+
|
59 |
+
# Gradio sliders to control the parameters for response generation
|
60 |
+
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
|
61 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
62 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
63 |
+
|
64 |
+
# Action when submit button is clicked
|
65 |
+
def on_submit(message, history, max_tokens, temperature, top_p):
|
66 |
+
history.append((message, ""))
|
67 |
+
for response in chatbot(message, history, system_message, max_tokens, temperature, top_p):
|
68 |
+
history[-1] = (message, response) # Update the history with the new response
|
69 |
+
return history, gr.update(value="") # Clear the input textbox after submission
|
70 |
+
|
71 |
+
submit_button.click(on_submit, inputs=[user_input, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])
|
72 |
+
|
73 |
+
# Action when topic button is clicked
|
74 |
+
def on_topic_click(topic, history, max_tokens, temperature, top_p):
|
75 |
+
topic_message = f"Tell me about {topic}."
|
76 |
+
history.append((topic_message, ""))
|
77 |
+
for response in chatbot(topic_message, history, system_message, max_tokens, temperature, top_p):
|
78 |
+
history[-1] = (topic_message, response)
|
79 |
+
return history, gr.update(value="") # Clear the input textbox after topic selection
|
80 |
+
|
81 |
+
topic_button_anxiety.click(on_topic_click, inputs=[topic_button_anxiety, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])
|
82 |
+
topic_button_depression.click(on_topic_click, inputs=[topic_button_depression, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])
|
83 |
+
topic_button_sleep.click(on_topic_click, inputs=[topic_button_sleep, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])
|
84 |
+
topic_button_drugs.click(on_topic_click, inputs=[topic_button_drugs, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])
|
85 |
+
topic_button_more.click(on_topic_click, inputs=[topic_button_more, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])
|
86 |
+
|
87 |
+
return demo
|
88 |
+
|
89 |
+
# Launch the interface
|
90 |
+
demo = create_interface()
|
91 |
+
demo.launch()
|