Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| import gradio as gr | |
| import datetime | |
| #from google.colab import userdata | |
| # Retrieve API key from environment variable | |
| groq_api_key = os.getenv("GROQ_API_KEY") | |
| if not groq_api_key: | |
| raise ValueError("GROQ_API_KEY environment variable is not set.") | |
| # Function to process user input using Groq's LLM | |
| def process_user_input(user_input, history): | |
| groq_api_url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {groq_api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| messages = [{"role": "system", "content": "You are a compassionate mental health support assistant."}] | |
| for i in range(0, len(history), 2): | |
| messages.append({"role": "user", "content": history[i]}) | |
| if i + 1 < len(history): | |
| messages.append({"role": "assistant", "content": history[i + 1]}) | |
| messages.append({"role": "user", "content": user_input}) | |
| data = { | |
| "model": "llama-3.3-70b-versatile", | |
| "messages": messages, | |
| "temperature": 0.7 | |
| } | |
| response = requests.post(groq_api_url, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| print(f"Error {response.status_code}: {response.text}") | |
| return "I'm sorry, I couldn't process your request at the moment." | |
| # Function to log conversation | |
| def log_conversation(user_input, bot_response): | |
| timestamp = datetime.datetime.now().isoformat() | |
| with open("conversation_log.txt", "a") as log_file: | |
| log_file.write(f"{timestamp}\nUser: {user_input}\nBot: {bot_response}\n\n") | |
| # Gradio Interface | |
| with gr.Blocks(css=""" | |
| #chatbox { border-radius: 20px; background-color: #f5f8fa; padding: 15px; } | |
| .gr-button { border-radius: 10px !important; padding: 10px 20px !important; } | |
| .gr-textbox textarea { font-size: 16px !important; padding: 10px !important; border-radius: 12px !important; } | |
| .gr-checkbox label { font-size: 14px; } | |
| """) as demo: | |
| gr.Markdown(""" | |
| <div style='text-align: center;'> | |
| <h1>🧠 AI-Powered Mental Health Support Chatbot</h1> | |
| <p style='color: gray;'>I'm here to listen, support, and provide guidance. All responses are private and confidential.</p> | |
| </div> | |
| """) | |
| with gr.Row(): | |
| emotion = gr.Dropdown( | |
| choices=[ | |
| "I am feeling happy", "I am feeling sad", "I am feeling angry", | |
| "I am excited", "I am anxious", "I am tired", "I am overwhelmed", "Typing.." | |
| ], | |
| label="Select your current emotion", | |
| value=None, | |
| allow_custom_value=False | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| label="You can type here", | |
| placeholder="💬 Share what's on your mind...", | |
| lines=2, | |
| max_lines=4, | |
| elem_id="user_input" | |
| ) | |
| send_btn = gr.Button("Send", scale=2) | |
| chatbot = gr.Chatbot(elem_id="chatbox", height=300) | |
| consent = gr.Checkbox(label="I consent to have this conversation logged for improvement purposes.", value=False) | |
| clear = gr.Button("🧹 Clear Chat") | |
| def respond(emotion, user_message, chat_history, consent): | |
| full_message = "" | |
| if emotion: | |
| full_message += emotion + ". " | |
| if user_message: | |
| full_message += user_message | |
| if not full_message.strip(): | |
| return "", chat_history # nothing to send | |
| bot_message = process_user_input(full_message, [item for sublist in chat_history for item in sublist]) | |
| chat_history.append((full_message, bot_message)) | |
| if consent: | |
| log_conversation(full_message, bot_message) | |
| return "", chat_history | |
| send_btn.click(respond, [emotion, msg, chatbot, consent], [msg, chatbot]) | |
| msg.submit(respond, [emotion, msg, chatbot, consent], [msg, chatbot]) | |
| clear.click(lambda: [], None, chatbot, queue=False) | |
| demo.launch() | |