rehaan commited on
Commit
efdbede
·
1 Parent(s): b041032

Refactor vision client initialization and update Gradio launch configuration

Browse files
Files changed (3) hide show
  1. .codex +0 -0
  2. agent/image_symptom_tool.py +31 -5
  3. app.py +11 -6
.codex ADDED
File without changes
agent/image_symptom_tool.py CHANGED
@@ -1,10 +1,34 @@
1
- from langchain_core.tools import tool
2
- from gradio_client import Client
3
  import json
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Instantiate client once (efficient & stable)
6
- VISION_SPACE_URL = "https://datdevsteve-nivra-vision-diagnosis-v2.hf.space/"
7
- vision_client = Client(VISION_SPACE_URL)
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  @tool
@@ -14,6 +38,8 @@ def analyze_symptom_image(img_url: str) -> str:
14
  Returns structured JSON string.
15
  """
16
  try:
 
 
17
  result = vision_client.predict(
18
  image_upload=None,
19
  image_url=img_url,
 
 
 
1
  import json
2
+ import os
3
+ from threading import Lock
4
+
5
+ from gradio_client import Client
6
+ from langchain_core.tools import tool
7
+
8
+ VISION_SPACE_URL = os.getenv(
9
+ "VISION_SPACE_URL",
10
+ "https://datdevsteve-nivra-vision-diagnosis-v2.hf.space/",
11
+ )
12
+
13
+ _vision_client = None
14
+ _vision_client_lock = Lock()
15
 
16
+
17
+ def get_vision_client() -> Client:
18
+ """
19
+ Lazily initializes the remote Gradio client so import-time network
20
+ failures do not prevent the app from starting.
21
+ """
22
+ global _vision_client
23
+
24
+ if _vision_client is not None:
25
+ return _vision_client
26
+
27
+ with _vision_client_lock:
28
+ if _vision_client is None:
29
+ _vision_client = Client(VISION_SPACE_URL)
30
+
31
+ return _vision_client
32
 
33
 
34
  @tool
 
38
  Returns structured JSON string.
39
  """
40
  try:
41
+ vision_client = get_vision_client()
42
+
43
  result = vision_client.predict(
44
  image_upload=None,
45
  image_url=img_url,
app.py CHANGED
@@ -93,9 +93,14 @@ with gr.Blocks(title="🩺 Nivra AI Agent") as demo:
93
  # Launch (HF-safe)
94
  # ==================================================
95
 
96
- demo.launch(
97
- server_name="0.0.0.0",
98
- server_port=7860,
99
- ssr_mode=False,
100
- show_error=True
101
- )
 
 
 
 
 
 
93
  # Launch (HF-safe)
94
  # ==================================================
95
 
96
+ launch_kwargs = {
97
+ "server_name": "0.0.0.0",
98
+ "ssr_mode": False,
99
+ "show_error": True,
100
+ }
101
+
102
+ configured_port = os.environ.get("GRADIO_SERVER_PORT") or os.environ.get("PORT")
103
+ if configured_port:
104
+ launch_kwargs["server_port"] = int(configured_port)
105
+
106
+ demo.launch(**launch_kwargs)