|
import os |
|
import gradio as gr |
|
from groq import Groq |
|
|
|
|
|
|
|
keyment = os.getenv ("Mental") |
|
|
|
client = Groq(api_key = keyment) |
|
|
|
|
|
def chatbot(message, history, system_message, max_tokens, temperature, top_p): |
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
|
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
response = "" |
|
for chunk in client.chat.completions.create( |
|
model="llama-3.3-70b-versatile", |
|
messages=messages, |
|
temperature=temperature, |
|
max_tokens=max_tokens, |
|
top_p=top_p, |
|
stream=True, |
|
): |
|
response += chunk.choices[0].delta.content or "" |
|
yield response |
|
|
|
|
|
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" |
|
|
|
|
|
def create_interface(): |
|
with gr.Blocks() as demo: |
|
|
|
gr.Markdown("# Welcome to Jude Mental Health AI Chat\n # What can we help you with?") |
|
|
|
|
|
chatbot_output = gr.Chatbot(label="Chat with Jude Mental Health AI") |
|
|
|
|
|
with gr.Row(): |
|
user_input = gr.Textbox(placeholder="Enter your question here...", label="Ask a question", interactive=True, elem_id="user_input", lines=3) |
|
submit_button = gr.Button("Send", elem_id="submit_button") |
|
|
|
|
|
with gr.Row(): |
|
topic_button_anxiety = gr.Button("Anxiety") |
|
topic_button_depression = gr.Button("Depression") |
|
topic_button_sleep = gr.Button("Impact of Sleep") |
|
topic_button_drugs = gr.Button("Drugs & Mental Health") |
|
topic_button_more = gr.Button("More") |
|
|
|
|
|
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens") |
|
temperature_slider = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature") |
|
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)") |
|
|
|
|
|
def on_submit(message, history, max_tokens, temperature, top_p): |
|
history.append((message, "")) |
|
for response in chatbot(message, history, system_message, max_tokens, temperature, top_p): |
|
history[-1] = (message, response) |
|
return history, gr.update(value="") |
|
|
|
submit_button.click(on_submit, inputs=[user_input, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input]) |
|
|
|
|
|
def on_topic_click(topic, history, max_tokens, temperature, top_p): |
|
topic_message = f"Tell me about {topic}." |
|
history.append((topic_message, "")) |
|
for response in chatbot(topic_message, history, system_message, max_tokens, temperature, top_p): |
|
history[-1] = (topic_message, response) |
|
return history, gr.update(value="") |
|
|
|
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]) |
|
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]) |
|
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]) |
|
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]) |
|
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]) |
|
|
|
return demo |
|
|
|
|
|
demo = create_interface() |
|
demo.launch() |
|
|