Spaces:
Runtime error
Runtime error
Commit
·
27df4ce
1
Parent(s):
8267da7
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,36 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
5 |
|
6 |
+
|
7 |
+
device='cpu'
|
8 |
+
|
9 |
+
processor = AutoProcessor.from_pretrained("microsoft/git-base")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/git-base").to(device)
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def predict(image,max_length=64,device='cpu'):
|
15 |
+
pixel_values = processor(images=image, return_tensors="pt").to(device).pixel_values
|
16 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=max_length)
|
17 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
18 |
+
|
19 |
+
return generated_caption
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
input = gr.inputs.Image(label="Upload your Image", type = 'jpg', optional=True)
|
24 |
+
output = gr.outputs.Textbox(type="auto",label="Captions")
|
25 |
+
|
26 |
+
|
27 |
+
title = "Image Captioning"
|
28 |
+
|
29 |
+
interface = gr.Interface(
|
30 |
+
fn=predict,
|
31 |
+
inputs = input,
|
32 |
+
theme="grass",
|
33 |
+
outputs=output,
|
34 |
+
title=title,
|
35 |
+
)
|
36 |
+
interface.launch(debug=True)
|