Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,47 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 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
|
| 10 |
import gradio as gr
|
|
|
|
| 11 |
|
| 12 |
-
# Suppress
|
| 13 |
warnings.filterwarnings("ignore", message=".*Using the model-agnostic default `max_length`.*")
|
| 14 |
|
| 15 |
-
# Load
|
| 16 |
-
|
| 17 |
-
hf_api_key = os.getenv('HF_API_KEY')
|
| 18 |
-
endpoint_url = os.getenv('HF_API_ITT_BASE')
|
| 19 |
-
|
| 20 |
-
# Helper function for image-to-text API
|
| 21 |
-
|
| 22 |
-
def get_completion(image, parameters=None, endpoint_url=endpoint_url):
|
| 23 |
-
headers = {
|
| 24 |
-
"Authorization": f"Bearer {hf_api_key}",
|
| 25 |
-
"Content-Type": "application/json"
|
| 26 |
-
}
|
| 27 |
-
# Convert image to base64 format
|
| 28 |
-
buffered = BytesIO()
|
| 29 |
-
image.save(buffered, format="JPEG")
|
| 30 |
-
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 31 |
-
|
| 32 |
-
data = {"inputs": {"image": image_base64}}
|
| 33 |
-
if parameters is not None:
|
| 34 |
-
data.update({"parameters": parameters})
|
| 35 |
-
|
| 36 |
-
response = requests.post(endpoint_url, headers=headers, data=json.dumps(data))
|
| 37 |
-
|
| 38 |
-
if response.status_code != 200:
|
| 39 |
-
return {"error": response.text}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
# Try parsing the response as JSON
|
| 43 |
-
response_data = json.loads(response.content.decode("utf-8"))
|
| 44 |
-
|
| 45 |
-
# Check if it's a list and extract the first item
|
| 46 |
-
if isinstance(response_data, list) and len(response_data) > 0:
|
| 47 |
-
return response_data[0]
|
| 48 |
-
elif isinstance(response_data, dict):
|
| 49 |
-
return response_data
|
| 50 |
-
else:
|
| 51 |
-
return {"error": "Unexpected response format"}
|
| 52 |
-
except json.JSONDecodeError:
|
| 53 |
-
return {"error": "Failed to decode API response"}
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# Helper function to download and process the image from a URL
|
| 57 |
def caption_image(image_url):
|
| 58 |
try:
|
|
|
|
| 59 |
response = requests.get(image_url)
|
| 60 |
response.raise_for_status()
|
| 61 |
-
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 62 |
-
|
| 63 |
-
# Get caption from API
|
| 64 |
-
caption_response = get_completion(image)
|
| 65 |
-
|
| 66 |
-
# Handle API response
|
| 67 |
-
if "error" in caption_response:
|
| 68 |
-
return f"Error: {caption_response['error']}"
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
return f"Error processing image: {str(e)}"
|
| 74 |
|
| 75 |
-
# Gradio interface
|
| 76 |
demo = gr.Interface(
|
| 77 |
fn=caption_image,
|
| 78 |
inputs=gr.Textbox(label="Image URL"),
|
| 79 |
outputs="text",
|
| 80 |
title="Image Captioning App",
|
| 81 |
description=(
|
| 82 |
-
"Upload an image or use one of the predefined
|
| 83 |
-
"This app uses
|
| 84 |
),
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import io
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import requests
|
|
|
|
| 6 |
import warnings
|
| 7 |
import gradio as gr
|
| 8 |
+
from transformers import pipeline
|
| 9 |
|
| 10 |
+
# Suppress warnings
|
| 11 |
warnings.filterwarnings("ignore", message=".*Using the model-agnostic default `max_length`.*")
|
| 12 |
|
| 13 |
+
# Load BLIP image captioning model via Hugging Face pipeline
|
| 14 |
+
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Helper function to download/process image and generate caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def caption_image(image_url):
|
| 18 |
try:
|
| 19 |
+
# Load image from URL
|
| 20 |
response = requests.get(image_url)
|
| 21 |
response.raise_for_status()
|
| 22 |
+
image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Generate caption using the pipeline
|
| 25 |
+
caption = captioner(image)[0]["generated_text"]
|
| 26 |
+
return caption
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error processing image: {str(e)}"
|
| 30 |
|
| 31 |
+
# Gradio interface with JPEG examples
|
| 32 |
demo = gr.Interface(
|
| 33 |
fn=caption_image,
|
| 34 |
inputs=gr.Textbox(label="Image URL"),
|
| 35 |
outputs="text",
|
| 36 |
title="Image Captioning App",
|
| 37 |
description=(
|
| 38 |
+
"Upload an image or use one of the predefined examples to generate a caption. "
|
| 39 |
+
"This app uses `Salesforce/blip-image-captioning-base`."
|
| 40 |
),
|
| 41 |
+
examples=[
|
| 42 |
+
['https://free-images.com/lg/9e46/white_bengal_tiger_tiger_0.jpg']
|
| 43 |
+
],
|
| 44 |
+
flagging_mode="never"
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|