measmonysuon commited on
Commit
f65a0cb
1 Parent(s): 824eec3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -43
app.py CHANGED
@@ -76,20 +76,15 @@ def notify_webhook(user_chat_id):
76
  logger.error(f"Error sending webhook notification: {e}")
77
 
78
  def get_user_points(user_chat_id):
79
- """Retrieve user points from the Flask server."""
80
- webhook_url = f"{webhook_server}/get_points"
81
- params = {'user_chat_id': user_chat_id}
82
- try:
83
- response = requests.get(webhook_url, params=params)
84
- response.raise_for_status()
85
- data = response.json()
86
- if 'points' in data:
87
- return data['points']
88
  else:
89
- return None # User not found or points not in response
90
- except requests.RequestException as e:
91
- logger.error(f"Error fetching user points: {e}")
92
- return None
93
 
94
  def extract_user_chat_id_from_url(url):
95
  parsed_url = urlparse(url)
@@ -110,6 +105,23 @@ def gradio_interface(prompt, resolution_key, user_chat_id):
110
  else:
111
  return None, "There was an error processing your photo. Please try again later."
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  def create_gradio_interface(user_chat_id):
114
  with gr.Blocks() as interface:
115
  # Personalized HTML content
@@ -118,12 +130,14 @@ def create_gradio_interface(user_chat_id):
118
  """)
119
 
120
  # Create input components
121
- user_chat_id_input = gr.Textbox(label="User Chat ID", placeholder="Enter your user chat ID here...", value=user_chat_id, interactive=False) # Make the textbox non-editable
 
 
 
 
 
122
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
123
 
124
- # Create the button to get user points
125
- get_points_button = gr.Button("Get Points")
126
-
127
  # Create other components
128
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
129
  resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
@@ -136,44 +150,19 @@ def create_gradio_interface(user_chat_id):
136
  with gr.Row():
137
  user_chat_id_input
138
  points_output
139
- get_points_button
140
 
141
  with gr.Row():
142
  prompt_input
143
  resolution_dropdown
144
  generate_button
145
 
146
- def handle_generate_image(prompt, resolution_key, user_chat_id):
147
- points = get_user_points(user_chat_id)
148
-
149
- # Check if user_chat_id is found in the database
150
- if points is None:
151
- return None, "User not found. Please register with @myexpsupportBot."
152
-
153
- # Check if the user has enough points
154
- if points >= 5:
155
- result = gradio_interface(prompt, resolution_key, user_chat_id)
156
- if result[0]: # If image generation is successful
157
- return result[0], "The image was generated successfully."
158
- else:
159
- return None, "There was an error processing your photo. Please try again later."
160
- else:
161
- return None, "Insufficient points. Please get more points before generating an image."
162
-
163
  # Set up interactions
164
  generate_button.click(
165
  fn=handle_generate_image,
166
  inputs=[prompt_input, resolution_dropdown, user_chat_id_input],
167
  outputs=[result_output, message_output]
168
  )
169
-
170
- # Remove the get_points_button related interaction if it's not needed anymore
171
- get_points_button.click(
172
- fn=lambda user_chat_id: get_user_points(user_chat_id),
173
- inputs=[user_chat_id_input],
174
- outputs=points_output
175
- )
176
-
177
  gr.HTML("""
178
  <style>
179
  footer.svelte-1rjryqp {
 
76
  logger.error(f"Error sending webhook notification: {e}")
77
 
78
  def get_user_points(user_chat_id):
79
+ """Fetches user points from the database. Returns 'User not found' if the user is not in the database."""
80
+ with sqlite3.connect(db_path) as conn:
81
+ cursor = conn.cursor()
82
+ cursor.execute('SELECT points FROM users WHERE user_chat_id = ?', (user_chat_id,))
83
+ result = cursor.fetchone()
84
+ if result:
85
+ return result[0] # Return the points
 
 
86
  else:
87
+ return "User not found" # Return an error message if user is not found
 
 
 
88
 
89
  def extract_user_chat_id_from_url(url):
90
  parsed_url = urlparse(url)
 
105
  else:
106
  return None, "There was an error processing your photo. Please try again later."
107
 
108
+ def handle_generate_image(prompt, resolution_key, user_chat_id):
109
+ points = get_user_points(user_chat_id)
110
+
111
+ if points == "User not found":
112
+ # If user is not found, update points_output with the registration message
113
+ return None, "Please register with @myexpsupportBot"
114
+
115
+ if points >= 5:
116
+ result = gradio_interface(prompt, resolution_key, user_chat_id)
117
+ if result[0]: # If image generation is successful
118
+ return result[0], "The image was generated successfully."
119
+ else:
120
+ return None, "There was an error processing your photo. Please try again later."
121
+ else:
122
+ return None, "Insufficient points. Please get more points before generating an image."
123
+
124
+
125
  def create_gradio_interface(user_chat_id):
126
  with gr.Blocks() as interface:
127
  # Personalized HTML content
 
130
  """)
131
 
132
  # Create input components
133
+ user_chat_id_input = gr.Textbox(
134
+ label="User Chat ID",
135
+ placeholder="Enter your user chat ID here...",
136
+ value=user_chat_id,
137
+ interactive=False # Make the textbox non-editable
138
+ )
139
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
140
 
 
 
 
141
  # Create other components
142
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
143
  resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
 
150
  with gr.Row():
151
  user_chat_id_input
152
  points_output
 
153
 
154
  with gr.Row():
155
  prompt_input
156
  resolution_dropdown
157
  generate_button
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  # Set up interactions
160
  generate_button.click(
161
  fn=handle_generate_image,
162
  inputs=[prompt_input, resolution_dropdown, user_chat_id_input],
163
  outputs=[result_output, message_output]
164
  )
165
+
 
 
 
 
 
 
 
166
  gr.HTML("""
167
  <style>
168
  footer.svelte-1rjryqp {