Noah-Wang commited on
Commit
f353283
·
1 Parent(s): fae0d88
Files changed (1) hide show
  1. app.py +65 -26
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
- "Authorization": "Bearer " + str(os.getenv('hf')),
11
- "Content-Type": "application/json"
12
  }
13
 
 
 
 
14
 
15
- def query(payload):
16
- response = requests.post(API_URL, headers=headers, json=payload)
17
- return response.json()
18
 
19
- def process_input(image=None, url=None):
20
- if url and url.strip(): # Input is a URL
21
- payload = {"inputs": [url.strip()]}
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
- response = query(payload)
35
- return response
36
- except Exception as e:
37
- return {"error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Gradio app
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- process_button.click(process_input, inputs=[image_input, url_input], outputs=output)
 
 
 
 
 
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)