tiya1012's picture
Update app.py
c05f70c verified
import subprocess
# Install the required library
subprocess.check_call(["pip", "install", "-q", "-U", "google-generativeai"])
import google.generativeai as genai
import time
import gradio as gr
# Replace with your actual Google API key
GOOGLE_API_KEY = "your_api_key_here"
genai.configure(api_key="AIzaSyAjpXjkDIuCOhWYu71v_EMq3oGW44TdD9k")
# Model configuration
model = genai.GenerativeModel('gemini-pro')
# Chat conversations
chat = model.start_chat(history=[])
# Prompt tuning for maternal health
def maternal_health_prompt(language_code):
return f"""You are a knowledgeable and compassionate maternal health expert.
Provide accurate, clear, and culturally sensitive information about maternal health,
pregnancy, childbirth, and postpartum care. Respond in the language corresponding to the language code: {language_code}.
Keep responses concise, friendly, and focused on evidence-based medical information.
If you're unsure about anything, recommend consulting a healthcare provider."""
# Transform Gradio history to Gemini format
def transform_history(history):
new_history = []
for chat in history:
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
return new_history
# Response function
def response(message, history, language_code):
global chat
# Update the chat history in the Gemini format
chat.history = transform_history(history)
# Use the prompt tuning for the maternal health expert context
prompt = maternal_health_prompt(language_code) + f"\n\nUser: {message}"
# Send the message and get the response
response = chat.send_message(prompt)
response.resolve()
# Each character of the answer is displayed
for i in range(len(response.text)):
time.sleep(0.05)
yield response.text[: i + 1]
# Gradio interface
language_code = "th-TH" # Change this to the desired language code
gr.ChatInterface(response,
title='Maternal Health Chatbot',
textbox=gr.Textbox(placeholder="Ask your question about maternal health")).launch(debug=True)