Spaces:
Sleeping
Sleeping
| import os | |
| import time | |
| import gradio as gr | |
| from groq import Groq | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| api_key = "gsk_..." | |
| client = Groq(api_key=api_key) | |
| APP_NAME = "MindEase: Your AI Therapist πΏ" | |
| SYSTEM_INSTRUCTION = "You are a professional, empathetic, and supportive Mental Health Therapist named 'MindEase AI'." | |
| def user_message(user_msg_dict, history): | |
| if not user_msg_dict or not user_msg_dict.get("text"): | |
| return history | |
| text = user_msg_dict["text"] | |
| history.append({"role": "user", "content": text}) | |
| return history | |
| def bot_response(history, model, temperature, max_tokens): | |
| time.sleep(1.5) | |
| messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] | |
| for msg in history: | |
| if msg.get("content"): | |
| messages.append({"role": msg["role"], "content": msg["content"]}) | |
| try: | |
| completion = client.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| temperature=temperature, | |
| max_tokens=max_tokens, | |
| stream=False | |
| ) | |
| response_content = completion.choices[0].message.content | |
| history.append({"role": "assistant", "content": response_content}) | |
| except Exception as e: | |
| history.append({"role": "assistant", "content": f"Error: {str(e)}"}) | |
| return history | |
| def generate_static_advice(mood): | |
| if not mood: return "Please enter your mood first." | |
| time.sleep(1) | |
| try: | |
| c = client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| messages=[{"role": "user", "content": f"Give me one short mental health tip for feeling {mood}"}], | |
| stream=False | |
| ) | |
| return c.choices[0].message.content | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown(f"# {APP_NAME}") | |
| gr.Markdown("Supportive conversations for a better mental well-being.") | |
| chatbot = gr.Chatbot(label="Chatbot", height=500) | |
| gr.Markdown("### π‘ Suggestions") | |
| with gr.Row(): | |
| btn_ex1 = gr.Button("I feel overwhelmed π", variant="secondary") | |
| btn_ex2 = gr.Button("Suggest a sleep routine π", variant="secondary") | |
| btn_ex3 = gr.Button("I need motivation πͺ", variant="secondary") | |
| msg_input = gr.MultimodalTextbox( | |
| interactive=True, | |
| file_count="none", | |
| placeholder="Type your message here...", | |
| show_label=False, | |
| container=False, | |
| scale=1, | |
| autofocus=True | |
| ) | |
| with gr.Accordion("βοΈ Additional Inputs & Tools", open=False): | |
| with gr.Row(): | |
| model_dd = gr.Dropdown(choices=["llama-3.3-70b-versatile", "llama3-8b-8192"], value="llama-3.3-70b-versatile", label="Model") | |
| temp_sl = gr.Slider(minimum=0, maximum=2, value=0.7, step=0.1, label="Temperature") | |
| tokens_sl = gr.Slider(minimum=256, maximum=4096, value=2048, label="Max Tokens") | |
| gr.Markdown("---") | |
| gr.Markdown("### β‘ Quick Static Advice") | |
| with gr.Row(): | |
| static_input = gr.Textbox(placeholder="Enter mood...", show_label=False, scale=4) | |
| static_btn = gr.Button("Get Tip", scale=1) | |
| static_output = gr.Markdown("Advice will appear here...") | |
| msg_input.submit( | |
| fn=user_message, | |
| inputs=[msg_input, chatbot], | |
| outputs=[chatbot], | |
| queue=False | |
| ).then( | |
| fn=bot_response, | |
| inputs=[chatbot, model_dd, temp_sl, tokens_sl], | |
| outputs=chatbot | |
| ) | |
| def suggestion_click(text, history): | |
| history.append({"role": "user", "content": text}) | |
| return history | |
| btn_ex1.click( | |
| fn=suggestion_click, | |
| inputs=[gr.State("I feel overwhelmed"), chatbot], | |
| outputs=chatbot, | |
| queue=False | |
| ).then( | |
| fn=bot_response, | |
| inputs=[chatbot, model_dd, temp_sl, tokens_sl], | |
| outputs=chatbot | |
| ) | |
| btn_ex2.click( | |
| fn=suggestion_click, | |
| inputs=[gr.State("Can you suggest a sleep routine?"), chatbot], | |
| outputs=chatbot, | |
| queue=False | |
| ).then( | |
| fn=bot_response, | |
| inputs=[chatbot, model_dd, temp_sl, tokens_sl], | |
| outputs=chatbot | |
| ) | |
| btn_ex3.click( | |
| fn=suggestion_click, | |
| inputs=[gr.State("I need motivation"), chatbot], | |
| outputs=chatbot, | |
| queue=False | |
| ).then( | |
| fn=bot_response, | |
| inputs=[chatbot, model_dd, temp_sl, tokens_sl], | |
| outputs=chatbot | |
| ) | |
| static_btn.click(fn=generate_static_advice, inputs=static_input, outputs=static_output) | |
| if __name__ == "__main__": | |
| demo.launch(theme=gr.themes.Soft(), css=".suggestion-btn {font-size: 14px;}") |