Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,16 @@
|
|
|
|
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 |
# -----------------------------------------------------------------------------
|
@@ -10,21 +18,20 @@ 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=
|
23 |
model = PaliGemmaForConditionalGeneration.from_pretrained(
|
24 |
model_id,
|
25 |
torch_dtype=dtype,
|
26 |
device_map=None,
|
27 |
-
use_auth_token=
|
28 |
).to(device).eval()
|
29 |
|
30 |
# build multimodal prompt
|
@@ -53,7 +60,6 @@ def run_inference_on_gpu(
|
|
53 |
# decode
|
54 |
return processor.decode(outputs[0].cpu(), skip_special_tokens=True)
|
55 |
|
56 |
-
|
57 |
# -----------------------------------------------------------------------------
|
58 |
# 2) Gradio UI
|
59 |
# -----------------------------------------------------------------------------
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
4 |
from PIL import Image
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# -----------------------------------------------------------------------------
|
8 |
+
# Load HF token from environment
|
9 |
+
# -----------------------------------------------------------------------------
|
10 |
+
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
11 |
+
if not HF_TOKEN:
|
12 |
+
raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable not set")
|
13 |
+
|
14 |
# -----------------------------------------------------------------------------
|
15 |
# 1) GPU inference function
|
16 |
# -----------------------------------------------------------------------------
|
|
|
18 |
model_id: str,
|
19 |
image: Image.Image,
|
20 |
prompt: str = "caption",
|
21 |
+
max_new_tokens: int = 100
|
|
|
22 |
) -> str:
|
23 |
# ensure CUDA is available
|
24 |
assert torch.cuda.is_available(), "CUDA not available—check your PyTorch installation!"
|
25 |
device = torch.device("cuda")
|
26 |
dtype = torch.float16
|
27 |
|
28 |
+
# load tokenizer + model onto GPU with explicit token
|
29 |
+
processor = AutoProcessor.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
30 |
model = PaliGemmaForConditionalGeneration.from_pretrained(
|
31 |
model_id,
|
32 |
torch_dtype=dtype,
|
33 |
device_map=None,
|
34 |
+
use_auth_token=HF_TOKEN
|
35 |
).to(device).eval()
|
36 |
|
37 |
# build multimodal prompt
|
|
|
60 |
# decode
|
61 |
return processor.decode(outputs[0].cpu(), skip_special_tokens=True)
|
62 |
|
|
|
63 |
# -----------------------------------------------------------------------------
|
64 |
# 2) Gradio UI
|
65 |
# -----------------------------------------------------------------------------
|