import gradio as gr import google.generativeai as genai import os # Configure Gemini API API_KEY = os.getenv("GOOGLE_API_KEY") # Retrieved from Hugging Face Spaces Secrets genai.configure(api_key=API_KEY) model = genai.GenerativeModel("gemini-2.0-flash") chat = model.start_chat() # Send initial instruction to the model initial_message = ( "Hey, I'm using you as a therapist to help people who are feeling sad feel better. " "I want you to improve the user's mental health and make them feel good about themselves, " "sort of like a supportive friend. Keep your responses short, concise, and make them feel like they matter. " "Do not directly guide them to a suicide prevention hotline, as it will be mentioned on my website already. " "Just be there to listen. Start the conversation by saying 'Hi, how are you doing today?'" ) chat.send_message(initial_message) # Chat function for Gradio def chat_function(message, history): # Transform Gradio history to Gemini format if history: for msg in history: chat.send_message(msg["content"]) # Send user and assistant messages from history # Send user message and get response response = chat.send_message(message) return response.text # Create Gradio interface with gr.Blocks() as demo: gr.Markdown("# VoiceAI: Your Supportive Chatbot") gr.Markdown("I'm here to listen and help you feel better. You matter! If you're having suicidal thoughts or feelings please call: 988") chatbot = gr.ChatInterface( fn=chat_function, chatbot=gr.Chatbot(height=500, show_copy_button=True, type="messages"), title="Chat with VoiceAI", description="A supportive chatbot to lift your spirits. Type 'exit' to close the chat.", submit_btn="Send" ) # Launch the app if __name__ == "__main__": demo.launch(share=True)