tiya1012 commited on
Commit
912cb88
·
verified ·
1 Parent(s): b3b2c98

Upload thai_maternal_health.py

Browse files
Files changed (1) hide show
  1. thai_maternal_health.py +78 -0
thai_maternal_health.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """thai maternal health.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1sk8TdnCQ8qOKCkzn_hPmCj8uU6xaV3HM
8
+ """
9
+
10
+ # Simple Chatbot Gradio + Google Gemini API 🚀
11
+
12
+ #@title Mostrar Imagen
13
+ from IPython.display import Image
14
+ url = 'https://github.com/AleNunezArroyo/Medium/blob/main/img/BannerM4.png?raw=true'
15
+ Image(url=url, width=800)
16
+
17
+ # Install the Python SDK
18
+ !pip install -q -U google-generativeai
19
+
20
+ # The library and API Key information is added
21
+ import google.generativeai as genai
22
+ import time # Import the time module
23
+
24
+ # Replace with your actual Google API key
25
+ GOOGLE_API_KEY = "AIzaSyDw4vZTtNZrHN32Ekv5sS-FTvgp3KkqQhk"
26
+ genai.configure(api_key=GOOGLE_API_KEY)
27
+
28
+ # Model configuration
29
+ model = genai.GenerativeModel('gemini-pro')
30
+
31
+ # Chat conversations
32
+ chat = model.start_chat(history=[])
33
+
34
+ # Prompt tuning for maternal health
35
+ def maternal_health_prompt(language_code):
36
+ return f"""You are a knowledgeable and compassionate maternal health expert.
37
+ Provide accurate, clear, and culturally sensitive information about maternal health,
38
+ pregnancy, childbirth, and postpartum care. Respond in the language corresponding to the language code: {language_code}.
39
+ Keep responses concise, friendly, and focused on evidence-based medical information.
40
+ If you're unsure about anything, recommend consulting a healthcare provider."""
41
+
42
+ # Transform Gradio history to Gemini format
43
+ def transform_history(history):
44
+ new_history = []
45
+ for chat in history:
46
+ new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
47
+ new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
48
+ return new_history
49
+
50
+ # Response function
51
+ def response(message, history, language_code):
52
+ global chat
53
+ # Update the chat history in the Gemini format
54
+ chat.history = transform_history(history)
55
+
56
+ # Use the prompt tuning for the maternal health expert context
57
+ prompt = maternal_health_prompt(language_code) + f"\n\nUser: {message}"
58
+
59
+ # Send the message and get the response
60
+ response = chat.send_message(prompt)
61
+ response.resolve()
62
+
63
+ # Each character of the answer is displayed
64
+ for i in range(len(response.text)):
65
+ time.sleep(0.05)
66
+ yield response.text[: i + 1]
67
+
68
+ # Gradio interface
69
+ import gradio as gr
70
+
71
+ # Set up Gradio interface
72
+ language_code = "th-TH" # Change this to the desired language code
73
+ gr.ChatInterface(response,
74
+ title='Maternal Health Chatbot',
75
+ textbox=gr.Textbox(placeholder="Ask your question about maternal health")).launch(debug=True)
76
+
77
+ # Save chat history if necessary
78
+ # chat.history can be accessed to save conversation history if required.