anonymous-IA commited on
Commit
da4f3b2
·
verified ·
1 Parent(s): ee3433a

Upload 41 files

Browse files
Files changed (4) hide show
  1. README.md +6 -3
  2. __pycache__/app.cpython-310.pyc +0 -0
  3. app.py +11 -5
  4. requirements.txt +1 -1
README.md CHANGED
@@ -5,10 +5,13 @@ colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
7
  sdk_version: 4.44.1
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
  short_description: Gaze-guided Gaussian-noise image regeneration
 
 
 
12
  ---
13
 
14
  # GazeCorrect — Gaze-Guided Regeneration
 
5
  colorTo: red
6
  sdk: gradio
7
  sdk_version: 4.44.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
  short_description: Gaze-guided Gaussian-noise image regeneration
12
+ hf_oauth: true
13
+ hf_oauth_scopes:
14
+ - gated-repos
15
  ---
16
 
17
  # GazeCorrect — Gaze-Guided Regeneration
__pycache__/app.cpython-310.pyc CHANGED
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
 
app.py CHANGED
@@ -134,18 +134,23 @@ def device_dtype():
134
  return ("cuda", torch.float16) if torch.cuda.is_available() else ("cpu", torch.float32)
135
 
136
 
137
- @lru_cache(maxsize=1)
138
- def pipeline():
139
  from diffusers import StableDiffusionImg2ImgPipeline
140
  device, dtype = device_dtype()
141
- token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
142
  return StableDiffusionImg2ImgPipeline.from_pretrained(MODEL_ID, torch_dtype=dtype, token=token).to(device)
143
 
144
 
145
  @spaces.GPU
146
- def generate(image, points, description, spread, degree, feather, strength, steps, seed, progress=gr.Progress()):
 
147
  if image is None: return None, None, None, "Upload an image first."
148
  if not points: return None, None, None, "Add clicked or CSV gaze points first."
 
 
 
 
 
149
  seed = None if seed < 0 else int(seed)
150
  try:
151
  progress(.1, desc="Creating gaze attention")
@@ -154,7 +159,7 @@ def generate(image, points, description, spread, degree, feather, strength, step
154
  noised, mask = noisy_image(image, heat, degree, feather, seed)
155
  progress(.4, desc="Regenerating with RoentGen-v2")
156
  device, _ = device_dtype(); generator = None if seed is None else torch.Generator(device=device).manual_seed(seed)
157
- output = pipeline()(prompt=description.strip() or "Normal chest radiograph.", image=noised,
158
  strength=float(strength), guidance_scale=3.5,
159
  num_inference_steps=int(steps), generator=generator).images[0].convert("RGB")
160
  corrected = Image.composite(output.resize(image.size), image.convert("RGB"), mask)
@@ -166,6 +171,7 @@ def generate(image, points, description, spread, degree, feather, strength, step
166
  with gr.Blocks(title="GazeCorrect") as demo:
167
  gr.Markdown("# GazeCorrect\nImage + gaze clicks/CSV + disease description → attention noise → corrected regenerated image. Use chest X-rays only; research use only.")
168
  image_state, points_state = gr.State(None), gr.State([])
 
169
  with gr.Row():
170
  with gr.Column():
171
  upload = gr.File(label="1. Upload chest X-ray", file_types=[".png", ".jpg", ".jpeg", ".webp", ".bmp"])
 
134
  return ("cuda", torch.float16) if torch.cuda.is_available() else ("cpu", torch.float32)
135
 
136
 
137
+ @lru_cache(maxsize=2)
138
+ def pipeline(token):
139
  from diffusers import StableDiffusionImg2ImgPipeline
140
  device, dtype = device_dtype()
 
141
  return StableDiffusionImg2ImgPipeline.from_pretrained(MODEL_ID, torch_dtype=dtype, token=token).to(device)
142
 
143
 
144
  @spaces.GPU
145
+ def generate(image, points, description, spread, degree, feather, strength, steps, seed,
146
+ oauth_token: gr.OAuthToken | None = None, progress=gr.Progress()):
147
  if image is None: return None, None, None, "Upload an image first."
148
  if not points: return None, None, None, "Add clicked or CSV gaze points first."
149
+ token = oauth_token.token if oauth_token is not None else (
150
+ os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
151
+ )
152
+ if not token:
153
+ return None, None, None, "Sign in with Hugging Face first, then click Generate."
154
  seed = None if seed < 0 else int(seed)
155
  try:
156
  progress(.1, desc="Creating gaze attention")
 
159
  noised, mask = noisy_image(image, heat, degree, feather, seed)
160
  progress(.4, desc="Regenerating with RoentGen-v2")
161
  device, _ = device_dtype(); generator = None if seed is None else torch.Generator(device=device).manual_seed(seed)
162
+ output = pipeline(token)(prompt=description.strip() or "Normal chest radiograph.", image=noised,
163
  strength=float(strength), guidance_scale=3.5,
164
  num_inference_steps=int(steps), generator=generator).images[0].convert("RGB")
165
  corrected = Image.composite(output.resize(image.size), image.convert("RGB"), mask)
 
171
  with gr.Blocks(title="GazeCorrect") as demo:
172
  gr.Markdown("# GazeCorrect\nImage + gaze clicks/CSV + disease description → attention noise → corrected regenerated image. Use chest X-rays only; research use only.")
173
  image_state, points_state = gr.State(None), gr.State([])
174
+ gr.LoginButton("Sign in with Hugging Face")
175
  with gr.Row():
176
  with gr.Column():
177
  upload = gr.File(label="1. Upload chest X-ray", file_types=[".png", ".jpg", ".jpeg", ".webp", ".bmp"])
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==4.44.1
2
  torch>=2.1
3
  numpy
4
  pandas
 
1
+ gradio[oauth]==4.44.1
2
  torch>=2.1
3
  numpy
4
  pandas