Spaces:
Sleeping
Sleeping
app.py
CHANGED
@@ -2,20 +2,19 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
import os
|
5 |
-
import base64
|
6 |
from io import BytesIO
|
|
|
7 |
|
8 |
-
# Define the API URL and headers
|
9 |
API_URL = "https://koj7q6d5hdy4h5tm.us-east-1.aws.endpoints.huggingface.cloud"
|
10 |
-
|
11 |
"Authorization": f"Bearer {os.getenv('hf')}",
|
12 |
-
"Accept": "application/json"
|
13 |
-
# Note: 'Content-Type' is omitted because it will be set automatically based on the payload
|
14 |
}
|
15 |
|
16 |
def query(url=None, image=None):
|
17 |
"""
|
18 |
-
Sends a request to the API endpoint with either a URL or an image
|
19 |
|
20 |
Args:
|
21 |
url (str): The URL of the image to process.
|
@@ -25,32 +24,44 @@ def query(url=None, image=None):
|
|
25 |
dict: The JSON response from the API or an error message.
|
26 |
"""
|
27 |
try:
|
28 |
-
if url:
|
29 |
-
# Prepare
|
|
|
|
|
30 |
payload = {"inputs": url.strip()}
|
|
|
|
|
31 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
32 |
elif image:
|
33 |
-
#
|
|
|
|
|
|
|
|
|
34 |
buffered = BytesIO()
|
35 |
-
image.save(buffered, format="PNG")
|
36 |
-
buffered.
|
37 |
-
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
else:
|
42 |
return {"error": "No valid input provided!"}
|
43 |
|
44 |
-
# Raise an
|
45 |
response.raise_for_status()
|
46 |
|
47 |
-
#
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
except requests.exceptions.HTTPError as http_err:
|
51 |
return {"error": f"HTTP error occurred: {http_err}"}
|
52 |
except Exception as err:
|
53 |
-
return {"error": f"An error occurred: {err}"}
|
54 |
|
55 |
def process_input(image=None, url=None):
|
56 |
"""
|
@@ -86,9 +97,9 @@ with gr.Blocks() as app:
|
|
86 |
|
87 |
# Define the action when the button is clicked
|
88 |
process_button.click(
|
89 |
-
fn=process_input,
|
90 |
-
inputs=[image_input, url_input],
|
91 |
-
outputs=output
|
92 |
)
|
93 |
|
94 |
# Launch the Gradio app
|
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
import os
|
|
|
5 |
from io import BytesIO
|
6 |
+
import json
|
7 |
|
8 |
+
# Define the API URL and base headers (excluding Content-Type)
|
9 |
API_URL = "https://koj7q6d5hdy4h5tm.us-east-1.aws.endpoints.huggingface.cloud"
|
10 |
+
BASE_HEADERS = {
|
11 |
"Authorization": f"Bearer {os.getenv('hf')}",
|
12 |
+
"Accept": "application/json"
|
|
|
13 |
}
|
14 |
|
15 |
def query(url=None, image=None):
|
16 |
"""
|
17 |
+
Sends a request to the API endpoint with either a URL or an image.
|
18 |
|
19 |
Args:
|
20 |
url (str): The URL of the image to process.
|
|
|
24 |
dict: The JSON response from the API or an error message.
|
25 |
"""
|
26 |
try:
|
27 |
+
if url and url.strip():
|
28 |
+
# Prepare headers and payload for URL input
|
29 |
+
headers = BASE_HEADERS.copy()
|
30 |
+
headers["Content-Type"] = "application/json"
|
31 |
payload = {"inputs": url.strip()}
|
32 |
+
|
33 |
+
# Send POST request with JSON payload
|
34 |
response = requests.post(API_URL, headers=headers, json=payload)
|
35 |
+
|
36 |
elif image:
|
37 |
+
# Prepare headers for image upload
|
38 |
+
headers = BASE_HEADERS.copy()
|
39 |
+
headers["Content-Type"] = "image/png" # Change if using a different format
|
40 |
+
|
41 |
+
# Convert PIL Image to bytes
|
42 |
buffered = BytesIO()
|
43 |
+
image.save(buffered, format="PNG") # Ensure format matches Content-Type
|
44 |
+
image_bytes = buffered.getvalue()
|
45 |
+
|
46 |
+
# Send POST request with raw image bytes
|
47 |
+
response = requests.post(API_URL, headers=headers, data=image_bytes)
|
48 |
+
|
49 |
else:
|
50 |
return {"error": "No valid input provided!"}
|
51 |
|
52 |
+
# Raise an exception for HTTP error codes
|
53 |
response.raise_for_status()
|
54 |
|
55 |
+
# Attempt to return JSON response
|
56 |
+
try:
|
57 |
+
return response.json()
|
58 |
+
except json.JSONDecodeError:
|
59 |
+
return {"error": "Failed to decode JSON response from the API."}
|
60 |
|
61 |
except requests.exceptions.HTTPError as http_err:
|
62 |
return {"error": f"HTTP error occurred: {http_err}"}
|
63 |
except Exception as err:
|
64 |
+
return {"error": f"An unexpected error occurred: {err}"}
|
65 |
|
66 |
def process_input(image=None, url=None):
|
67 |
"""
|
|
|
97 |
|
98 |
# Define the action when the button is clicked
|
99 |
process_button.click(
|
100 |
+
fn=process_input, # Function to execute
|
101 |
+
inputs=[image_input, url_input], # Inputs to the function
|
102 |
+
outputs=output # Output component to update
|
103 |
)
|
104 |
|
105 |
# Launch the Gradio app
|