Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,25 +13,26 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
# Define function for image captioning
|
| 16 |
-
def
|
| 17 |
try:
|
| 18 |
# Preprocess the image
|
| 19 |
processed_image = processor(images=image, return_tensors="pt").pixel_values.to(device)
|
| 20 |
with torch.no_grad():
|
| 21 |
output = model.generate(processed_image)
|
| 22 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 23 |
-
return caption
|
| 24 |
except Exception as e:
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
# Create Gradio Interface
|
| 28 |
interface = gr.Interface(
|
| 29 |
-
fn=
|
| 30 |
inputs=gr.Image(type="pil"),
|
| 31 |
-
outputs="
|
| 32 |
-
title="BLIP Image Captioning",
|
| 33 |
-
description="
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
interface
|
|
|
|
|
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
# Define function for image captioning
|
| 16 |
+
def analyze_scene_api(image):
|
| 17 |
try:
|
| 18 |
# Preprocess the image
|
| 19 |
processed_image = processor(images=image, return_tensors="pt").pixel_values.to(device)
|
| 20 |
with torch.no_grad():
|
| 21 |
output = model.generate(processed_image)
|
| 22 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
return {"caption": caption}
|
| 24 |
except Exception as e:
|
| 25 |
+
return {"error": str(e)}
|
| 26 |
|
| 27 |
+
# Create Gradio Interface with API mode
|
| 28 |
interface = gr.Interface(
|
| 29 |
+
fn=analyze_scene_api,
|
| 30 |
inputs=gr.Image(type="pil"),
|
| 31 |
+
outputs="json",
|
| 32 |
+
title="BLIP API for Image Captioning",
|
| 33 |
+
description="Send an image to get a caption response in JSON format."
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
+
# Launch Gradio interface in API mode
|
| 38 |
+
interface.launch(server_name="0.0.0.0", server_port=7860, enable_queue=True)
|