# -*- 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 = "AIzaSyDw4vZTtNZrHN32Ekv5sS-FTvgp3KkqQhk" 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.