datdevsteve commited on
Commit
7b86135
·
verified ·
1 Parent(s): 7498b9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -62
app.py CHANGED
@@ -1,100 +1,105 @@
1
- import gradio as gr
2
- import tempfile
3
- import uuid
4
  import os
5
- import base64
6
- from nivra_agent import nivra_chat, nivra_vision
 
7
 
8
- # ===============================
9
- # Chat endpoint
10
- # ===============================
11
- def chat_fn(message, history):
12
- history = history or []
13
- history.append({"role": "user", "content": message})
14
- response = nivra_chat(message, history)
15
- history.append({"role": "assistant", "content": response})
16
- return history, ""
17
 
 
 
 
18
 
19
  UPLOAD_DIR = "/tmp/uploads"
20
  os.makedirs(UPLOAD_DIR, exist_ok=True)
21
 
22
- def upload_image(image_base64: str):
23
- img_bytes = base64.b64decode(image_base64)
24
- filename = f"{uuid.uuid4()}.png"
25
- path = os.path.join(UPLOAD_DIR, filename)
26
-
27
- with open(path, "wb") as f:
28
- f.write(img_bytes)
29
 
30
- # Expose via HF Space static file serving
31
- return f"{os.environ['SPACE_HOST']}/file={path}"
 
32
 
33
- # ===============================
34
- # 🆕 Vision endpoint (CRITICAL FIX)
35
- # ===============================
36
- import uuid
37
- import os
38
-
39
- UPLOAD_DIR = "/tmp/uploads"
40
- os.makedirs(UPLOAD_DIR, exist_ok=True)
41
 
42
- def vision_fn(image, hint_text):
43
- import uuid, os
44
 
45
- UPLOAD_DIR = "/tmp/uploads"
46
- os.makedirs(UPLOAD_DIR, exist_ok=True)
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  filename = f"{uuid.uuid4()}.png"
49
  path = os.path.join(UPLOAD_DIR, filename)
50
-
51
  image.save(path)
52
 
53
- image_url = f"https://datdevsteve-nivra-ai-agent.hf.space/file={path}"
54
- return nivra_vision(image_url, hint_text)
 
 
55
 
 
 
56
 
57
- # ===============================
58
  # UI + API
59
- # ===============================
 
60
  with gr.Blocks(title="🩺 Nivra AI Agent") as demo:
61
- gr.Markdown("# 🩺 Nivra AI Agent")
 
 
 
62
 
63
  # -------------------------------
64
  # Chat UI
65
  # -------------------------------
66
  chatbot = gr.Chatbot(show_label=False)
67
- txt = gr.Textbox(placeholder="Describe your symptoms...")
 
 
68
  send = gr.Button("Send")
69
 
70
  send.click(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
71
  txt.submit(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
72
- gr.Button(visible=False).click(
73
- upload_image,
74
- inputs=[gr.Textbox()],
75
- outputs=[gr.Textbox()],
76
- api_name="upload_image",
77
- )
78
  # -------------------------------
79
- # Hidden API endpoints (ISOLATED)
80
  # -------------------------------
81
-
82
- with gr.Row(visible=False):
83
- vision_image = gr.Textbox()
84
- vision_hint = gr.Textbox()
85
- vision_out = gr.Textbox()
86
-
87
- gr.Button(visible=False).click(
88
- vision_fn,
89
- inputs=[
90
- gr.Image(type="pil", label="Medical Image"),
91
- gr.Textbox(label="Optional description"),
92
  ],
93
  outputs=[gr.Textbox()],
94
  api_name="vision_fn",
95
- )
 
 
 
 
96
 
97
- # 🔴 DO NOT enable queue for Flutter SSE
98
  demo.launch(
99
  server_name="0.0.0.0",
100
  server_port=7860,
 
 
 
 
1
  import os
2
+ import uuid
3
+ import gradio as gr
4
+ from PIL import Image
5
 
6
+ from nivra_agent import nivra_chat, nivra_vision
 
 
 
 
 
 
 
 
7
 
8
+ # ==================================================
9
+ # Constants
10
+ # ==================================================
11
 
12
  UPLOAD_DIR = "/tmp/uploads"
13
  os.makedirs(UPLOAD_DIR, exist_ok=True)
14
 
15
+ SPACE_HOST = os.environ.get(
16
+ "SPACE_HOST",
17
+ "https://datdevsteve-nivra-ai-agent.hf.space"
18
+ )
 
 
 
19
 
20
+ # ==================================================
21
+ # CHAT ENDPOINT
22
+ # ==================================================
23
 
24
+ def chat_fn(message, history):
25
+ history = history or []
26
+ history.append({"role": "user", "content": message})
 
 
 
 
 
27
 
28
+ response = nivra_chat(message, history)
 
29
 
30
+ history.append({"role": "assistant", "content": response})
31
+ return history, ""
32
 
33
+ # ==================================================
34
+ # 🆕 VISION ENDPOINT (FILE-BASED, NO BASE64)
35
+ # ==================================================
36
+
37
+ def vision_fn(image: Image.Image, hint_text: str):
38
+ """
39
+ image: PIL Image (uploaded by Gradio)
40
+ hint_text: optional user context
41
+ """
42
+
43
+ if image is None:
44
+ return (
45
+ "[PRIMARY DIAGNOSIS] Inconclusive [/PRIMARY DIAGNOSIS]\n"
46
+ "[DIAGNOSIS DESCRIPTION] No image provided. [/DIAGNOSIS DESCRIPTION]\n"
47
+ "[FIRST AID] Please upload a valid image. [/FIRST AID]\n"
48
+ "[EMERGENCY CONSULTATION REQUIRED] No [/EMERGENCY CONSULTATION REQUIRED]"
49
+ )
50
+
51
+ # Save image to HF-served temp directory
52
  filename = f"{uuid.uuid4()}.png"
53
  path = os.path.join(UPLOAD_DIR, filename)
 
54
  image.save(path)
55
 
56
+ # HF Spaces automatically serve /file= paths
57
+ image_url = f"{SPACE_HOST}/file={path}"
58
+
59
+ print(f"📥 Vision image saved: {image_url}")
60
 
61
+ # Pass URL to agent (CRITICAL FIX)
62
+ return nivra_vision(image_url, hint_text)
63
 
64
+ # ==================================================
65
  # UI + API
66
+ # ==================================================
67
+
68
  with gr.Blocks(title="🩺 Nivra AI Agent") as demo:
69
+ gr.Markdown(
70
+ "# 🩺 Nivra AI Agent\n"
71
+ "_AI-powered healthcare assistant for preliminary guidance._"
72
+ )
73
 
74
  # -------------------------------
75
  # Chat UI
76
  # -------------------------------
77
  chatbot = gr.Chatbot(show_label=False)
78
+ txt = gr.Textbox(
79
+ placeholder="Describe your symptoms (e.g. fever, headache, rash)..."
80
+ )
81
  send = gr.Button("Send")
82
 
83
  send.click(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
84
  txt.submit(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
85
+
 
 
 
 
 
86
  # -------------------------------
87
+ # 🔒 Hidden Vision API (Flutter)
88
  # -------------------------------
89
+ gr.Button(visible=False).click(
90
+ vision_fn,
91
+ inputs=[
92
+ gr.Image(type="pil"),
93
+ gr.Textbox()
 
 
 
 
 
 
94
  ],
95
  outputs=[gr.Textbox()],
96
  api_name="vision_fn",
97
+ )
98
+
99
+ # ==================================================
100
+ # Launch (HF-safe)
101
+ # ==================================================
102
 
 
103
  demo.launch(
104
  server_name="0.0.0.0",
105
  server_port=7860,