Spaces:
Running
on
Zero
Running
on
Zero
Add files
Browse files
README.md
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
title: image-captioning-with-git
|
4 |
emoji: ⚡
|
5 |
colorFrom: red
|
6 |
colorTo: purple
|
@@ -9,6 +8,7 @@ sdk_version: 3.50.2
|
|
9 |
python_version: 3.10.13
|
10 |
app_file: app.py
|
11 |
pinned: false
|
|
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Image Captioning with GIT
|
|
|
3 |
emoji: ⚡
|
4 |
colorFrom: red
|
5 |
colorTo: purple
|
|
|
8 |
python_version: 3.10.13
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
+
license: mit
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -1,9 +1,42 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
|
|
|
|
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
7 |
|
8 |
if __name__ == "__main__":
|
9 |
-
demo.queue().launch()
|
|
|
1 |
#!/usr/bin/env python
|
2 |
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
import gradio as gr
|
6 |
+
import PIL.Image
|
7 |
+
import spaces
|
8 |
+
import torch
|
9 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
10 |
+
|
11 |
+
DESCRIPTION = "# Image Captioning with GIT"
|
12 |
+
|
13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
model_id = "microsoft/git-large-coco"
|
16 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
|
18 |
+
|
19 |
+
|
20 |
+
@spaces.GPU
|
21 |
+
def run(image: PIL.Image.Image) -> str:
|
22 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
23 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values, num_beams=3, max_length=20, min_length=5)
|
24 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
25 |
+
return generated_caption
|
26 |
+
|
27 |
+
|
28 |
+
with gr.Blocks(css="style.css") as demo:
|
29 |
+
gr.Markdown(DESCRIPTION)
|
30 |
+
input_image = gr.Image(type="pil")
|
31 |
+
run_button = gr.Button("Caption")
|
32 |
+
output = gr.Textbox(label="Result")
|
33 |
|
34 |
+
run_button.click(
|
35 |
+
fn=run,
|
36 |
+
inputs=input_image,
|
37 |
+
outputs=output,
|
38 |
+
api_name="caption",
|
39 |
+
)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
+
demo.queue(max_size=20).launch()
|
requirements.txt
CHANGED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.50.2
|
2 |
+
Pillow==10.1.0
|
3 |
+
spaces==0.16.3
|
4 |
+
torch==2.0.0
|
5 |
+
torchvision==0.15.1
|
6 |
+
transformers==4.34.1
|
style.css
CHANGED
@@ -8,3 +8,9 @@ h1 {
|
|
8 |
background: #1565c0;
|
9 |
border-radius: 100vh;
|
10 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
background: #1565c0;
|
9 |
border-radius: 100vh;
|
10 |
}
|
11 |
+
|
12 |
+
.contain {
|
13 |
+
width: 730px;
|
14 |
+
margin: auto;
|
15 |
+
padding-top: 1.5rem;
|
16 |
+
}
|