Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,97 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
gr.Slider(
|
53 |
-
minimum=
|
54 |
-
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
+
# -----------------------------------------------------------------------------
|
7 |
+
# 1) GPU inference function
|
8 |
+
# -----------------------------------------------------------------------------
|
9 |
+
def run_inference_on_gpu(
|
10 |
+
model_id: str,
|
11 |
+
image: Image.Image,
|
12 |
+
prompt: str = "caption",
|
13 |
+
max_new_tokens: int = 100,
|
14 |
+
use_auth_token: bool = True
|
15 |
+
) -> str:
|
16 |
+
# ensure CUDA is available
|
17 |
+
assert torch.cuda.is_available(), "CUDA not available—check your PyTorch installation!"
|
18 |
+
device = torch.device("cuda")
|
19 |
+
dtype = torch.float16
|
20 |
|
21 |
+
# load tokenizer + model onto GPU
|
22 |
+
processor = AutoProcessor.from_pretrained(model_id, use_auth_token=use_auth_token)
|
23 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(
|
24 |
+
model_id,
|
25 |
+
torch_dtype=dtype,
|
26 |
+
device_map=None,
|
27 |
+
use_auth_token=use_auth_token
|
28 |
+
).to(device).eval()
|
29 |
|
30 |
+
# build multimodal prompt
|
31 |
+
image_tokens = "<image>"
|
32 |
+
multimodal_prompt = f"{image_tokens} {prompt}"
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# prepare inputs
|
35 |
+
inputs = processor(
|
36 |
+
text=multimodal_prompt,
|
37 |
+
images=[image],
|
38 |
+
padding="longest",
|
39 |
+
return_tensors="pt",
|
40 |
+
do_convert_rgb=True,
|
41 |
+
)
|
42 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
43 |
|
44 |
+
# generate
|
45 |
+
with torch.no_grad():
|
46 |
+
outputs = model.generate(
|
47 |
+
**inputs,
|
48 |
+
max_new_tokens=max_new_tokens,
|
49 |
+
num_beams=3,
|
50 |
+
do_sample=False,
|
51 |
+
)
|
52 |
|
53 |
+
# decode
|
54 |
+
return processor.decode(outputs[0].cpu(), skip_special_tokens=True)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# -----------------------------------------------------------------------------
|
58 |
+
# 2) Gradio UI
|
59 |
+
# -----------------------------------------------------------------------------
|
60 |
+
MODEL_ID = "mychen76/paligemma-3b-mix-448-med_30k-ct-brain"
|
61 |
|
62 |
+
def caption_fn(image, prompt, max_tokens):
|
63 |
+
"""
|
64 |
+
Gradio callback: takes a PIL image, a text prompt, and
|
65 |
+
max tokens → returns the generated caption.
|
66 |
+
"""
|
67 |
+
return run_inference_on_gpu(
|
68 |
+
model_id=MODEL_ID,
|
69 |
+
image=image,
|
70 |
+
prompt=prompt,
|
71 |
+
max_new_tokens=max_tokens,
|
72 |
+
)
|
73 |
|
74 |
+
demo = gr.Interface(
|
75 |
+
fn=caption_fn,
|
76 |
+
inputs=[
|
77 |
+
gr.Image(type="pil", label="Upload CT Scan"),
|
78 |
+
gr.Textbox(
|
79 |
+
value="What do you see in this CT scan?",
|
80 |
+
label="Prompt"
|
81 |
+
),
|
|
|
82 |
gr.Slider(
|
83 |
+
minimum=10, maximum=300, step=10, value=100,
|
84 |
+
label="Max New Tokens"
|
|
|
|
|
|
|
85 |
),
|
86 |
],
|
87 |
+
outputs=gr.Textbox(label="Model Caption"),
|
88 |
+
title="PaliGemma CT-Scan Captioning",
|
89 |
+
description=(
|
90 |
+
"Upload a brain CT scan (or any image), write a short prompt, "
|
91 |
+
"and let the PaliGemma model describe what it sees."
|
92 |
+
),
|
93 |
+
allow_flagging="never",
|
94 |
)
|
95 |
|
|
|
96 |
if __name__ == "__main__":
|
97 |
+
demo.launch(share=False) # set share=True if you need a public link
|