Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
def image_to_prompt(image):
|
| 6 |
+
"""Generates a caption (prompt) from an image using Vertex AI."""
|
| 7 |
+
if image is None:
|
| 8 |
+
return "Please upload an image."
|
| 9 |
+
|
| 10 |
+
try:
|
| 11 |
+
model = ImageCaptioningModel.from_pretrained("imagetext@004") #or imagetext@003
|
| 12 |
+
captions = model.get_captions(image=image, number_of_results=1) # Get the top caption
|
| 13 |
+
if captions:
|
| 14 |
+
return captions[0]
|
| 15 |
+
else:
|
| 16 |
+
return "Could not generate a caption."
|
| 17 |
+
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"An error occurred: {e}"
|
| 20 |
+
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=image_to_prompt,
|
| 23 |
+
inputs=gr.Image(type="pil"),
|
| 24 |
+
outputs=gr.Textbox(label="Generated Prompt"),
|
| 25 |
+
title="Image to Prompt Generator",
|
| 26 |
+
description="Upload an image and get a text prompt describing it.",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
iface.launch()
|