|
import subprocess |
|
|
|
|
|
subprocess.check_call(["pip", "install", "-q", "-U", "google-generativeai"]) |
|
|
|
import google.generativeai as genai |
|
import time |
|
import gradio as gr |
|
|
|
|
|
GOOGLE_API_KEY = "your_api_key_here" |
|
genai.configure(api_key="AIzaSyAjpXjkDIuCOhWYu71v_EMq3oGW44TdD9k") |
|
|
|
|
|
model = genai.GenerativeModel('gemini-pro') |
|
|
|
|
|
chat = model.start_chat(history=[]) |
|
|
|
|
|
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.""" |
|
|
|
|
|
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 |
|
|
|
|
|
def response(message, history, language_code): |
|
global chat |
|
|
|
chat.history = transform_history(history) |
|
|
|
|
|
prompt = maternal_health_prompt(language_code) + f"\n\nUser: {message}" |
|
|
|
|
|
response = chat.send_message(prompt) |
|
response.resolve() |
|
|
|
|
|
for i in range(len(response.text)): |
|
time.sleep(0.05) |
|
yield response.text[: i + 1] |
|
|
|
|
|
language_code = "th-TH" |
|
gr.ChatInterface(response, |
|
title='Maternal Health Chatbot', |
|
textbox=gr.Textbox(placeholder="Ask your question about maternal health")).launch(debug=True) |
|
|