measmonysuon commited on
Commit
f8dd778
1 Parent(s): 81ead26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -16,7 +16,7 @@ API_TOKEN = os.getenv('ZeroGPU')
16
  # Set your token
17
  login(token=API_TOKEN, add_to_git_credential=True)
18
  # Initialize API client
19
- api = HfApi(token=HF_TOKEN)
20
 
21
  # Initialize the client for image generation "prithivMLmods/IMAGINEO-4K" "mukaist/DALLE-4K"
22
  client_image = Client("measmonysuon/DALLE-4K")
@@ -39,7 +39,6 @@ logging.basicConfig(level=logging.INFO)
39
  logger = logging.getLogger(__name__)
40
 
41
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
42
- api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='a10g-large')
43
  resolution = resolutions.get(resolution_key, (1024, 1024))
44
  width, height = resolution
45
  full_prompt = f"{prompt}, Canon EOS R5, 4K, Photo-Realistic, appearing photorealistic with super fine details, high resolution, natural look, hyper realistic photography, cinematic lighting, --ar 64:37, --v 6.0, --style raw, --stylize 750"
@@ -58,11 +57,9 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
58
  api_name="/run"
59
  )
60
  logger.info("Image generation successful.")
61
- api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='zero-a10g')
62
  return result
63
  except Exception as e:
64
  logger.error(f"Error generating image: {e}")
65
- api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='zero-a10g')
66
  return None
67
 
68
  def request_otp(user_chat_id):
@@ -144,9 +141,13 @@ def notify_webhook(user_chat_id):
144
  except requests.RequestException as e:
145
  logger.error(f"Error sending webhook notification: {e}")
146
 
147
- def notify_webhook_with_file(user_chat_id, full_file_url):
148
  webhook_url = f"{webhook_server}/notify_image"
149
- payload = {'user_chat_id': user_chat_id, 'file_path': full_file_url}
 
 
 
 
150
  try:
151
  response = requests.post(webhook_url, json=payload)
152
  response.raise_for_status()
@@ -172,21 +173,40 @@ def gradio_interface(prompt, resolution_key, user_chat_id):
172
  def gradio_interface(prompt, resolution_key, user_chat_id):
173
  if not user_chat_id.strip():
174
  return None, "User Chat ID cannot be blank. Please provide a valid User Chat ID or register with @supportBot to get UID."
 
 
 
175
 
176
- result = generate_image(prompt, resolution_key)
177
- if result and result[0]:
178
- file_path = result[0][0].get('image')
179
- if file_path:
180
- # Combine base URL with file path to create full URL
181
- base_url = "https://measmonysuon-imagen.hf.space/file="
182
- full_file_url = base_url + file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
- if os.path.exists(file_path):
185
- update_user_points(user_chat_id, -5)
186
- notify_webhook_with_file(user_chat_id, full_file_url) # Notify webhook with full file URL
187
- return full_file_url, "Check your telegram, The image was generated successfully."
188
  return None, "The image file is not available. Please try again later."
189
- return None, "There was an error processing your photo. Please try again later."
 
 
 
 
 
190
 
191
  def handle_generate_image(prompt, resolution_key, user_chat_id):
192
  points = get_user_points(user_chat_id)
 
16
  # Set your token
17
  login(token=API_TOKEN, add_to_git_credential=True)
18
  # Initialize API client
19
+ api = HfApi(token=API_TOKEN)
20
 
21
  # Initialize the client for image generation "prithivMLmods/IMAGINEO-4K" "mukaist/DALLE-4K"
22
  client_image = Client("measmonysuon/DALLE-4K")
 
39
  logger = logging.getLogger(__name__)
40
 
41
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
 
42
  resolution = resolutions.get(resolution_key, (1024, 1024))
43
  width, height = resolution
44
  full_prompt = f"{prompt}, Canon EOS R5, 4K, Photo-Realistic, appearing photorealistic with super fine details, high resolution, natural look, hyper realistic photography, cinematic lighting, --ar 64:37, --v 6.0, --style raw, --stylize 750"
 
57
  api_name="/run"
58
  )
59
  logger.info("Image generation successful.")
 
60
  return result
61
  except Exception as e:
62
  logger.error(f"Error generating image: {e}")
 
63
  return None
64
 
65
  def request_otp(user_chat_id):
 
141
  except requests.RequestException as e:
142
  logger.error(f"Error sending webhook notification: {e}")
143
 
144
+ def notify_webhook_with_file(user_chat_id, full_file_url, gpu_info):
145
  webhook_url = f"{webhook_server}/notify_image"
146
+ payload = {
147
+ 'user_chat_id': user_chat_id,
148
+ 'file_path': full_file_url,
149
+ 'gpu_info': gpu_info
150
+ }
151
  try:
152
  response = requests.post(webhook_url, json=payload)
153
  response.raise_for_status()
 
173
  def gradio_interface(prompt, resolution_key, user_chat_id):
174
  if not user_chat_id.strip():
175
  return None, "User Chat ID cannot be blank. Please provide a valid User Chat ID or register with @supportBot to get UID."
176
+
177
+ # Request large hardware for image generation
178
+ api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='a10g-large')
179
 
180
+ # Get GPU info
181
+ gpu_info = f"Is CUDA available: {torch.cuda.is_available()}"
182
+
183
+ try:
184
+ # Generate the image
185
+ result = generate_image(prompt, resolution_key)
186
+
187
+ if result and result[0]:
188
+ file_path = result[0][0].get('image')
189
+ if file_path:
190
+ # Combine base URL with file path to create full URL
191
+ base_url = "https://measmonysuon-imagen.hf.space/file="
192
+ full_file_url = base_url + file_path
193
+
194
+ if os.path.exists(file_path):
195
+ # Update user points and notify webhook
196
+ update_user_points(user_chat_id, -5)
197
+ notify_webhook_with_file(user_chat_id, full_file_url, gpu_info) # Notify webhook with full file URL
198
+
199
+ return full_file_url, f"Check your telegram, The image was generated successfully. {gpu_info}"
200
+
201
+ return None, "The image file is not available. Please try again later."
202
 
 
 
 
 
203
  return None, "The image file is not available. Please try again later."
204
+
205
+ return None, "There was an error processing your photo. Please try again later."
206
+
207
+ finally:
208
+ # Always request to switch back hardware
209
+ api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='zero-a10g')
210
 
211
  def handle_generate_image(prompt, resolution_key, user_chat_id):
212
  points = get_user_points(user_chat_id)