Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
from dotenv import load_dotenv, find_dotenv
|
| 2 |
import os
|
| 3 |
import io
|
| 4 |
-
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
-
import base64
|
| 7 |
import requests
|
| 8 |
import json
|
| 9 |
import warnings
|
|
@@ -17,6 +15,12 @@ load_dotenv(find_dotenv())
|
|
| 17 |
hf_api_key = os.getenv('HF_API_KEY')
|
| 18 |
endpoint_url = os.getenv('HF_API_ITT_BASE')
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def generate_caption(image):
|
| 22 |
"""
|
|
@@ -24,26 +28,45 @@ def generate_caption(image):
|
|
| 24 |
:param image: An image in PIL format.
|
| 25 |
:return: Generated caption or error message.
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
if response.status_code == 200:
|
| 32 |
-
return response.json().get("generated_text", "No caption generated.")
|
| 33 |
-
else:
|
| 34 |
-
return f"Error: {response.status_code} - {response.text}"
|
| 35 |
|
| 36 |
# Predefined sample images
|
| 37 |
def get_sample_images():
|
| 38 |
"""
|
| 39 |
Returns a list of predefined sample images in the assets directory.
|
| 40 |
"""
|
| 41 |
-
sample_dir = "CreatureCaptures"
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Gradio interface
|
| 49 |
sample_images = get_sample_images() # Load predefined sample images
|
|
@@ -60,4 +83,6 @@ demo = gr.Interface(
|
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
|
|
|
| 63 |
demo.launch()
|
|
|
|
|
|
| 1 |
from dotenv import load_dotenv, find_dotenv
|
| 2 |
import os
|
| 3 |
import io
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
import warnings
|
|
|
|
| 15 |
hf_api_key = os.getenv('HF_API_KEY')
|
| 16 |
endpoint_url = os.getenv('HF_API_ITT_BASE')
|
| 17 |
|
| 18 |
+
# Validate environment variables
|
| 19 |
+
if not hf_api_key:
|
| 20 |
+
raise ValueError("HF_API_KEY is not set in the .env file.")
|
| 21 |
+
if not endpoint_url:
|
| 22 |
+
raise ValueError("HF_API_ITT_BASE is not set in the .env file.")
|
| 23 |
+
|
| 24 |
|
| 25 |
def generate_caption(image):
|
| 26 |
"""
|
|
|
|
| 28 |
:param image: An image in PIL format.
|
| 29 |
:return: Generated caption or error message.
|
| 30 |
"""
|
| 31 |
+
try:
|
| 32 |
+
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
| 33 |
+
|
| 34 |
+
# Convert PIL image to binary stream
|
| 35 |
+
buffered = io.BytesIO()
|
| 36 |
+
image.save(buffered, format="JPEG")
|
| 37 |
+
buffered.seek(0)
|
| 38 |
+
files = {"inputs": ("image.jpg", buffered, "image/jpeg")}
|
| 39 |
+
|
| 40 |
+
# Make the POST request to the endpoint
|
| 41 |
+
response = requests.post(endpoint_url, headers=headers, files=files)
|
| 42 |
+
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
return response.json().get("generated_text", "No caption generated.")
|
| 45 |
+
else:
|
| 46 |
+
# Log the error response for debugging
|
| 47 |
+
return (
|
| 48 |
+
f"Error: {response.status_code} - {response.text}\n"
|
| 49 |
+
f"Headers: {headers}\nEndpoint: {endpoint_url}"
|
| 50 |
+
)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"An error occurred: {str(e)}"
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Predefined sample images
|
| 56 |
def get_sample_images():
|
| 57 |
"""
|
| 58 |
Returns a list of predefined sample images in the assets directory.
|
| 59 |
"""
|
| 60 |
+
sample_dir = "CreatureCaptures" # Ensure this directory exists and contains sample images
|
| 61 |
+
try:
|
| 62 |
+
return [
|
| 63 |
+
os.path.join(sample_dir, file)
|
| 64 |
+
for file in os.listdir(sample_dir)
|
| 65 |
+
if file.lower().endswith((".png", ".jpg", ".jpeg"))
|
| 66 |
+
]
|
| 67 |
+
except FileNotFoundError:
|
| 68 |
+
return []
|
| 69 |
+
|
| 70 |
|
| 71 |
# Gradio interface
|
| 72 |
sample_images = get_sample_images() # Load predefined sample images
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
# Launch the Gradio demo
|
| 87 |
demo.launch()
|
| 88 |
+
|