ved1beta
commited on
Commit
·
44157d6
1
Parent(s):
5b195c1
main
Browse files- Readme.md +26 -0
- app.py +75 -64
- requirements.txt +6 -6
Readme.md
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# PaliGemma Image Captioning Gradio App
|
2 |
+
|
3 |
+
## Deployment Instructions
|
4 |
+
|
5 |
+
1. Create a new Hugging Face Space
|
6 |
+
2. Choose Python as the SDK
|
7 |
+
3. Select 16GB CPU hardware
|
8 |
+
4. Upload the following files:
|
9 |
+
- `app.py`
|
10 |
+
- `requirements.txt`
|
11 |
+
|
12 |
+
### HuggingFace Access Token
|
13 |
+
|
14 |
+
1. Go to HuggingFace settings
|
15 |
+
2. Create a new access token with "Read" permissions
|
16 |
+
3. Add the token as a secret named `HF_TOKEN` in your Space settings
|
17 |
+
|
18 |
+
### Features
|
19 |
+
- Multi-language image captioning
|
20 |
+
- Upload custom images
|
21 |
+
- Example images included
|
22 |
+
- Supports English, Spanish, French, German captions
|
23 |
+
|
24 |
+
## Model Details
|
25 |
+
- Model: google/paligemma-3b-mix-224
|
26 |
+
- Task: Multilingual Image Captioning
|
app.py
CHANGED
@@ -1,74 +1,85 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from transformers import AutoProcessor
|
5 |
-
from transformers import TextIteratorStreamer
|
6 |
-
from threading import Thread
|
7 |
import torch
|
8 |
-
import
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
trust_remote_code=True,
|
15 |
-
torch_dtype=torch.float32,
|
16 |
-
_attn_implementation="eager"
|
17 |
-
)
|
18 |
-
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
conversation = []
|
29 |
-
for user, assistant in history:
|
30 |
-
conversation.extend([
|
31 |
-
{"role": "user", "content": user},
|
32 |
-
{"role": "assistant", "content": assistant or ""}
|
33 |
-
])
|
34 |
-
|
35 |
-
conversation.append({"role": "user", "content": f"<|image_1|>\n{message['text']}"})
|
36 |
-
|
37 |
-
prompt = processor.tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
38 |
-
image = Image.open(image)
|
39 |
-
inputs = processor(prompt, image, return_tensors="pt")
|
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 |
-
gr.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
|
73 |
-
|
74 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
3 |
+
from PIL import Image
|
|
|
|
|
|
|
4 |
import torch
|
5 |
+
import requests
|
6 |
|
7 |
+
# Load the model and processor
|
8 |
+
model_id = "google/paligemma-3b-mix-224"
|
9 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, token=True).eval()
|
10 |
+
processor = AutoProcessor.from_pretrained(model_id, token=True)
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Supported languages and example prompts
|
13 |
+
LANGUAGES = {
|
14 |
+
"English": "caption en",
|
15 |
+
"Spanish": "caption es",
|
16 |
+
"French": "caption fr",
|
17 |
+
"German": "caption de"
|
18 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def generate_caption(image, language, max_tokens=100):
|
21 |
+
"""Generate image caption in specified language"""
|
22 |
+
if image is None:
|
23 |
+
return "Please upload an image."
|
24 |
+
|
25 |
+
prompt = LANGUAGES.get(language, "caption en")
|
26 |
+
|
27 |
+
# Preprocess inputs
|
28 |
+
model_inputs = processor(text=prompt, images=image, return_tensors="pt")
|
29 |
+
input_len = model_inputs["input_ids"].shape[-1]
|
30 |
+
|
31 |
+
# Generate caption
|
32 |
+
with torch.inference_mode():
|
33 |
+
generation = model.generate(**model_inputs, max_new_tokens=max_tokens, do_sample=False)
|
34 |
+
generation = generation[0][input_len:]
|
35 |
+
decoded = processor.decode(generation, skip_special_tokens=True)
|
36 |
+
|
37 |
+
return decoded
|
38 |
|
39 |
+
def load_example_image(url):
|
40 |
+
"""Load example image from URL"""
|
41 |
+
return Image.open(requests.get(url, stream=True).raw)
|
42 |
|
43 |
+
# Prepare example images
|
44 |
+
EXAMPLE_IMAGES = [
|
45 |
+
load_example_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg"),
|
46 |
+
load_example_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/food.jpg"),
|
47 |
+
load_example_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/city.jpg")
|
48 |
+
]
|
49 |
|
50 |
+
# Create Gradio Interface
|
51 |
+
with gr.Blocks() as demo:
|
52 |
+
gr.Markdown("# PaliGemma Image Captioning")
|
53 |
+
gr.Markdown("Upload an image and get a caption in your preferred language!")
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
with gr.Column():
|
57 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
58 |
+
language_dropdown = gr.Dropdown(
|
59 |
+
list(LANGUAGES.keys()),
|
60 |
+
value="English",
|
61 |
+
label="Caption Language"
|
62 |
+
)
|
63 |
+
submit_btn = gr.Button("Generate Caption")
|
64 |
+
|
65 |
+
with gr.Column():
|
66 |
+
output_text = gr.Textbox(label="Generated Caption")
|
67 |
+
|
68 |
+
# Connect components
|
69 |
+
submit_btn.click(
|
70 |
+
fn=generate_caption,
|
71 |
+
inputs=[input_image, language_dropdown],
|
72 |
+
outputs=output_text
|
73 |
+
)
|
74 |
+
|
75 |
+
# Add example images
|
76 |
+
gr.Examples(
|
77 |
+
examples=[[img, lang] for img in EXAMPLE_IMAGES for lang in LANGUAGES.keys()],
|
78 |
+
inputs=[input_image, language_dropdown],
|
79 |
+
fn=generate_caption,
|
80 |
+
outputs=output_text
|
81 |
)
|
82 |
|
83 |
+
# Launch the app
|
84 |
+
if __name__ == "__main__":
|
85 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
1 |
+
torch>=2.0.0
|
2 |
+
transformers>=4.37.0
|
3 |
+
gradio>=4.0.0
|
4 |
+
pillow>=10.0.0
|
5 |
+
huggingface_hub
|
6 |
+
requests
|