Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import warnings
|
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
# Suppress specific warnings
|
| 11 |
-
warnings.filterwarnings("ignore", message=".*Using the model-agnostic default
|
| 12 |
|
| 13 |
# Load environment variables from .env file
|
| 14 |
load_dotenv(find_dotenv())
|
|
@@ -44,9 +44,17 @@ def generate_caption(image):
|
|
| 44 |
|
| 45 |
# Make the POST request to the endpoint
|
| 46 |
response = requests.post(endpoint_url, headers=headers, json=payload)
|
|
|
|
| 47 |
|
| 48 |
if response.status_code == 200:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
# Log the error response for debugging
|
| 52 |
return (
|
|
@@ -83,12 +91,13 @@ demo = gr.Interface(
|
|
| 83 |
title="Image Captioning App",
|
| 84 |
description=(
|
| 85 |
"Upload an image or use one of the predefined samples to generate a caption. "
|
| 86 |
-
"This app uses a Hugging Face Inference Endpoint for the
|
| 87 |
),
|
| 88 |
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
# Launch the Gradio demo
|
| 92 |
-
demo.launch()
|
|
|
|
| 93 |
|
| 94 |
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
# Suppress specific warnings
|
| 11 |
+
warnings.filterwarnings("ignore", message=".*Using the model-agnostic default max_length.*")
|
| 12 |
|
| 13 |
# Load environment variables from .env file
|
| 14 |
load_dotenv(find_dotenv())
|
|
|
|
| 44 |
|
| 45 |
# Make the POST request to the endpoint
|
| 46 |
response = requests.post(endpoint_url, headers=headers, json=payload)
|
| 47 |
+
response_data = response.json()
|
| 48 |
|
| 49 |
if response.status_code == 200:
|
| 50 |
+
# Handle response as a list or dictionary
|
| 51 |
+
if isinstance(response_data, list):
|
| 52 |
+
# Assuming the first item contains the generated text
|
| 53 |
+
return response_data[0].get("generated_text", "No caption generated.")
|
| 54 |
+
elif isinstance(response_data, dict):
|
| 55 |
+
return response_data.get("generated_text", "No caption generated.")
|
| 56 |
+
else:
|
| 57 |
+
return f"Unexpected response format: {response_data}"
|
| 58 |
else:
|
| 59 |
# Log the error response for debugging
|
| 60 |
return (
|
|
|
|
| 91 |
title="Image Captioning App",
|
| 92 |
description=(
|
| 93 |
"Upload an image or use one of the predefined samples to generate a caption. "
|
| 94 |
+
"This app uses a Hugging Face Inference Endpoint for the Salesforce/blip-image-captioning-base model."
|
| 95 |
),
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
# Launch the Gradio demo
|
| 100 |
+
demo.launch()
|
| 101 |
+
|
| 102 |
|
| 103 |
|