File size: 5,013 Bytes
039c847
15bc571
 
 
 
039c847
 
 
 
15bc571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import gradio as gr
from groq import Groq

# Groq API Configuration

keyment = os.getenv ("Mental")

client = Groq(api_key = keyment)

# Function to handle responses from Groq
def chatbot(message, history, system_message, max_tokens, temperature, top_p):
    messages = [{"role": "system", "content": system_message}]
    
    # Add conversation history
    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})
    
    # Append current user message
    messages.append({"role": "user", "content": message})
    
    # Call the Groq API to generate the response
    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 to inform the AI about its purpose
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"

# Define the Gradio interface
def create_interface():
    with gr.Blocks() as demo:
        # Display the main header
        gr.Markdown("# Welcome to Jude Mental Health AI Chat\n # What can we help you with?")
        
        # Chatbot output area (AI's response)
        chatbot_output = gr.Chatbot(label="Chat with Jude Mental Health AI")
        
        # Textbox for user input (prompt)
        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")  # Shorter button
        
        # Mental Health Topics Buttons
        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")
        
        # Gradio sliders to control the parameters for response generation
        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)")

        # Action when submit button is clicked
        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)  # Update the history with the new response
            return history, gr.update(value="")  # Clear the input textbox after submission
        
        submit_button.click(on_submit, inputs=[user_input, gr.State([]), max_tokens_slider, temperature_slider, top_p_slider], outputs=[chatbot_output, user_input])

        # Action when topic button is clicked
        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="")  # Clear the input textbox after topic selection
        
        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

# Launch the interface
demo = create_interface()
demo.launch()