samwell Claude commited on
Commit
b6fb0da
·
1 Parent(s): 14acaec

Add Hugging Face Space deployment files

Browse files

- Add app.py with Gradio interface for HF Spaces
- Add requirements.txt with all dependencies
- Update README.md with HF Space configuration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

Files changed (3) hide show
  1. README.md +12 -0
  2. app.py +94 -0
  3. requirements.txt +296 -0
README.md CHANGED
@@ -1,3 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  <h1 align="center">
2
  🤖 MedRAX-2: Medical Reasoning Agent for Chest X-ray
3
  </h1>
 
1
+ ---
2
+ title: MedRAX2
3
+ emoji: 🏥
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 5.9.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
  <h1 align="center">
14
  🤖 MedRAX-2: Medical Reasoning Agent for Chest X-ray
15
  </h1>
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ MedRax2 Gradio Interface for Hugging Face Spaces
3
+ Simple standalone version for deployment
4
+ """
5
+ import os
6
+ os.environ["OMP_NUM_THREADS"] = "1"
7
+
8
+ import warnings
9
+ warnings.filterwarnings("ignore")
10
+
11
+ from huggingface_hub import login
12
+ hf_token = os.environ.get("HF_TOKEN")
13
+ if hf_token:
14
+ login(token=hf_token)
15
+ print("✓ Logged in to HuggingFace")
16
+
17
+ import gradio as gr
18
+ from dotenv import load_dotenv
19
+ import torch
20
+
21
+ load_dotenv()
22
+
23
+ from langgraph.checkpoint.memory import MemorySaver
24
+ from medrax.models import ModelFactory
25
+ from medrax.agent import Agent
26
+ from medrax.utils import load_prompts_from_file
27
+
28
+ os.makedirs("temp", exist_ok=True)
29
+
30
+ device = "cuda" if torch.cuda.is_available() else "cpu"
31
+ print(f"Using device: {device}")
32
+
33
+ tools = []
34
+
35
+ if device == "cuda":
36
+ try:
37
+ from medrax.tools import XRayPhraseGroundingTool
38
+ grounding_tool = XRayPhraseGroundingTool(
39
+ device=device,
40
+ temp_dir="temp",
41
+ load_in_4bit=True
42
+ )
43
+ tools.append(grounding_tool)
44
+ print("✓ Loaded grounding tool")
45
+ except Exception as e:
46
+ print(f"✗ Failed to load grounding tool: {e}")
47
+
48
+ checkpointer = MemorySaver()
49
+
50
+ llm = ModelFactory.create_model(
51
+ model_name="gemini-2.0-flash",
52
+ temperature=0.7,
53
+ max_tokens=5000
54
+ )
55
+
56
+ prompts = load_prompts_from_file("medrax/docs/system_prompts.txt")
57
+ prompt = prompts.get("MEDICAL_ASSISTANT", "You are a helpful medical imaging assistant.")
58
+
59
+ agent = Agent(
60
+ llm,
61
+ tools=tools,
62
+ system_prompt=prompt,
63
+ checkpointer=checkpointer,
64
+ )
65
+
66
+ print(f"Tools loaded: {len(tools)}")
67
+
68
+ def chat(message, history):
69
+ config = {"configurable": {"thread_id": "default"}}
70
+
71
+ # Handle multimodal input
72
+ if isinstance(message, dict):
73
+ text = message.get("text", "")
74
+ files = message.get("files", [])
75
+ if files:
76
+ file_info = f"[Image uploaded: {files[0]}]\n\n"
77
+ text = file_info + text
78
+ message = text
79
+
80
+ response = agent.workflow.invoke(
81
+ {"messages": [("user", message)]},
82
+ config=config
83
+ )
84
+ return response["messages"][-1].content
85
+
86
+ demo = gr.ChatInterface(
87
+ fn=chat,
88
+ title="MedRAX2 - Medical AI Assistant",
89
+ description=f"Device: {device} | Tools: {len(tools)} loaded",
90
+ multimodal=True
91
+ )
92
+
93
+ if __name__ == "__main__":
94
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==1.12.0
2
+ aiofiles==24.1.0
3
+ aiohappyeyeballs==2.6.1
4
+ aiohttp==3.13.2
5
+ aiohttp-retry==2.9.1
6
+ aiosignal==1.4.0
7
+ albucore==0.0.24
8
+ albumentations==2.0.8
9
+ annotated-doc==0.0.4
10
+ annotated-types==0.7.0
11
+ anthropic==0.75.0
12
+ antlr4-python3-runtime==4.9.3
13
+ anyio==4.12.0
14
+ appnope==0.1.4
15
+ argon2-cffi==25.1.0
16
+ argon2-cffi-bindings==25.1.0
17
+ arrow==1.4.0
18
+ asttokens==3.0.1
19
+ async-lru==2.0.5
20
+ attrs==25.4.0
21
+ babel==2.17.0
22
+ backoff==2.2.1
23
+ bcrypt==5.0.0
24
+ beautifulsoup4==4.14.3
25
+ bitsandbytes==0.49.0
26
+ bleach==6.3.0
27
+ brotli==1.2.0
28
+ build==1.3.0
29
+ cachetools==6.2.4
30
+ certifi==2025.11.12
31
+ cffi==2.0.0
32
+ charset-normalizer==3.4.4
33
+ chromadb==1.3.7
34
+ click==8.2.1
35
+ cohere==5.20.0
36
+ coloredlogs==15.0.1
37
+ comm==0.2.3
38
+ contourpy==1.3.3
39
+ cryptography==46.0.3
40
+ cycler==0.12.1
41
+ dataclasses-json==0.6.7
42
+ datasets==4.4.1
43
+ debugpy==1.8.19
44
+ decorator==5.2.1
45
+ defusedxml==0.7.1
46
+ diffusers==0.36.0
47
+ dill==0.4.0
48
+ distro==1.9.0
49
+ docstring_parser==0.17.0
50
+ duckduckgo_search==8.1.1
51
+ durationpy==0.10
52
+ einops==0.8.1
53
+ einops-exts==0.0.4
54
+ executing==2.2.1
55
+ fastapi==0.124.4
56
+ fastavro==1.12.1
57
+ fastjsonschema==2.21.2
58
+ ffmpy==1.0.0
59
+ filelock==3.20.1
60
+ filetype==1.2.0
61
+ flatbuffers==25.9.23
62
+ fonttools==4.61.1
63
+ fqdn==1.5.1
64
+ frozenlist==1.8.0
65
+ fsspec==2025.10.0
66
+ google-auth==2.45.0
67
+ google-genai==1.55.0
68
+ googleapis-common-protos==1.72.0
69
+ gradio==6.1.0
70
+ gradio_client==2.0.1
71
+ groovy==0.1.2
72
+ grpcio==1.76.0
73
+ h11==0.16.0
74
+ hf-xet==1.2.0
75
+ httpcore==1.0.9
76
+ httptools==0.7.1
77
+ httpx==0.28.1
78
+ httpx-sse==0.4.0
79
+ huggingface-hub==0.36.0
80
+ humanfriendly==10.0
81
+ hydra-core==1.3.2
82
+ idna==3.11
83
+ ImageIO==2.37.2
84
+ importlib_metadata==8.7.0
85
+ importlib_resources==6.5.2
86
+ iopath==0.1.10
87
+ ipykernel==7.1.0
88
+ ipython==9.8.0
89
+ ipython_pygments_lexers==1.1.1
90
+ ipywidgets==8.1.8
91
+ isoduration==20.11.0
92
+ jedi==0.19.2
93
+ Jinja2==3.1.6
94
+ jiter==0.12.0
95
+ joblib==1.5.3
96
+ json5==0.12.1
97
+ jsonpatch==1.33
98
+ jsonpointer==3.0.0
99
+ jsonschema==4.25.1
100
+ jsonschema-specifications==2025.9.1
101
+ jupyter==1.1.1
102
+ jupyter-console==6.6.3
103
+ jupyter-events==0.12.0
104
+ jupyter-lsp==2.3.0
105
+ jupyter_client==8.7.0
106
+ jupyter_core==5.9.1
107
+ jupyter_server==2.17.0
108
+ jupyter_server_terminals==0.5.3
109
+ jupyterlab==4.5.1
110
+ jupyterlab_pygments==0.3.0
111
+ jupyterlab_server==2.28.0
112
+ jupyterlab_widgets==3.0.16
113
+ kiwisolver==1.4.9
114
+ kubernetes==34.1.0
115
+ langchain==1.2.0
116
+ langchain-anthropic==1.3.0
117
+ langchain-chroma==1.1.0
118
+ langchain-classic==1.0.0
119
+ langchain-cohere==0.5.0
120
+ langchain-community==0.4.1
121
+ langchain-core==1.2.2
122
+ langchain-google-genai==4.1.1
123
+ langchain-openai==1.1.4
124
+ langchain-pinecone==0.2.13
125
+ langchain-text-splitters==1.1.0
126
+ langchain-xai==1.1.0
127
+ langgraph==1.0.5
128
+ langgraph-checkpoint==3.0.1
129
+ langgraph-prebuilt==1.0.5
130
+ langgraph-sdk==0.3.0
131
+ langsmith==0.5.0
132
+ lark==1.3.1
133
+ latex2mathml==3.78.1
134
+ lazy_loader==0.4
135
+ lxml==6.0.2
136
+ markdown-it-py==4.0.0
137
+ markdown2==2.5.4
138
+ MarkupSafe==3.0.3
139
+ marshmallow==3.26.1
140
+ matplotlib==3.10.8
141
+ matplotlib-inline==0.2.1
142
+ mdurl==0.1.2
143
+ -e git+https://github.com/bowang-lab/MedRAX2.git@95ddfff0fe4bf8aa271c38302447d9b85181a897#egg=medrax
144
+ mistune==3.1.4
145
+ mmh3==5.2.0
146
+ mpmath==1.3.0
147
+ msgpack==1.1.2
148
+ multidict==6.7.0
149
+ multiprocess==0.70.18
150
+ mypy_extensions==1.1.0
151
+ nbclient==0.10.2
152
+ nbconvert==7.16.6
153
+ nbformat==5.10.4
154
+ nest-asyncio==1.6.0
155
+ networkx==3.6.1
156
+ notebook==7.5.1
157
+ notebook_shim==0.2.4
158
+ numpy==2.2.6
159
+ oauthlib==3.3.1
160
+ omegaconf==2.3.0
161
+ onnxruntime==1.23.2
162
+ openai==2.13.0
163
+ opencv-python==4.12.0.88
164
+ opencv-python-headless==4.12.0.88
165
+ opentelemetry-api==1.39.1
166
+ opentelemetry-exporter-otlp-proto-common==1.39.1
167
+ opentelemetry-exporter-otlp-proto-grpc==1.39.1
168
+ opentelemetry-proto==1.39.1
169
+ opentelemetry-sdk==1.39.1
170
+ opentelemetry-semantic-conventions==0.60b1
171
+ orjson==3.11.5
172
+ ormsgpack==1.12.1
173
+ overrides==7.7.0
174
+ packaging==24.2
175
+ pandas==2.3.3
176
+ pandocfilters==1.5.1
177
+ parso==0.8.5
178
+ pdfminer.six==20251107
179
+ pdfplumber==0.11.8
180
+ peft==0.18.0
181
+ pexpect==4.9.0
182
+ pillow==12.0.0
183
+ pinecone==7.3.0
184
+ pinecone-client==6.0.0
185
+ pinecone-plugin-assistant==1.8.0
186
+ pinecone-plugin-interface==0.0.7
187
+ platformdirs==4.5.1
188
+ portalocker==3.2.0
189
+ posthog==5.4.0
190
+ primp==0.15.0
191
+ prometheus_client==0.23.1
192
+ prompt_toolkit==3.0.52
193
+ propcache==0.4.1
194
+ protobuf==6.33.2
195
+ psutil==7.1.3
196
+ ptyprocess==0.7.0
197
+ pure_eval==0.2.3
198
+ pyarrow==22.0.0
199
+ pyasn1==0.6.1
200
+ pyasn1_modules==0.4.2
201
+ pybase64==1.4.3
202
+ pycparser==2.23
203
+ pydantic==2.12.4
204
+ pydantic-settings==2.12.0
205
+ pydantic_core==2.41.5
206
+ pydicom==3.0.1
207
+ pydub==0.25.1
208
+ Pygments==2.19.2
209
+ pylibjpeg==2.1.0
210
+ pyngrok==7.5.0
211
+ pyparsing==3.2.5
212
+ PyPDF2==3.0.1
213
+ pypdfium2==5.2.0
214
+ PyPika==0.48.9
215
+ pyproject_hooks==1.2.0
216
+ python-dateutil==2.9.0.post0
217
+ python-dotenv==1.2.1
218
+ python-json-logger==4.0.0
219
+ python-multipart==0.0.20
220
+ pytz==2025.2
221
+ PyYAML==6.0.3
222
+ pyzmq==27.1.0
223
+ ray==2.52.1
224
+ referencing==0.37.0
225
+ regex==2025.11.3
226
+ requests==2.32.5
227
+ requests-oauthlib==2.0.0
228
+ requests-toolbelt==1.0.0
229
+ rfc3339-validator==0.1.4
230
+ rfc3986-validator==0.1.1
231
+ rfc3987-syntax==1.1.0
232
+ rich==14.2.0
233
+ rpds-py==0.30.0
234
+ rsa==4.9.1
235
+ safehttpx==0.1.7
236
+ safetensors==0.7.0
237
+ scikit-image==0.25.2
238
+ scikit-learn==1.8.0
239
+ scipy==1.16.3
240
+ seaborn==0.13.2
241
+ semantic-version==2.10.0
242
+ Send2Trash==1.8.3
243
+ sentencepiece==0.2.1
244
+ setuptools==80.9.0
245
+ shellingham==1.5.4
246
+ shortuuid==1.0.13
247
+ simsimd==6.5.8
248
+ six==1.17.0
249
+ sniffio==1.3.1
250
+ soupsieve==2.8
251
+ SQLAlchemy==2.0.45
252
+ stack-data==0.6.3
253
+ starlette==0.50.0
254
+ stringzilla==4.5.1
255
+ svgwrite==1.4.3
256
+ sympy==1.14.0
257
+ tenacity==9.1.2
258
+ terminado==0.18.1
259
+ threadpoolctl==3.6.0
260
+ tifffile==2025.12.12
261
+ tiktoken==0.12.0
262
+ timm==0.5.4
263
+ tinycss2==1.4.0
264
+ tokenizers==0.20.3
265
+ tomlkit==0.13.3
266
+ torch==2.9.1
267
+ torchvision==0.24.1
268
+ torchxrayvision==1.4.0
269
+ tornado==6.5.4
270
+ tqdm==4.67.1
271
+ traitlets==5.14.3
272
+ transformers @ git+https://github.com/huggingface/transformers.git@88d960937c81a32bfb63356a2e8ecf7999619681
273
+ typer==0.20.0
274
+ types-PyYAML==6.0.12.20250915
275
+ types-requests==2.32.4.20250913
276
+ typing-inspect==0.9.0
277
+ typing-inspection==0.4.2
278
+ typing_extensions==4.15.0
279
+ tzdata==2025.3
280
+ uri-template==1.3.0
281
+ urllib3==2.3.0
282
+ uuid_utils==0.12.0
283
+ uvicorn==0.38.0
284
+ uvloop==0.22.1
285
+ watchfiles==1.1.1
286
+ wavedrom==2.0.3.post3
287
+ wcwidth==0.2.14
288
+ webcolors==25.10.0
289
+ webencodings==0.5.1
290
+ websocket-client==1.9.0
291
+ websockets==15.0.1
292
+ widgetsnbextension==4.0.15
293
+ xxhash==3.6.0
294
+ yarl==1.22.0
295
+ zipp==3.23.0
296
+ zstandard==0.25.0