Spaces:
Sleeping
Sleeping
app.py
CHANGED
@@ -2,41 +2,70 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
-
|
7 |
API_URL = "https://koj7q6d5hdy4h5tm.us-east-1.aws.endpoints.huggingface.cloud"
|
8 |
headers = {
|
|
|
9 |
"Accept": "application/json",
|
10 |
-
|
11 |
-
"Content-Type": "application/json"
|
12 |
}
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
elif image: # Input is an uploaded image
|
23 |
-
# Convert PIL Image to Base64
|
24 |
-
buffered = BytesIO()
|
25 |
-
image.save(buffered, format="PNG")
|
26 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
27 |
-
|
28 |
-
payload = {"inputs": [img_str]} # Sending Base64-encoded image
|
29 |
-
else:
|
30 |
-
return {"error": "No valid input provided!"}
|
31 |
-
|
32 |
-
# Call the API
|
33 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Blocks() as app:
|
41 |
gr.Markdown("# Image Upload or URL Input App")
|
42 |
gr.Markdown(
|
@@ -44,13 +73,23 @@ with gr.Blocks() as app:
|
|
44 |
)
|
45 |
|
46 |
with gr.Row():
|
|
|
47 |
image_input = gr.Image(label="Upload Image", type="pil") # PIL Image for uploaded images
|
|
|
48 |
url_input = gr.Textbox(label="Image URL", placeholder="Enter image URL")
|
49 |
|
|
|
50 |
output = gr.JSON(label="API Response")
|
51 |
|
|
|
52 |
process_button = gr.Button("Process")
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
|
|
|
56 |
app.launch(share=True)
|
|
|
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 |
headers = {
|
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 file.
|
19 |
|
20 |
+
Args:
|
21 |
+
url (str): The URL of the image to process.
|
22 |
+
image (PIL.Image): The uploaded image to process.
|
23 |
|
24 |
+
Returns:
|
25 |
+
dict: The JSON response from the API or an error message.
|
26 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
try:
|
28 |
+
if url:
|
29 |
+
# Prepare the payload for a URL input
|
30 |
+
payload = {"inputs": url.strip()}
|
31 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
32 |
+
elif image:
|
33 |
+
# Convert the PIL Image to bytes
|
34 |
+
buffered = BytesIO()
|
35 |
+
image.save(buffered, format="PNG")
|
36 |
+
buffered.seek(0) # Move to the beginning of the buffer
|
37 |
+
|
38 |
+
# Prepare the files payload for an image upload
|
39 |
+
files = {"file": ("image.png", buffered, "image/png")}
|
40 |
+
response = requests.post(API_URL, headers=headers, files=files)
|
41 |
+
else:
|
42 |
+
return {"error": "No valid input provided!"}
|
43 |
+
|
44 |
+
# Raise an HTTPError if the response was unsuccessful
|
45 |
+
response.raise_for_status()
|
46 |
+
|
47 |
+
# Return the JSON response from the API
|
48 |
+
return response.json()
|
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 |
+
"""
|
57 |
+
Determines whether to process a URL or an image upload.
|
58 |
+
|
59 |
+
Args:
|
60 |
+
image (PIL.Image): The uploaded image.
|
61 |
+
url (str): The URL of the image.
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
dict: The response from the API.
|
65 |
+
"""
|
66 |
+
return query(url=url, image=image)
|
67 |
+
|
68 |
+
# Define the Gradio interface
|
69 |
with gr.Blocks() as app:
|
70 |
gr.Markdown("# Image Upload or URL Input App")
|
71 |
gr.Markdown(
|
|
|
73 |
)
|
74 |
|
75 |
with gr.Row():
|
76 |
+
# Image input component
|
77 |
image_input = gr.Image(label="Upload Image", type="pil") # PIL Image for uploaded images
|
78 |
+
# URL input component
|
79 |
url_input = gr.Textbox(label="Image URL", placeholder="Enter image URL")
|
80 |
|
81 |
+
# Output component to display the API response
|
82 |
output = gr.JSON(label="API Response")
|
83 |
|
84 |
+
# Button to trigger processing
|
85 |
process_button = gr.Button("Process")
|
86 |
|
87 |
+
# Define the action when the button is clicked
|
88 |
+
process_button.click(
|
89 |
+
fn=process_input, # Function to execute
|
90 |
+
inputs=[image_input, url_input], # Inputs to the function
|
91 |
+
outputs=output # Output component to update
|
92 |
+
)
|
93 |
|
94 |
+
# Launch the Gradio app
|
95 |
app.launch(share=True)
|