File size: 2,186 Bytes
f2664b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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="AIzaSyBzEmxM4lu-x6eZ5Qyzi7UAwjkSaBwIWVc")

# 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)