tiya1012 commited on
Commit
f2664b0
·
verified ·
1 Parent(s): d7e15ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -58
app.py CHANGED
@@ -1,58 +1,58 @@
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)
 
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="AIzaSyBzEmxM4lu-x6eZ5Qyzi7UAwjkSaBwIWVc")
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)