Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ try:
|
|
| 5 |
captioning_pipeline = pipeline(
|
| 6 |
"image-to-text",
|
| 7 |
model="nlpconnect/vit-gpt2-image-captioning",
|
| 8 |
-
device=-1
|
| 9 |
)
|
| 10 |
except Exception as e:
|
| 11 |
captioning_pipeline = None
|
|
@@ -19,15 +19,14 @@ def generate_caption(image):
|
|
| 19 |
if captioning_pipeline is None:
|
| 20 |
return "Model failed to load. Check logs for details."
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
except Exception as e:
|
| 27 |
return f"An error occurred during generation: {e}"
|
| 28 |
|
| 29 |
-
#Gradio Interface Definition
|
| 30 |
-
|
| 31 |
example_paths = [
|
| 32 |
["examples/dog_park.jpg"],
|
| 33 |
["examples/city_scene.png"],
|
|
@@ -40,10 +39,9 @@ iface = gr.Interface(
|
|
| 40 |
outputs=gr.Textbox(label="Generated Caption"),
|
| 41 |
title="Custom Image Caption Generator (Hugging Face Space)",
|
| 42 |
description="Upload an image and have a generative AI model describe what it sees.",
|
| 43 |
-
examples=example_paths,
|
| 44 |
allow_flagging="auto"
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# --- 4. Launch ---
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
iface.launch()
|
|
|
|
| 5 |
captioning_pipeline = pipeline(
|
| 6 |
"image-to-text",
|
| 7 |
model="nlpconnect/vit-gpt2-image-captioning",
|
| 8 |
+
device=-1 # -1 CPU, 0 GPU
|
| 9 |
)
|
| 10 |
except Exception as e:
|
| 11 |
captioning_pipeline = None
|
|
|
|
| 19 |
if captioning_pipeline is None:
|
| 20 |
return "Model failed to load. Check logs for details."
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
results = captioning_pipeline(image, max_new_tokens=50) # Set a limit on caption length
|
| 24 |
+
caption = results[0]['generated_text']
|
| 25 |
+
return caption
|
| 26 |
except Exception as e:
|
| 27 |
return f"An error occurred during generation: {e}"
|
| 28 |
|
| 29 |
+
# Gradio Interface Definition
|
|
|
|
| 30 |
example_paths = [
|
| 31 |
["examples/dog_park.jpg"],
|
| 32 |
["examples/city_scene.png"],
|
|
|
|
| 39 |
outputs=gr.Textbox(label="Generated Caption"),
|
| 40 |
title="Custom Image Caption Generator (Hugging Face Space)",
|
| 41 |
description="Upload an image and have a generative AI model describe what it sees.",
|
| 42 |
+
examples=example_paths,
|
| 43 |
allow_flagging="auto"
|
| 44 |
)
|
| 45 |
|
|
|
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
iface.launch()
|