measmonysuon commited on
Commit
94e298f
1 Parent(s): 85ddfa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from gradio_client import Client
3
  import os
4
  import logging
5
- import sqlite3
6
  import requests
7
  from urllib.parse import urlparse, parse_qs
8
 
@@ -51,20 +50,24 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
51
  return None
52
 
53
  def update_user_points(user_chat_id, points):
54
- """Updates the user's points in the database."""
55
- with sqlite3.connect(db_path) as conn:
56
- cursor = conn.cursor()
57
- cursor.execute('''
58
- INSERT INTO users (user_chat_id, points) VALUES (?, ?)
59
- ON CONFLICT(user_chat_id) DO UPDATE SET points = points + ?
60
- ''', (user_chat_id, points, points))
61
- conn.commit()
62
- logger.info(f"User with ID {user_chat_id} had their points updated by {points}.")
 
 
 
 
63
 
64
  def notify_webhook(user_chat_id):
65
  """Notify the webhook server about the points update."""
66
- webhook_url = f"{webhook_server}/update_points"
67
- payload = {'user_chat_id': user_chat_id, 'points': 10} # Adjust payload as necessary
68
  try:
69
  response = requests.post(webhook_url, json=payload)
70
  response.raise_for_status()
@@ -87,7 +90,7 @@ def get_user_points(user_chat_id):
87
  def extract_user_chat_id_from_url(url):
88
  parsed_url = urlparse(url)
89
  params = parse_qs(parsed_url.query)
90
- return params.get('user_chat_id', ["Guest"])[0]
91
 
92
  def gradio_interface(prompt, resolution_key, user_chat_id):
93
  result = generate_image(prompt, resolution_key)
@@ -103,7 +106,7 @@ def gradio_interface(prompt, resolution_key, user_chat_id):
103
  else:
104
  return None, "There was an error processing your photo. Please try again later."
105
 
106
- def create_gradio_interface():
107
  with gr.Blocks() as interface:
108
  # Personalized HTML content
109
  gr.HTML("""
@@ -111,7 +114,7 @@ def create_gradio_interface():
111
  """)
112
 
113
  # Create input components
114
- user_chat_id_input = gr.Textbox(label="User Chat ID", placeholder="Enter your user chat ID here...")
115
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
116
 
117
  # Create the button to get user points
@@ -178,9 +181,11 @@ def create_gradio_interface():
178
  return interface
179
 
180
  def launch_gradio():
181
- interface = create_gradio_interface()
 
 
 
182
  interface.launch()
183
 
184
  if __name__ == '__main__':
185
- # Launch Gradio
186
  launch_gradio()
 
2
  from gradio_client import Client
3
  import os
4
  import logging
 
5
  import requests
6
  from urllib.parse import urlparse, parse_qs
7
 
 
50
  return None
51
 
52
  def update_user_points(user_chat_id, points):
53
+ """Updates the user's points using a webhook."""
54
+ webhook_url = f"{webhook_server}/update_points"
55
+ payload = {
56
+ 'user_chat_id': user_chat_id,
57
+ 'points': points
58
+ }
59
+
60
+ try:
61
+ response = requests.post(webhook_url, json=payload)
62
+ response.raise_for_status()
63
+ logger.info(f"User with ID {user_chat_id} had their points updated by {points}. Webhook response: {response.status_code}")
64
+ except requests.RequestException as e:
65
+ logger.error(f"Error updating user points via webhook: {e}")
66
 
67
  def notify_webhook(user_chat_id):
68
  """Notify the webhook server about the points update."""
69
+ webhook_url = f"{webhook_server}/notify_update"
70
+ payload = {'user_chat_id': user_chat_id}
71
  try:
72
  response = requests.post(webhook_url, json=payload)
73
  response.raise_for_status()
 
90
  def extract_user_chat_id_from_url(url):
91
  parsed_url = urlparse(url)
92
  params = parse_qs(parsed_url.query)
93
+ return params.get('user_chat_id', ["Guest"])[0]]
94
 
95
  def gradio_interface(prompt, resolution_key, user_chat_id):
96
  result = generate_image(prompt, resolution_key)
 
106
  else:
107
  return None, "There was an error processing your photo. Please try again later."
108
 
109
+ def create_gradio_interface(user_chat_id):
110
  with gr.Blocks() as interface:
111
  # Personalized HTML content
112
  gr.HTML("""
 
114
  """)
115
 
116
  # Create input components
117
+ user_chat_id_input = gr.Textbox(label="User Chat ID", placeholder="Enter your user chat ID here...", value=user_chat_id)
118
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
119
 
120
  # Create the button to get user points
 
181
  return interface
182
 
183
  def launch_gradio():
184
+ # Dynamically obtain the URL, e.g., from an environment variable
185
+ url = os.getenv('USER_CHAT_ID_URL', 'http://example.com?user_chat_id=Guest')
186
+ user_chat_id = extract_user_chat_id_from_url(url)
187
+ interface = create_gradio_interface(user_chat_id)
188
  interface.launch()
189
 
190
  if __name__ == '__main__':
 
191
  launch_gradio()