File size: 2,732 Bytes
912cb88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe759e3
912cb88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""thai maternal health.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1sk8TdnCQ8qOKCkzn_hPmCj8uU6xaV3HM
"""

# Simple Chatbot Gradio + Google Gemini API 🚀

#@title Mostrar Imagen
from IPython.display import Image
url = 'https://github.com/AleNunezArroyo/Medium/blob/main/img/BannerM4.png?raw=true'
Image(url=url, width=800)

# Install the Python SDK
!pip install -q -U google-generativeai

# The library and API Key information is added
import google.generativeai as genai
import time  # Import the time module

# Replace with your actual Google API key
GOOGLE_API_KEY = "AIzaSyAjpXjkDIuCOhWYu71v_EMq3oGW44TdD9k"
genai.configure(api_key=GOOGLE_API_KEY)

# 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
import gradio as gr

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

# Save chat history if necessary
# chat.history can be accessed to save conversation history if required.