Spaces:
Sleeping
Sleeping
Commit
·
5334465
1
Parent(s):
98864d9
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoProcessor,AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 5 |
+
def generate_caption(image,length):
|
| 6 |
+
encoded=processor(images=image, return_tensors="pt").to(device)
|
| 7 |
+
pixels=encoded['pixel_values'].to(device)
|
| 8 |
+
with torch.no_grad():
|
| 9 |
+
generated_ids=model.generate(pixel_values=pixels,max_length=length)
|
| 10 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 11 |
+
return generated_caption
|
| 12 |
+
demo=gr.Interface(
|
| 13 |
+
generate_caption,
|
| 14 |
+
[
|
| 15 |
+
gr.Image(type='pil',flagging_options=["blurry", "incorrect", "other"]),
|
| 16 |
+
gr.Slider(10,50,value=10)
|
| 17 |
+
],
|
| 18 |
+
'label',
|
| 19 |
+
theme=gr.themes.Soft(primary_hue='purple',secondary_hue=gr.themes.colors.gray)
|
| 20 |
+
)
|
| 21 |
+
demo.launch()
|