Spaces:
Runtime error
Runtime error
Commit
·
44717e7
1
Parent(s):
dc55101
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
from transformers import pipeline
|
4 |
|
5 |
CAPTION_MODELS = {
|
@@ -10,7 +13,14 @@ CAPTION_MODELS = {
|
|
10 |
}
|
11 |
|
12 |
# Simple caption creation
|
13 |
-
def caption_image(model_choice,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
captioner = pipeline(task="image-to-text",
|
15 |
model=CAPTION_MODELS[model_choice],
|
16 |
max_new_tokens=30,
|
@@ -23,5 +33,5 @@ def launch(model_choice, input):
|
|
23 |
return caption_image(model_choice, input)
|
24 |
|
25 |
model_dropdown = gr.Dropdown(choices=list(CAPTION_MODELS.keys()), label='Model Choice')
|
26 |
-
iface = gr.Interface(launch, inputs=[model_dropdown, "
|
27 |
-
iface.launch()
|
|
|
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 = {
|
|
|
13 |
}
|
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,
|
|
|
33 |
return caption_image(model_choice, input)
|
34 |
|
35 |
model_dropdown = gr.Dropdown(choices=list(CAPTION_MODELS.keys()), label='Model Choice')
|
36 |
+
iface = gr.Interface(launch, inputs=[model_dropdown, gr.Data(type="file", label="Upload Image or Enter URL")], outputs="text")
|
37 |
+
iface.launch()
|