Spaces:
Sleeping
Sleeping
File size: 3,689 Bytes
9318973 36d889f 6fae26b 284315c f353283 c3e32b0 284315c c3e32b0 36d889f c3e32b0 f353283 c3e32b0 36d889f 9318973 f353283 c3e32b0 fae0d88 f353283 36d889f f353283 fae0d88 c3e32b0 f353283 c3e32b0 f353283 c3e32b0 f353283 c3e32b0 f353283 c3e32b0 f353283 c3e32b0 f353283 c3e32b0 f353283 c3e32b0 36d889f f353283 36d889f fae0d88 6fae26b 2c5ad27 6fae26b f353283 6fae26b f353283 6fae26b 36d889f f353283 36d889f 6fae26b f353283 36d889f 6fae26b f353283 c3e32b0 f353283 36d889f f353283 b629e4c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import gradio as gr
import requests
from PIL import Image
import os
from io import BytesIO
import json
# Define the API URL and base headers (excluding Content-Type)
API_URL = "https://koj7q6d5hdy4h5tm.us-east-1.aws.endpoints.huggingface.cloud"
BASE_HEADERS = {
"Authorization": f"Bearer {os.getenv('hf')}",
"Accept": "application/json"
}
def query(url=None, image=None):
"""
Sends a request to the API endpoint with either a URL or an image.
Args:
url (str): The URL of the image to process.
image (PIL.Image): The uploaded image to process.
Returns:
dict: The JSON response from the API or an error message.
"""
try:
if url and url.strip():
# Prepare headers and payload for URL input
headers = BASE_HEADERS.copy()
headers["Content-Type"] = "application/json"
payload = {"inputs": url.strip()}
# Send POST request with JSON payload
response = requests.post(API_URL, headers=headers, json=payload)
elif image:
# Prepare headers for image upload
headers = BASE_HEADERS.copy()
headers["Content-Type"] = "image/png" # Change if using a different format
# Convert PIL Image to bytes
buffered = BytesIO()
image.save(buffered, format="PNG") # Ensure format matches Content-Type
image_bytes = buffered.getvalue()
# Send POST request with raw image bytes
response = requests.post(API_URL, headers=headers, data=image_bytes)
else:
return {"error": "No valid input provided!"}
# Raise an exception for HTTP error codes
response.raise_for_status()
# Attempt to return JSON response
try:
return response.json()
except json.JSONDecodeError:
return {"error": "Failed to decode JSON response from the API."}
except requests.exceptions.HTTPError as http_err:
return {"error": f"HTTP error occurred: {http_err}"}
except Exception as err:
return {"error": f"An unexpected error occurred: {err}"}
def process_input(image=None, url=None):
"""
Determines whether to process a URL or an image upload.
Args:
image (PIL.Image): The uploaded image.
url (str): The URL of the image.
Returns:
dict: The response from the API.
"""
return query(url=url, image=image)
# Define the Gradio interface
with gr.Blocks() as app:
gr.Markdown("# Image Upload or URL Input App")
gr.Markdown(
"Upload an image or provide an image URL to process the input and get the API response. The server may have a cold start and is running with a CPU. Thus, a 400, 503, timeout, etc. error may occur on the first attempt. If so, please wait one minute and try again. "
)
with gr.Row():
# Image input component
image_input = gr.Image(label="Upload Image", type="pil") # PIL Image for uploaded images
# URL input component
url_input = gr.Textbox(label="Image URL", placeholder="Enter image URL")
# Output component to display the API response
output = gr.JSON(label="API Response")
# Button to trigger processing
process_button = gr.Button("Process")
# Define the action when the button is clicked
process_button.click(
fn=process_input, # Function to execute
inputs=[image_input, url_input], # Inputs to the function
outputs=output # Output component to update
)
# Launch the Gradio app
app.launch(share=True)
|