Noah-Wang commited on
Commit
6fae26b
·
1 Parent(s): 7e57d7c
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -1,41 +1,55 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
  API_URL = "https://koj7q6d5hdy4h5tm.us-east-1.aws.endpoints.huggingface.cloud"
5
  headers = {
6
  "Accept": "application/json",
7
- "Authorization": "Bearer hf_XXXXX",
8
  "Content-Type": "application/json"
9
  }
10
 
11
  def query(payload):
 
12
  response = requests.post(API_URL, headers=headers, json=payload)
13
  return response.json()
14
 
15
- def process_file(filepath):
16
- if not filepath:
17
- return {"error": "No file provided!"}
18
-
19
- # Assuming the API takes file content as input
20
- with open(filepath, "rb") as f:
21
- file_content = f.read()
22
-
23
- response = query({
24
- "inputs": file_content.decode("utf-8", errors="ignore"), # Adjust encoding as per API expectations
25
- "parameters": {}
26
- })
27
 
 
28
  return response
29
 
30
  # Gradio app
31
  with gr.Blocks() as app:
32
- gr.Markdown("# File Upload App")
33
- gr.Markdown("Upload a file and send its content to the API.")
 
 
 
 
 
 
34
 
35
- file_input = gr.File(label="Upload a File", type="filepath") # Use 'filepath' to pass the file path
36
  output = gr.JSON(label="API Response")
37
-
38
  process_button = gr.Button("Process")
39
- process_button.click(process_file, inputs=file_input, outputs=output)
 
 
 
 
 
 
 
 
 
40
 
41
  app.launch()
 
1
  import gradio as gr
2
  import requests
3
+ from PIL import Image
4
 
5
  API_URL = "https://koj7q6d5hdy4h5tm.us-east-1.aws.endpoints.huggingface.cloud"
6
  headers = {
7
  "Accept": "application/json",
8
+ "Authorization": "Bearer " + str(os.getenv('hf')),
9
  "Content-Type": "application/json"
10
  }
11
 
12
  def query(payload):
13
+ print(payload)
14
  response = requests.post(API_URL, headers=headers, json=payload)
15
  return response.json()
16
 
17
+ def process_input(input):
18
+ if isinstance(input, str) and input.startswith("http"):
19
+ # Input is a URL
20
+ payload = {"inputs": [input]}
21
+ elif isinstance(input, Image.Image):
22
+ # Input is a PIL Image
23
+ payload = {"inputs": input} # Assuming the API can handle raw image objects
24
+ else:
25
+ return {"error": "Invalid input type. Provide a valid URL or image."}
 
 
 
26
 
27
+ response = query(payload)
28
  return response
29
 
30
  # Gradio app
31
  with gr.Blocks() as app:
32
+ gr.Markdown("# Image Upload App")
33
+ gr.Markdown(
34
+ "Upload an image file or provide an image URL. The app will process the input and call the API."
35
+ )
36
+
37
+ with gr.Row():
38
+ image_input = gr.Image(label="Upload Image", type="pil") # PIL Image for uploaded images
39
+ url_input = gr.Textbox(label="Image URL", placeholder="Enter image URL")
40
 
 
41
  output = gr.JSON(label="API Response")
42
+
43
  process_button = gr.Button("Process")
44
+
45
+ def handle_input(image, url):
46
+ if url.strip(): # If URL is provided
47
+ return process_input(url.strip())
48
+ elif image: # If image is uploaded
49
+ return process_input(image)
50
+ else:
51
+ return {"error": "No valid input provided!"}
52
+
53
+ process_button.click(handle_input, inputs=[image_input, url_input], outputs=output)
54
 
55
  app.launch()