Spaces:
Runtime error
Runtime error
Update app.py
Browse filesThis version fixes the deprecated token argument and the InferenceClient call.
app.py
CHANGED
@@ -1,223 +1,216 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
-
|
5 |
-
- Otherwise
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
import
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
)
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
title
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
submit.click(on_submit, inputs=[prompt_input, max_tokens, temperature, top_p, num_beams, stop_token], outputs=output)
|
219 |
-
|
220 |
-
gr.Markdown("**Disclaimer:** This demo is for evaluation and research. It is not a medical device.")
|
221 |
-
|
222 |
-
if __name__ == "__main__":
|
223 |
-
demo.launch()
|
|
|
1 |
+
"""
|
2 |
+
Gradio app that:
|
3 |
+
- Uses a local model if torch is installed,
|
4 |
+
- Otherwise tries Hugging Face InferenceClient,
|
5 |
+
- Otherwise falls back to legacy InferenceApi with task="text-generation".
|
6 |
+
Make sure HF_TOKEN is set in Space secrets if your model is private.
|
7 |
+
"""
|
8 |
+
|
9 |
+
import os
|
10 |
+
from typing import Optional
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
MODEL_ID = "marvinisjarvis/radio_model"
|
14 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
15 |
+
|
16 |
+
# Flags & clients
|
17 |
+
LOCAL_AVAILABLE = False
|
18 |
+
INFERENCE_CLIENT_AVAILABLE = False
|
19 |
+
INFERENCE_API_AVAILABLE = False
|
20 |
+
|
21 |
+
# Attempt local loading (torch + transformers)
|
22 |
+
try:
|
23 |
+
import torch
|
24 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
25 |
+
|
26 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
27 |
+
|
28 |
+
def try_load_local():
|
29 |
+
print("Attempting to load local model...")
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
31 |
+
MODEL_ID, trust_remote_code=True, use_fast=True, token=HF_TOKEN
|
32 |
+
)
|
33 |
+
kwargs = {"trust_remote_code": True, "token": HF_TOKEN, "low_cpu_mem_usage": True}
|
34 |
+
if device == "cuda":
|
35 |
+
kwargs.update({"device_map": "auto", "torch_dtype": torch.float16})
|
36 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, **kwargs)
|
37 |
+
return tokenizer, model
|
38 |
+
|
39 |
+
try:
|
40 |
+
tokenizer, model = try_load_local()
|
41 |
+
LOCAL_AVAILABLE = True
|
42 |
+
print("Local model loaded.")
|
43 |
+
except Exception as e:
|
44 |
+
print("Local model load failed:", e)
|
45 |
+
LOCAL_AVAILABLE = False
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
print("Torch not available or failed to import:", e)
|
49 |
+
LOCAL_AVAILABLE = False
|
50 |
+
|
51 |
+
|
52 |
+
# Attempt to use InferenceClient (preferred)
|
53 |
+
if not LOCAL_AVAILABLE:
|
54 |
+
try:
|
55 |
+
from huggingface_hub import InferenceClient
|
56 |
+
client = InferenceClient(token=HF_TOKEN)
|
57 |
+
INFERENCE_CLIENT_AVAILABLE = True
|
58 |
+
print("InferenceClient available - will use remote text-generation via InferenceClient.")
|
59 |
+
except Exception as e:
|
60 |
+
print("InferenceClient not available:", e)
|
61 |
+
INFERENCE_CLIENT_AVAILABLE = False
|
62 |
+
|
63 |
+
# Fallback to legacy InferenceApi with explicit task
|
64 |
+
inference_api = None
|
65 |
+
if (not LOCAL_AVAILABLE) and (not INFERENCE_CLIENT_AVAILABLE):
|
66 |
+
try:
|
67 |
+
from huggingface_hub import InferenceApi
|
68 |
+
# Explicitly specify task to avoid "Task not specified" errors
|
69 |
+
inference_api = InferenceApi(repo_id=MODEL_ID, token=HF_TOKEN, task="text-generation")
|
70 |
+
INFERENCE_API_AVAILABLE = True
|
71 |
+
print("Using legacy InferenceApi with task='text-generation'.")
|
72 |
+
except Exception as e:
|
73 |
+
print("Hugging Face InferenceApi not available or failed:", e)
|
74 |
+
INFERENCE_API_AVAILABLE = False
|
75 |
+
|
76 |
+
|
77 |
+
# Generation wrapper that handles all three paths
|
78 |
+
def generate_answer(
|
79 |
+
prompt: str,
|
80 |
+
max_new_tokens: int = 256,
|
81 |
+
temperature: float = 0.7,
|
82 |
+
top_p: float = 0.9,
|
83 |
+
num_beams: int = 1,
|
84 |
+
stop_token: Optional[str] = None,
|
85 |
+
) -> str:
|
86 |
+
if not prompt or prompt.strip() == "":
|
87 |
+
return "Please enter a prompt."
|
88 |
+
|
89 |
+
# Local path
|
90 |
+
if LOCAL_AVAILABLE:
|
91 |
+
try:
|
92 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
93 |
+
device0 = next(model.parameters()).device
|
94 |
+
input_ids = inputs["input_ids"].to(device0)
|
95 |
+
attention_mask = inputs.get("attention_mask")
|
96 |
+
if attention_mask is not None:
|
97 |
+
attention_mask = attention_mask.to(device0)
|
98 |
+
|
99 |
+
gen_kwargs = dict(
|
100 |
+
input_ids=input_ids,
|
101 |
+
attention_mask=attention_mask,
|
102 |
+
max_new_tokens=int(max_new_tokens),
|
103 |
+
temperature=float(temperature),
|
104 |
+
top_p=float(top_p),
|
105 |
+
num_beams=int(num_beams),
|
106 |
+
eos_token_id=getattr(tokenizer, "eos_token_id", None),
|
107 |
+
pad_token_id=getattr(tokenizer, "pad_token_id", None),
|
108 |
+
do_sample=(float(temperature) > 0) and (int(num_beams) == 1),
|
109 |
+
)
|
110 |
+
outputs = model.generate(**gen_kwargs)
|
111 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
112 |
+
result = decoded[len(prompt) :].strip() if decoded.startswith(prompt) else decoded.strip()
|
113 |
+
if stop_token:
|
114 |
+
idx = result.find(stop_token)
|
115 |
+
if idx != -1:
|
116 |
+
result = result[:idx].strip()
|
117 |
+
return result
|
118 |
+
except Exception as e:
|
119 |
+
print("Local generation failed, falling back to remote. Error:", e)
|
120 |
+
|
121 |
+
# InferenceClient path (preferred remote)
|
122 |
+
if INFERENCE_CLIENT_AVAILABLE:
|
123 |
+
try:
|
124 |
+
# The InferenceClient text_generation method takes kwargs for parameters
|
125 |
+
response = client.text_generation(
|
126 |
+
model=MODEL_ID,
|
127 |
+
prompt=prompt,
|
128 |
+
max_new_tokens=int(max_new_tokens),
|
129 |
+
temperature=float(temperature),
|
130 |
+
top_p=float(top_p),
|
131 |
+
num_beams=int(num_beams),
|
132 |
+
)
|
133 |
+
# The response is the generated text string
|
134 |
+
out = response
|
135 |
+
if out.startswith(prompt):
|
136 |
+
out = out[len(prompt) :].strip()
|
137 |
+
if stop_token:
|
138 |
+
idx = out.find(stop_token)
|
139 |
+
if idx != -1:
|
140 |
+
out = out[:idx].strip()
|
141 |
+
return out.strip()
|
142 |
+
except Exception as e:
|
143 |
+
print("InferenceClient call failed, will try legacy InferenceApi. Error:", e)
|
144 |
+
|
145 |
+
# Legacy InferenceApi fallback (explicit task)
|
146 |
+
if INFERENCE_API_AVAILABLE and inference_api is not None:
|
147 |
+
try:
|
148 |
+
params = {"max_new_tokens": int(max_new_tokens), "temperature": float(temperature), "top_p": float(top_p)}
|
149 |
+
res = inference_api(prompt, params=params)
|
150 |
+
# normalize response
|
151 |
+
if isinstance(res, str):
|
152 |
+
out = res
|
153 |
+
elif isinstance(res, dict) and "generated_text" in res:
|
154 |
+
out = res["generated_text"]
|
155 |
+
elif isinstance(res, list) and res and isinstance(res[0], dict) and "generated_text" in res[0]:
|
156 |
+
out = res[0]["generated_text"]
|
157 |
+
else:
|
158 |
+
out = str(res)
|
159 |
+
if out.startswith(prompt):
|
160 |
+
out = out[len(prompt) :].strip()
|
161 |
+
if stop_token:
|
162 |
+
idx = out.find(stop_token)
|
163 |
+
if idx != -1:
|
164 |
+
out = out[:idx].strip()
|
165 |
+
return out.strip()
|
166 |
+
except Exception as e:
|
167 |
+
print("Legacy InferenceApi failed:", e)
|
168 |
+
return f"Remote inference failed: {e}"
|
169 |
+
|
170 |
+
return ("No inference path available. Install torch for local inference or ensure HF_TOKEN is set and huggingface_hub
|
171 |
+
supports InferenceClient/InferenceApi.")
|
172 |
+
|
173 |
+
|
174 |
+
# --- Gradio UI ---
|
175 |
+
title = "RadioModel — Radiology Q&A (Mistral 7B fine-tuned)"
|
176 |
+
description = """
|
177 |
+
Demo for marvinisjarvis/radio_model.
|
178 |
+
Tries local inference first; otherwise uses Hugging Face remote inference.
|
179 |
+
If your model is private, add HF_TOKEN in Space secrets. Not for clinical use.
|
180 |
+
"""
|
181 |
+
|
182 |
+
with gr.Blocks(title=title) as demo:
|
183 |
+
gr.Markdown(f"## {title}")
|
184 |
+
gr.Markdown(description)
|
185 |
+
|
186 |
+
with gr.Row():
|
187 |
+
with gr.Column(scale=3):
|
188 |
+
prompt_input = gr.Textbox(label="Enter your radiology question", lines=6)
|
189 |
+
submit = gr.Button("Generate Answer")
|
190 |
+
examples = gr.Examples(
|
191 |
+
examples=[
|
192 |
+
"What does an X-ray of pneumonia typically show?",
|
193 |
+
"How can you differentiate a benign lung nodule from a malignant one on CT?",
|
194 |
+
"What are common signs of bone fracture on X-rays?",
|
195 |
+
"Which imaging modality is best for detecting small brain tumors?"
|
196 |
+
],
|
197 |
+
inputs=prompt_input
|
198 |
+
)
|
199 |
+
with gr.Column(scale=2):
|
200 |
+
max_tokens = gr.Slider(32, 1024, value=256, step=32, label="Max New Tokens")
|
201 |
+
temperature = gr.Slider(0.0, 1.5, value=0.7, step=0.05, label="Temperature")
|
202 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.01, label="Top-p")
|
203 |
+
num_beams = gr.Slider(1, 5, value=1, step=1, label="Num Beams")
|
204 |
+
stop_token = gr.Textbox(label="Optional stop token", placeholder="e.g., ### or <END>", lines=1)
|
205 |
+
|
206 |
+
output = gr.Textbox(label="Model output", lines=14)
|
207 |
+
|
208 |
+
def on_submit(prompt, max_new_tokens, temperature, top_p, num_beams, stop_token):
|
209 |
+
return generate_answer(prompt, max_new_tokens, temperature, top_p, int(num_beams), stop_token)
|
210 |
+
|
211 |
+
submit.click(on_submit, inputs=[prompt_input, max_tokens, temperature, top_p, num_beams, stop_token], outputs=output)
|
212 |
+
|
213 |
+
gr.Markdown("Disclaimer: This demo is for evaluation and research. It is not a medical device.")
|
214 |
+
|
215 |
+
if __name__ == "__main__":
|
216 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|