Spaces:
Runtime error
Runtime error
Commit
·
e1acbd5
1
Parent(s):
86573d5
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,9 @@ CAPTION_MODELS = {
|
|
| 9 |
'blip2-2.7b-fp16': 'Mediocreatmybest/blip2-opt-2.7b-fp16-sharded',
|
| 10 |
}
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
# Simple caption creation
|
| 13 |
def caption_image(model_choice, image_input, url_input):
|
| 14 |
if image_input is not None:
|
|
@@ -16,11 +19,18 @@ def caption_image(model_choice, image_input, url_input):
|
|
| 16 |
else:
|
| 17 |
input_data = url_input
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
caption = captioner(input_data)[0]['generated_text']
|
| 25 |
return str(caption).strip()
|
| 26 |
|
|
@@ -32,4 +42,4 @@ image_input = gr.Image(type="pil", label="Input Image")
|
|
| 32 |
url_input = gr.Text(label="Input URL")
|
| 33 |
|
| 34 |
iface = gr.Interface(launch, inputs=[model_dropdown, image_input, url_input], outputs="text")
|
| 35 |
-
iface.launch()
|
|
|
|
| 9 |
'blip2-2.7b-fp16': 'Mediocreatmybest/blip2-opt-2.7b-fp16-sharded',
|
| 10 |
}
|
| 11 |
|
| 12 |
+
# Create a dictionary to store loaded models
|
| 13 |
+
loaded_models = {}
|
| 14 |
+
|
| 15 |
# Simple caption creation
|
| 16 |
def caption_image(model_choice, image_input, url_input):
|
| 17 |
if image_input is not None:
|
|
|
|
| 19 |
else:
|
| 20 |
input_data = url_input
|
| 21 |
|
| 22 |
+
# Check if the model is already loaded
|
| 23 |
+
if model_choice in loaded_models:
|
| 24 |
+
captioner = loaded_models[model_choice]
|
| 25 |
+
else:
|
| 26 |
+
captioner = pipeline(task="image-to-text",
|
| 27 |
+
model=CAPTION_MODELS[model_choice],
|
| 28 |
+
max_new_tokens=30,
|
| 29 |
+
device_map="cpu", use_fast=True
|
| 30 |
+
)
|
| 31 |
+
# Store the loaded model
|
| 32 |
+
loaded_models[model_choice] = captioner
|
| 33 |
+
|
| 34 |
caption = captioner(input_data)[0]['generated_text']
|
| 35 |
return str(caption).strip()
|
| 36 |
|
|
|
|
| 42 |
url_input = gr.Text(label="Input URL")
|
| 43 |
|
| 44 |
iface = gr.Interface(launch, inputs=[model_dropdown, image_input, url_input], outputs="text")
|
| 45 |
+
iface.launch()
|