SkalskiP commited on
Commit
24aa6b9
1 Parent(s): d981a02

test running API calls with user HF_TOKEN

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -1,10 +1,9 @@
 
1
  from typing import Tuple
2
 
3
- import os
4
- import requests
5
- import random
6
- import numpy as np
7
  import gradio as gr
 
 
8
  import spaces
9
  import torch
10
  from PIL import Image, ImageFilter
@@ -23,8 +22,8 @@ MAX_SEED = np.iinfo(np.int32).max
23
  IMAGE_SIZE = 1024
24
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
25
 
26
- HF_TOKEN = os.environ.get("HF_TOKEN", None)
27
- client = Client("SkalskiP/florence-sam-masking", hf_token=HF_TOKEN)
28
 
29
 
30
  def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
@@ -44,6 +43,7 @@ def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
44
 
45
  EXAMPLES = [
46
  [
 
47
  {
48
  "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
49
  "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
@@ -57,6 +57,7 @@ EXAMPLES = [
57
  30
58
  ],
59
  [
 
60
  {
61
  "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
62
  "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
@@ -101,8 +102,14 @@ def is_image_empty(image: Image.Image) -> bool:
101
  return all(pixel == 0 for pixel in pixels)
102
 
103
 
 
 
 
 
 
104
  @spaces.GPU(duration=100)
105
  def process(
 
106
  input_image_editor: dict,
107
  inpainting_prompt_text: str,
108
  masking_prompt_text: str,
@@ -164,6 +171,7 @@ def process(
164
 
165
 
166
  with gr.Blocks() as demo:
 
167
  gr.Markdown(MARKDOWN)
168
  with gr.Row():
169
  with gr.Column():
@@ -240,6 +248,7 @@ with gr.Blocks() as demo:
240
  input_image_editor_component,
241
  inpainting_prompt_text_component,
242
  masking_prompt_text_component,
 
243
  seed_slicer_component,
244
  randomize_seed_checkbox_component,
245
  strength_slider_component,
@@ -269,5 +278,6 @@ with gr.Blocks() as demo:
269
  output_mask_component
270
  ]
271
  )
 
272
 
273
  demo.launch(debug=False, show_error=True)
 
1
+ import random
2
  from typing import Tuple
3
 
 
 
 
 
4
  import gradio as gr
5
+ import numpy as np
6
+ import requests
7
  import spaces
8
  import torch
9
  from PIL import Image, ImageFilter
 
22
  IMAGE_SIZE = 1024
23
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
24
 
25
+ # HF_TOKEN = os.environ.get("HF_TOKEN", None)
26
+ # client = Client("SkalskiP/florence-sam-masking", hf_token=HF_TOKEN)
27
 
28
 
29
  def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
 
43
 
44
  EXAMPLES = [
45
  [
46
+ None,
47
  {
48
  "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
49
  "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
 
57
  30
58
  ],
59
  [
60
+ None,
61
  {
62
  "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
63
  "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
 
102
  return all(pixel == 0 for pixel in pixels)
103
 
104
 
105
+ def set_client_for_session(request: gr.Request):
106
+ x_ip_token = request.headers['x-ip-token']
107
+ return Client("SkalskiP/florence-sam-masking", headers={"X-IP-Token": x_ip_token})
108
+
109
+
110
  @spaces.GPU(duration=100)
111
  def process(
112
+ client,
113
  input_image_editor: dict,
114
  inpainting_prompt_text: str,
115
  masking_prompt_text: str,
 
171
 
172
 
173
  with gr.Blocks() as demo:
174
+ client_component = gr.State()
175
  gr.Markdown(MARKDOWN)
176
  with gr.Row():
177
  with gr.Column():
 
248
  input_image_editor_component,
249
  inpainting_prompt_text_component,
250
  masking_prompt_text_component,
251
+ masking_prompt_text_component,
252
  seed_slicer_component,
253
  randomize_seed_checkbox_component,
254
  strength_slider_component,
 
278
  output_mask_component
279
  ]
280
  )
281
+ demo.load(set_client_for_session, None, client_component)
282
 
283
  demo.launch(debug=False, show_error=True)