sab commited on
Commit
4b5b8f2
·
1 Parent(s): b7139ee

token client

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -42,24 +42,36 @@ def process_image(image: np.ndarray):
42
  image_pil.save(buffered, format="PNG")
43
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
44
 
45
- # Send image to backend
 
 
 
 
 
 
46
  response = requests.post(
47
- os.getenv('BACKEND_URL'),
 
48
  files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
49
  )
50
 
51
- # Process the response
 
 
 
 
52
  result = response.json()
53
  processed_image_b64 = result["processed_image"]
54
  processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
55
 
56
  # Save the processed image
 
 
57
  image_path = output_folder / f"no_bg_image_{uuid.uuid4().hex}.png"
58
  processed_image.save(image_path)
59
 
60
  return (processed_image, image_pil), str(image_path)
61
 
62
-
63
  # Define inputs and outputs for the Gradio interface
64
  image = gr.Image(label="Upload a photo")
65
  output_slider = ImageSlider(label="Processed photo", type="pil")
 
42
  image_pil.save(buffered, format="PNG")
43
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
44
 
45
+ # Get API key from environment variable
46
+ api_key = os.getenv('API_KEY')
47
+
48
+ if not api_key:
49
+ raise ValueError("API_KEY is not set in the environment variables")
50
+
51
+ # Send image to backend with API key in headers
52
  response = requests.post(
53
+ os.getenv('BACKEND_URL') + "/process_image/",
54
+ headers={"access_token": api_key},
55
  files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
56
  )
57
 
58
+ # Check if the response is successful
59
+ if response.status_code != 200:
60
+ raise Exception(f"Request failed with status code {response.status_code}: {response.text}")
61
+
62
+ # Process the response
63
  result = response.json()
64
  processed_image_b64 = result["processed_image"]
65
  processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
66
 
67
  # Save the processed image
68
+ output_folder = Path("output") # Make sure this folder exists or create it
69
+ output_folder.mkdir(parents=True, exist_ok=True)
70
  image_path = output_folder / f"no_bg_image_{uuid.uuid4().hex}.png"
71
  processed_image.save(image_path)
72
 
73
  return (processed_image, image_pil), str(image_path)
74
 
 
75
  # Define inputs and outputs for the Gradio interface
76
  image = gr.Image(label="Upload a photo")
77
  output_slider = ImageSlider(label="Processed photo", type="pil")