measmonysuon commited on
Commit
af1f292
1 Parent(s): 318fb32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -61
app.py CHANGED
@@ -18,6 +18,10 @@ 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")
23
 
@@ -34,10 +38,6 @@ resolutions = {
34
  }
35
  DEFAULT_STYLE = "3840 x 2160"
36
 
37
- # Set up logging
38
- logging.basicConfig(level=logging.INFO)
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
@@ -61,7 +61,77 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
61
  except Exception as e:
62
  logger.error(f"Error generating image: {e}")
63
  return None
 
 
 
 
 
 
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def request_otp(user_chat_id):
66
  try:
67
  response = requests.post(f"{webhook_server}/request_otp", json={"user_chat_id": user_chat_id})
@@ -170,63 +240,6 @@ def gradio_interface(prompt, resolution_key, user_chat_id):
170
  return None, "There was an error processing your photo. Please try again later."
171
  '''
172
 
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='cpu-basic')
210
-
211
- def handle_generate_image(prompt, resolution_key, user_chat_id):
212
- points = get_user_points(user_chat_id)
213
- if isinstance(points, str):
214
- if points == "User not found":
215
- return None, "Please register with https://t.me/myexpsupportBot"
216
- return None, "Error retrieving points. Please try again later."
217
-
218
- try:
219
- points = int(points)
220
- except ValueError:
221
- return None, "Error processing points. Please try again later."
222
-
223
- if points >= 5:
224
- result = gradio_interface(prompt, resolution_key, user_chat_id)
225
- if result[0]:
226
- return result[0], "The image was generated successfully."
227
- return None, "There was an error processing your photo. Please try again later."
228
- return None, "Insufficient points. Please get more points before generating an image."
229
-
230
  def request_otp_and_update_ui(user_chat_id):
231
  # Validate user_chat_id
232
  if not user_chat_id.strip():
 
18
  # Initialize API client
19
  api = HfApi(token=API_TOKEN)
20
 
21
+ # Set up logging
22
+ logging.basicConfig(level=logging.INFO)
23
+ logger = logging.getLogger(__name__)
24
+
25
  # Initialize the client for image generation "prithivMLmods/IMAGINEO-4K" "mukaist/DALLE-4K"
26
  client_image = Client("measmonysuon/DALLE-4K")
27
 
 
38
  }
39
  DEFAULT_STYLE = "3840 x 2160"
40
 
 
 
 
 
41
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
42
  resolution = resolutions.get(resolution_key, (1024, 1024))
43
  width, height = resolution
 
61
  except Exception as e:
62
  logger.error(f"Error generating image: {e}")
63
  return None
64
+
65
+ def gradio_interface(prompt, resolution_key, user_chat_id):
66
+ if not user_chat_id.strip():
67
+ return None, "User Chat ID cannot be blank. Please provide a valid User Chat ID or register with @supportBot to get UID."
68
+
69
+ # Request large hardware for image generation
70
+ api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='a10g-large')
71
 
72
+ # Get GPU info
73
+ gpu_info = {
74
+ "is_cuda_available": torch.cuda.is_available(),
75
+ "device_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "N/A",
76
+ "total_memory": torch.cuda.get_device_properties(0).total_memory if torch.cuda.is_available() else "N/A"
77
+ }
78
+
79
+ try:
80
+ # Generate the image
81
+ result = generate_image(prompt, resolution_key)
82
+
83
+ if result and result[0]:
84
+ file_path = result[0][0].get('image')
85
+ if file_path:
86
+ # Combine base URL with file path to create full URL
87
+ base_url = "https://measmonysuon-imagen.hf.space/file="
88
+ full_file_url = base_url + file_path
89
+
90
+ if os.path.exists(file_path):
91
+ # Update user points and notify webhook
92
+ update_user_points(user_chat_id, -5)
93
+ notify_webhook_with_file(user_chat_id, full_file_url, gpu_info) # Notify webhook with full file URL
94
+
95
+ return full_file_url, f"Check your telegram, The image was generated successfully. {gpu_info}"
96
+
97
+ return None, "The image file is not available. Please try again later."
98
+
99
+ return None, "The image file is not available. Please try again later."
100
+
101
+ return None, "There was an error processing your photo. Please try again later."
102
+
103
+ finally:
104
+ # Log GPU info before resetting hardware
105
+ logger.info(f"GPU info before reset: {gpu_info}")
106
+
107
+ # Always request to switch back hardware
108
+ api.request_space_hardware(repo_id='measmonysuon/DALLE-4K', hardware='zero-a10g')
109
+ logger.info("Hardware reset to Zero-A10G")
110
+
111
+ def handle_generate_image(prompt, resolution_key, user_chat_id):
112
+ points = get_user_points(user_chat_id)
113
+
114
+ if isinstance(points, str):
115
+ if points == "User not found":
116
+ return None, "Please register with https://t.me/myexpsupportBot"
117
+ return None, "Error retrieving points. Please try again later."
118
+
119
+ try:
120
+ points = int(points)
121
+ except ValueError:
122
+ return None, "Error processing points. Please try again later."
123
+
124
+ if points >= 5:
125
+ result = gradio_interface(prompt, resolution_key, user_chat_id)
126
+ if result[0]:
127
+ # Include the GPU info in the success message
128
+ gpu_info = result[1] # Assuming gradio_interface returns GPU info in the second part of the tuple
129
+ return result[0], f"Check your telegram, The image was generated successfully. {gpu_info}"
130
+
131
+ return None, "There was an error processing your photo. Please try again later."
132
+
133
+ return None, "Insufficient points. Please get more points before generating an image."
134
+
135
  def request_otp(user_chat_id):
136
  try:
137
  response = requests.post(f"{webhook_server}/request_otp", json={"user_chat_id": user_chat_id})
 
240
  return None, "There was an error processing your photo. Please try again later."
241
  '''
242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  def request_otp_and_update_ui(user_chat_id):
244
  # Validate user_chat_id
245
  if not user_chat_id.strip():