Spaces:
Runtime error
Runtime error
Commit
·
364461b
1
Parent(s):
44717e7
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import requests
|
| 5 |
-
import io
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
CAPTION_MODELS = {
|
|
@@ -14,24 +11,19 @@ CAPTION_MODELS = {
|
|
| 14 |
|
| 15 |
# Simple caption creation
|
| 16 |
def caption_image(model_choice, image_input):
|
| 17 |
-
if isinstance(image_input, str): # Hopefully a URL
|
| 18 |
-
image_path = image_input
|
| 19 |
-
else: # Upload a file
|
| 20 |
-
image = Image.open(io.BytesIO(image_input))
|
| 21 |
-
image.save('temp_image_file.jpg')
|
| 22 |
-
image_path = 'temp_image_file.jpg'
|
| 23 |
-
|
| 24 |
captioner = pipeline(task="image-to-text",
|
| 25 |
model=CAPTION_MODELS[model_choice],
|
| 26 |
max_new_tokens=30,
|
| 27 |
device_map="cpu", use_fast=True
|
| 28 |
)
|
| 29 |
-
caption = captioner(
|
| 30 |
return str(caption).strip()
|
| 31 |
|
| 32 |
-
def launch(model_choice,
|
| 33 |
-
return caption_image(model_choice,
|
| 34 |
|
| 35 |
model_dropdown = gr.Dropdown(choices=list(CAPTION_MODELS.keys()), label='Model Choice')
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
iface.launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
CAPTION_MODELS = {
|
|
|
|
| 11 |
|
| 12 |
# Simple caption creation
|
| 13 |
def caption_image(model_choice, image_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
captioner = pipeline(task="image-to-text",
|
| 15 |
model=CAPTION_MODELS[model_choice],
|
| 16 |
max_new_tokens=30,
|
| 17 |
device_map="cpu", use_fast=True
|
| 18 |
)
|
| 19 |
+
caption = captioner(image_input)[0]['generated_text']
|
| 20 |
return str(caption).strip()
|
| 21 |
|
| 22 |
+
def launch(model_choice, image_input):
|
| 23 |
+
return caption_image(model_choice, image_input)
|
| 24 |
|
| 25 |
model_dropdown = gr.Dropdown(choices=list(CAPTION_MODELS.keys()), label='Model Choice')
|
| 26 |
+
image_input = gr.Image(type="pil", label="Input Image or URL")
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(launch, inputs=[model_dropdown, image_input], outputs="text")
|
| 29 |
iface.launch()
|