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

Upload app.py

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