Anupam007 commited on
Commit
024f032
·
verified ·
1 Parent(s): 418c9a6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ import gradio as gr
3
+ import os
4
+ from PIL import Image
5
+ import io
6
+ import tempfile
7
+
8
+ # --- 1. Configure API Key ---
9
+ GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
10
+ if not GOOGLE_API_KEY:
11
+ raise ValueError("API key not found. Please set it in Hugging Face Secrets.")
12
+ genai.configure(api_key=GOOGLE_API_KEY)
13
+
14
+ # --- 2. System Prompt (Same as before) ---
15
+ SYSTEM_PROMPT = """
16
+ [Your entire SYSTEM_PROMPT content goes here exactly as you have it]
17
+ """
18
+
19
+ # --- 3. Initialize the Gemini Model ---
20
+ model = genai.GenerativeModel(
21
+ model_name='gemini-1.5-flash-latest',
22
+ system_instruction=SYSTEM_PROMPT,
23
+ safety_settings={
24
+ 'HARM_CATEGORY_HARASSMENT': 'BLOCK_MEDIUM_AND_ABOVE',
25
+ 'HARM_CATEGORY_HATE_SPEECH': 'BLOCK_MEDIUM_AND_ABOVE',
26
+ 'HARM_CATEGORY_SEXUALLY_EXPLICIT': 'BLOCK_MEDIUM_AND_ABOVE',
27
+ 'HARM_CATEGORY_DANGEROUS_CONTENT': 'BLOCK_MEDIUM_AND_ABOVE',
28
+ }
29
+ )
30
+
31
+ # --- 4. Chat Function (Same as before) ---
32
+ def dia_buddy_chat(message_text, history, image_input):
33
+ [Your entire dia_buddy_chat function goes here exactly as you have it]
34
+
35
+ # --- 5. Create Gradio Interface (Modified for Hugging Face) ---
36
+ with gr.Blocks(theme="gradio/soft") as iface:
37
+ gr.Markdown(
38
+ """
39
+ # DiaBuddy - T1D Support Assistant 🩺
40
+ [Your markdown content goes here]
41
+ """
42
+ )
43
+ chatbot = gr.Chatbot([], elem_id="chatbot", height=600)
44
+ with gr.Row():
45
+ image_box = gr.Image(type="pil", label="Upload Food Image (Optional)")
46
+ with gr.Row():
47
+ text_box = gr.Textbox(
48
+ label="Type your message or question here:",
49
+ placeholder="Ask about T1D...",
50
+ container=False,
51
+ scale=7
52
+ )
53
+ submit_btn = gr.Button("Send", variant="primary", scale=1)
54
+
55
+ def handle_submit(message, history, image):
56
+ bot_response = dia_buddy_chat(message, history, image)
57
+ return "", history + [[message, bot_response]], None
58
+
59
+ text_box.submit(
60
+ handle_submit,
61
+ inputs=[text_box, chatbot, image_box],
62
+ outputs=[text_box, chatbot, image_box]
63
+ )
64
+ submit_btn.click(
65
+ handle_submit,
66
+ inputs=[text_box, chatbot, image_box],
67
+ outputs=[text_box, chatbot, image_box]
68
+ )
69
+
70
+ gr.Examples(
71
+ examples=[
72
+ ["What are the common symptoms of Type 1 Diabetes?", None],
73
+ ["Can you explain carb counting for T1D?", None],
74
+ [("My BG is 280 mg/dL, I ate 35g carbs. My ICR is 15, ISF is 50, and "
75
+ "target BG is 100 mg/dL. Can you show me an illustrative calculation?"), None],
76
+ ["My TDD is 30 units. How can I estimate my ICR using the rule of 400?", None]
77
+ ],
78
+ inputs=[text_box, image_box]
79
+ )
80
+
81
+ iface.launch()