measmonysuon commited on
Commit
ec175b4
1 Parent(s): 4644a25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -22
app.py CHANGED
@@ -3,7 +3,6 @@ from gradio_client import Client
3
  import os
4
  import logging
5
  import requests
6
- from urllib.parse import urlparse, parse_qs
7
 
8
  webhook_server = os.getenv('webhook_server')
9
 
@@ -48,39 +47,32 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
48
 
49
  def request_otp(user_chat_id):
50
  try:
51
- # Make the request to the /request_otp endpoint
52
  response = requests.post(f"{webhook_server}/request_otp", json={"user_chat_id": user_chat_id})
53
- response.raise_for_status() # Ensure we raise an exception for HTTP errors
54
 
55
- # Log the response details
56
  logger.debug(f"Response status code: {response.status_code}")
57
  logger.debug(f"Response content: {response.text}")
58
 
59
- # Parse and return the JSON response
60
  response_data = response.json()
61
 
62
- # Check if the response contains an error
63
  if 'error' in response_data:
64
  logger.error(f"Error response from /request_otp: {response_data['error']}")
65
  return {"success": False, "error": response_data.get("error", "Unknown error")}
66
 
67
- # Return success response
68
  return {"success": response_data.get("success", False)}
69
 
70
  except requests.RequestException as e:
71
- # Log request exceptions and return an error response
72
  logger.error(f"Request exception: {e}")
73
  return {"success": False, "error": "Request exception occurred"}
74
 
75
  except ValueError as e:
76
- # Handle JSON decode errors
77
  logger.error(f"JSON decode error: {e}")
78
  return {"success": False, "error": "Invalid JSON response"}
79
 
80
  def verify_otp(user_chat_id, otp):
81
  try:
82
  response = requests.post(f"{webhook_server}/verify_otp", json={"user_chat_id": user_chat_id, "otp": otp})
83
- response.raise_for_status() # Raise an exception for HTTP errors
84
  return response.json().get("valid", False)
85
  except requests.RequestException as e:
86
  logger.error(f"Request exception: {e}")
@@ -90,11 +82,9 @@ def verify_otp(user_chat_id, otp):
90
  return False
91
 
92
  def retrieve_user_points(user_chat_id, otp):
93
- # Verify OTP via Flask server
94
  if not verify_otp(user_chat_id, otp):
95
  return "Invalid or expired OTP. Please request a new OTP."
96
 
97
- # Retrieve user points via Flask server
98
  response = requests.get(f"{webhook_server}/get_points", params={"user_chat_id": user_chat_id})
99
  if response.status_code == 200:
100
  data = response.json()
@@ -134,11 +124,6 @@ def notify_webhook(user_chat_id):
134
  except requests.RequestException as e:
135
  logger.error(f"Error sending webhook notification: {e}")
136
 
137
- def extract_user_chat_id_from_url(url):
138
- parsed_url = urlparse(url)
139
- params = parse_qs(parsed_url.query)
140
- return params.get('user_chat_id', ["Guest"])[0]
141
-
142
  def gradio_interface(prompt, resolution_key, user_chat_id):
143
  if not user_chat_id.strip():
144
  return None, "User Chat ID cannot be blank. Please provide a valid User Chat ID."
@@ -147,7 +132,7 @@ def gradio_interface(prompt, resolution_key, user_chat_id):
147
  if result and result[0]:
148
  file_path = result[0][0].get('image')
149
  if file_path and os.path.exists(file_path):
150
- update_user_points(user_chat_id, -5) # Deduct points for each image generated
151
  return file_path, "The image was generated successfully."
152
  return None, "The image file is not available. Please try again later."
153
  return None, "There was an error processing your photo. Please try again later."
@@ -229,11 +214,8 @@ def create_gradio_interface():
229
  return interface
230
 
231
  def launch_gradio():
232
- url = 'https://measmonysuon-imagen.hf.space/?user_chat_id=00000000'
233
- user_chat_id = extract_user_chat_id_from_url(url)
234
-
235
  interface = create_gradio_interface()
236
- interface.launch()
237
 
238
  if __name__ == '__main__':
239
  launch_gradio()
 
3
  import os
4
  import logging
5
  import requests
 
6
 
7
  webhook_server = os.getenv('webhook_server')
8
 
 
47
 
48
  def request_otp(user_chat_id):
49
  try:
 
50
  response = requests.post(f"{webhook_server}/request_otp", json={"user_chat_id": user_chat_id})
51
+ response.raise_for_status()
52
 
 
53
  logger.debug(f"Response status code: {response.status_code}")
54
  logger.debug(f"Response content: {response.text}")
55
 
 
56
  response_data = response.json()
57
 
 
58
  if 'error' in response_data:
59
  logger.error(f"Error response from /request_otp: {response_data['error']}")
60
  return {"success": False, "error": response_data.get("error", "Unknown error")}
61
 
 
62
  return {"success": response_data.get("success", False)}
63
 
64
  except requests.RequestException as e:
 
65
  logger.error(f"Request exception: {e}")
66
  return {"success": False, "error": "Request exception occurred"}
67
 
68
  except ValueError as e:
 
69
  logger.error(f"JSON decode error: {e}")
70
  return {"success": False, "error": "Invalid JSON response"}
71
 
72
  def verify_otp(user_chat_id, otp):
73
  try:
74
  response = requests.post(f"{webhook_server}/verify_otp", json={"user_chat_id": user_chat_id, "otp": otp})
75
+ response.raise_for_status()
76
  return response.json().get("valid", False)
77
  except requests.RequestException as e:
78
  logger.error(f"Request exception: {e}")
 
82
  return False
83
 
84
  def retrieve_user_points(user_chat_id, otp):
 
85
  if not verify_otp(user_chat_id, otp):
86
  return "Invalid or expired OTP. Please request a new OTP."
87
 
 
88
  response = requests.get(f"{webhook_server}/get_points", params={"user_chat_id": user_chat_id})
89
  if response.status_code == 200:
90
  data = response.json()
 
124
  except requests.RequestException as e:
125
  logger.error(f"Error sending webhook notification: {e}")
126
 
 
 
 
 
 
127
  def gradio_interface(prompt, resolution_key, user_chat_id):
128
  if not user_chat_id.strip():
129
  return None, "User Chat ID cannot be blank. Please provide a valid User Chat ID."
 
132
  if result and result[0]:
133
  file_path = result[0][0].get('image')
134
  if file_path and os.path.exists(file_path):
135
+ update_user_points(user_chat_id, -5)
136
  return file_path, "The image was generated successfully."
137
  return None, "The image file is not available. Please try again later."
138
  return None, "There was an error processing your photo. Please try again later."
 
214
  return interface
215
 
216
  def launch_gradio():
 
 
 
217
  interface = create_gradio_interface()
218
+ interface.launch(show_error=True) # Enable verbose error reporting
219
 
220
  if __name__ == '__main__':
221
  launch_gradio()