njavidfar commited on
Commit
1dceef9
·
verified ·
1 Parent(s): 03c90ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -10,8 +10,10 @@ import json
10
 
11
  # Project by Nymbo
12
 
13
- API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
14
  timeout = 100
 
 
15
 
16
  # Function to query the API and return the generated image
17
  def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
@@ -42,24 +44,38 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
42
  }
43
  }
44
 
45
- # Send the request to the API and handle the response
46
- response = requests.post(API_URL, json=payload, timeout=timeout)
47
- if response.status_code != 200:
48
- print(f"Error: Failed to get image. Response status: {response.status_code}")
49
- print(f"Response content: {response.text}")
50
- if response.status_code == 503:
51
- raise gr.Error(f"{response.status_code} : The model is being loaded")
52
- raise gr.Error(f"{response.status_code}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- try:
55
- # Convert the response content into an image
56
- image_bytes = response.content
57
- image = Image.open(io.BytesIO(image_bytes))
58
- print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
59
- return image
60
- except Exception as e:
61
- print(f"Error when trying to open the image: {e}")
62
- return None
63
 
64
  # CSS to style the app
65
  css = """
 
10
 
11
  # Project by Nymbo
12
 
13
+ API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
14
  timeout = 100
15
+ max_retries = 3
16
+ retry_delay = 5
17
 
18
  # Function to query the API and return the generated image
19
  def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
 
44
  }
45
  }
46
 
47
+ # Add retry logic for model loading
48
+ for attempt in range(max_retries):
49
+ try:
50
+ # Send the request to the API and handle the response
51
+ response = requests.post(API_URL, json=payload, timeout=timeout)
52
+ if response.status_code == 200:
53
+ try:
54
+ # Convert the response content into an image
55
+ image_bytes = response.content
56
+ image = Image.open(io.BytesIO(image_bytes))
57
+ print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
58
+ return image
59
+ except Exception as e:
60
+ print(f"Error when trying to open the image: {e}")
61
+ return None
62
+ elif response.status_code == 503 and attempt < max_retries - 1:
63
+ print(f"Model is loading, attempt {attempt + 1}/{max_retries}. Waiting {retry_delay} seconds...")
64
+ time.sleep(retry_delay)
65
+ continue
66
+ else:
67
+ print(f"Error: Failed to get image. Response status: {response.status_code}")
68
+ print(f"Response content: {response.text}")
69
+ if response.status_code == 503:
70
+ raise gr.Error(f"Model is still loading after {max_retries} attempts. Please try again in a few minutes.")
71
+ raise gr.Error(f"{response.status_code}")
72
+ except requests.exceptions.Timeout:
73
+ if attempt < max_retries - 1:
74
+ print(f"Request timed out, attempt {attempt + 1}/{max_retries}. Retrying...")
75
+ continue
76
+ raise gr.Error("Request timed out. Please try again.")
77
 
78
+ return None
 
 
 
 
 
 
 
 
79
 
80
  # CSS to style the app
81
  css = """