Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
|
5 |
+
processor = AutoProcessor.from_pretrained('microsoft/git-base')
|
6 |
+
model = AutoModelForCausalLM.from_pretrained('./')
|
7 |
+
|
8 |
+
def predict(image):
|
9 |
+
try:
|
10 |
+
inputs = processor(images=image, return_tensors="pt")
|
11 |
+
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
14 |
+
model.to(device)
|
15 |
+
|
16 |
+
outputs = model.generate(**inputs)
|
17 |
+
|
18 |
+
caption = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
19 |
+
|
20 |
+
return caption
|
21 |
+
|
22 |
+
except Exception as e:
|
23 |
+
print("Error during prediction:", str(e))
|
24 |
+
return "Error: " + str(e)
|
25 |
+
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
image = gr.Image(type="pil")
|
28 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
29 |
+
output = gr.Textbox(label="Generated Caption")
|
30 |
+
|
31 |
+
inputs = [image]
|
32 |
+
outputs = [output]
|
33 |
+
|
34 |
+
predict_btn.click(predict, inputs=inputs, outputs=outputs)
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
demo.launch()
|