artificialguybr commited on
Commit
cb4574c
1 Parent(s): 33e2812

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import gradio as gr
2
  import requests
3
  import io
 
 
4
  from PIL import Image
5
- import json
6
  from image_processing import downscale_image, limit_colors, convert_to_grayscale, convert_to_black_and_white, resize_image, DITHER_METHODS, QUANTIZATION_METHODS
7
- import logging
 
 
8
 
9
  class SomeClass:
10
  def __init__(self):
@@ -28,44 +31,44 @@ def update_selection(selected_state: gr.SelectData):
28
 
29
  def run_lora(prompt, selected_state, progress=gr.Progress(track_tqdm=True)):
30
  logging.debug(f"Inside run_lora, selected_state: {selected_state}")
 
31
  if not selected_state:
32
- logging.error("selected_state is None or empty.")
33
- raise gr.Error("You must select a LoRA before proceeding.") # Popup error when no LoRA is selected
34
-
35
- selected_lora_index = selected_state.index # Changed this line
 
 
 
 
 
36
  selected_lora = loras[selected_lora_index]
37
  api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
38
  trigger_word = selected_lora["trigger_word"]
39
- token = os.getenv("API_TOKEN")
40
  payload = {
41
  "inputs": f"{prompt} {trigger_word}",
42
- "parameters":{"negative_prompt": "bad art, ugly, watermark, deformed"},
43
  }
44
  headers = {"Authorization": f"Bearer {token}"}
45
 
46
- # Add a print statement to display the API request
47
- print(f"API Request: {api_url}")
48
- #print(f"API Headers: {headers}")
49
- print(f"API Payload: {payload}")
50
 
51
  error_count = 0
52
- pbar = tqdm(total=None, desc="Loading model")
53
- while(True):
54
- response = requests.post(api_url, json=payload)
55
  if response.status_code == 200:
56
  return Image.open(io.BytesIO(response.content))
57
  elif response.status_code == 503:
58
- #503 is triggered when the model is doing cold boot. It also gives you a time estimate from when the model is loaded but it is not super precise
59
  time.sleep(1)
60
- pbar.update(1)
61
  elif response.status_code == 500 and error_count < 5:
62
- print(response.content)
63
  time.sleep(1)
64
  error_count += 1
65
- continue
66
  else:
67
- logging.error(f"API Error: {response.status_code}")
68
- raise gr.Error("API Error: Unable to fetch the image.") # Raise a Gradio error here
 
69
 
70
  def postprocess(
71
  image,
 
1
  import gradio as gr
2
  import requests
3
  import io
4
+ import os
5
+ import logging
6
  from PIL import Image
 
7
  from image_processing import downscale_image, limit_colors, convert_to_grayscale, convert_to_black_and_white, resize_image, DITHER_METHODS, QUANTIZATION_METHODS
8
+
9
+ # Configuração de log
10
+ logging.basicConfig(level=logging.DEBUG)
11
 
12
  class SomeClass:
13
  def __init__(self):
 
31
 
32
  def run_lora(prompt, selected_state, progress=gr.Progress(track_tqdm=True)):
33
  logging.debug(f"Inside run_lora, selected_state: {selected_state}")
34
+
35
  if not selected_state:
36
+ logging.error("selected_state is None or empty. Make sure a LoRA is selected.")
37
+ raise gr.Error("You must select a LoRA before proceeding.")
38
+
39
+ token = os.getenv("API_TOKEN")
40
+ if not token:
41
+ logging.error("API_TOKEN is not set.")
42
+ raise gr.Error("API_TOKEN is not set.")
43
+
44
+ selected_lora_index = selected_state.index
45
  selected_lora = loras[selected_lora_index]
46
  api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
47
  trigger_word = selected_lora["trigger_word"]
 
48
  payload = {
49
  "inputs": f"{prompt} {trigger_word}",
50
+ "parameters": {"negative_prompt": "bad art, ugly, watermark, deformed"},
51
  }
52
  headers = {"Authorization": f"Bearer {token}"}
53
 
54
+ logging.debug(f"API Request: {api_url}")
55
+ logging.debug(f"API Payload: {payload}")
 
 
56
 
57
  error_count = 0
58
+ while True:
59
+ response = requests.post(api_url, json=payload, headers=headers)
 
60
  if response.status_code == 200:
61
  return Image.open(io.BytesIO(response.content))
62
  elif response.status_code == 503:
 
63
  time.sleep(1)
 
64
  elif response.status_code == 500 and error_count < 5:
65
+ logging.error(response.content)
66
  time.sleep(1)
67
  error_count += 1
 
68
  else:
69
+ logging.error(f"Unexpected API Error: {response.status_code}")
70
+ raise gr.Error(f"Unexpected API Error: {response.status_code}")
71
+
72
 
73
  def postprocess(
74
  image,