Upload app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,58 @@
|
|
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 |
-
# 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.
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
# Install the required library
|
4 |
+
subprocess.check_call(["pip", "install", "-q", "-U", "google-generativeai"])
|
5 |
+
|
6 |
+
import google.generativeai as genai
|
7 |
+
import time
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
# Replace with your actual Google API key
|
11 |
+
GOOGLE_API_KEY = "your_api_key_here"
|
12 |
+
genai.configure(api_key="AIzaSyDw4vZTtNZrHN32Ekv5sS-FTvgp3KkqQhk")
|
13 |
+
|
14 |
+
# Model configuration
|
15 |
+
model = genai.GenerativeModel('gemini-pro')
|
16 |
+
|
17 |
+
# Chat conversations
|
18 |
+
chat = model.start_chat(history=[])
|
19 |
+
|
20 |
+
# Prompt tuning for maternal health
|
21 |
+
def maternal_health_prompt(language_code):
|
22 |
+
return f"""You are a knowledgeable and compassionate maternal health expert.
|
23 |
+
Provide accurate, clear, and culturally sensitive information about maternal health,
|
24 |
+
pregnancy, childbirth, and postpartum care. Respond in the language corresponding to the language code: {language_code}.
|
25 |
+
Keep responses concise, friendly, and focused on evidence-based medical information.
|
26 |
+
If you're unsure about anything, recommend consulting a healthcare provider."""
|
27 |
+
|
28 |
+
# Transform Gradio history to Gemini format
|
29 |
+
def transform_history(history):
|
30 |
+
new_history = []
|
31 |
+
for chat in history:
|
32 |
+
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
33 |
+
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
34 |
+
return new_history
|
35 |
+
|
36 |
+
# Response function
|
37 |
+
def response(message, history, language_code):
|
38 |
+
global chat
|
39 |
+
# Update the chat history in the Gemini format
|
40 |
+
chat.history = transform_history(history)
|
41 |
+
|
42 |
+
# Use the prompt tuning for the maternal health expert context
|
43 |
+
prompt = maternal_health_prompt(language_code) + f"\n\nUser: {message}"
|
44 |
+
|
45 |
+
# Send the message and get the response
|
46 |
+
response = chat.send_message(prompt)
|
47 |
+
response.resolve()
|
48 |
+
|
49 |
+
# Each character of the answer is displayed
|
50 |
+
for i in range(len(response.text)):
|
51 |
+
time.sleep(0.05)
|
52 |
+
yield response.text[: i + 1]
|
53 |
+
|
54 |
+
# Gradio interface
|
55 |
+
language_code = "th-TH" # Change this to the desired language code
|
56 |
+
gr.ChatInterface(response,
|
57 |
+
title='Maternal Health Chatbot',
|
58 |
+
textbox=gr.Textbox(placeholder="Ask your question about maternal health")).launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|