Spaces:
Sleeping
Sleeping
Commit
·
9a3f3d2
1
Parent(s):
3e392cd
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
processor=AutoProcessor.from_pretrained("saravanamax/General_image_captioning")
|
| 6 |
+
model=AutoModelForCausalLM.from_pretrained("saravanamax/General_image_captioning").to(device)
|
| 7 |
+
def generate_caption(image):
|
| 8 |
+
encoded=processor(images=image, return_tensors="pt").to(device)
|
| 9 |
+
pixels=encoded['pixel_values'].to(device)
|
| 10 |
+
with torch.no_grad():
|
| 11 |
+
generated_ids=model.generate(pixel_values=pixels,max_length=10)
|
| 12 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 13 |
+
return generated_caption
|
| 14 |
+
demo=gr.Interface(
|
| 15 |
+
fn=generate_caption,
|
| 16 |
+
inputs=[
|
| 17 |
+
gr.Image(type='pil'),
|
| 18 |
+
],
|
| 19 |
+
outputs= 'label',
|
| 20 |
+
examples=['sample.jpg','sample1.jpg','sample2.jpg'],
|
| 21 |
+
theme=gr.themes.Soft(primary_hue='purple',secondary_hue=gr.themes.colors.gray)
|
| 22 |
+
)
|
| 23 |
+
demo.launch(show_error=True)
|