measmonysuon commited on
Commit
f35c9d9
1 Parent(s): ff897dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -20
app.py CHANGED
@@ -23,6 +23,9 @@ DEFAULT_STYLE = "3840 x 2160"
23
  logging.basicConfig(level=logging.INFO)
24
  logger = logging.getLogger(__name__)
25
 
 
 
 
26
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
27
  resolution = resolutions.get(resolution_key, (1024, 1024))
28
  width, height = resolution
@@ -47,21 +50,21 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
47
  logger.error(f"Error generating image: {e}")
48
  return None
49
 
50
- def update_user_points(username, points):
51
  """Updates the user's points in the database."""
52
  with sqlite3.connect(db_path) as conn:
53
  cursor = conn.cursor()
54
  cursor.execute('''
55
- INSERT INTO users (username, points) VALUES (?, ?)
56
- ON CONFLICT(username) DO UPDATE SET points = points + ?
57
- ''', (username, points, points))
58
  conn.commit()
59
- logger.info(f"User with username {username} had their points updated by {points}.")
60
 
61
- def notify_webhook(username):
62
  """Notify the webhook server about the points update."""
63
  webhook_url = "http://65.108.76.174:5000/update_points"
64
- payload = {'username': username, 'points': 10} # Adjust payload as necessary
65
  try:
66
  response = requests.post(webhook_url, json=payload)
67
  response.raise_for_status()
@@ -69,10 +72,10 @@ def notify_webhook(username):
69
  except requests.RequestException as e:
70
  logger.error(f"Error sending webhook notification: {e}")
71
 
72
- def get_user_points(username):
73
  """Retrieve user points from the Flask server."""
74
  webhook_url = "http://65.108.76.174:5000/get_points"
75
- params = {'username': username}
76
  try:
77
  response = requests.get(webhook_url, params=params)
78
  response.raise_for_status()
@@ -81,19 +84,19 @@ def get_user_points(username):
81
  logger.error(f"Error fetching user points: {e}")
82
  return "Failed to retrieve user points"
83
 
84
- def extract_username_from_url(url):
85
  parsed_url = urlparse(url)
86
  params = parse_qs(parsed_url.query)
87
- return params.get('username', ["Guest"])[0]
88
 
89
- def gradio_interface(prompt, resolution_key, username):
90
  result = generate_image(prompt, resolution_key)
91
 
92
  if result and result[0]:
93
  file_path = result[0][0].get('image')
94
  if file_path and os.path.exists(file_path):
95
- update_user_points(username, 10) # Award 10 points for each image generated
96
- notify_webhook(username) # Notify the webhook server
97
  return file_path, "The image was generated successfully."
98
  else:
99
  return None, "The image file is not available. Please try again later."
@@ -130,19 +133,19 @@ def create_gradio_interface():
130
  get_points_button = gr.Button("Get Points")
131
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
132
 
133
- # Hidden textbox to hold the username
134
- username_input = gr.Textbox(visible=False, value="Guest", elem_id="username-input")
135
 
136
  # Set up interaction
137
  generate_button.click(
138
- fn=lambda prompt, resolution_key, username: gradio_interface(prompt, resolution_key, username),
139
- inputs=[prompt_input, resolution_dropdown, username_input],
140
  outputs=[result_output, message_output]
141
  )
142
 
143
  get_points_button.click(
144
- fn=lambda username: get_user_points(username),
145
- inputs=[username_input],
146
  outputs=points_output
147
  )
148
 
 
23
  logging.basicConfig(level=logging.INFO)
24
  logger = logging.getLogger(__name__)
25
 
26
+ # Database path
27
+ db_path = 'C:/Users/Administrator/Desktop/TGBoot/photo2prompt_db/users.db'
28
+
29
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
30
  resolution = resolutions.get(resolution_key, (1024, 1024))
31
  width, height = resolution
 
50
  logger.error(f"Error generating image: {e}")
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 user_chat_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 = "http://65.108.76.174:5000/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()
 
72
  except requests.RequestException as e:
73
  logger.error(f"Error sending webhook notification: {e}")
74
 
75
+ def get_user_points(user_chat_id):
76
  """Retrieve user points from the Flask server."""
77
  webhook_url = "http://65.108.76.174:5000/get_points"
78
+ params = {'user_chat_id': user_chat_id}
79
  try:
80
  response = requests.get(webhook_url, params=params)
81
  response.raise_for_status()
 
84
  logger.error(f"Error fetching user points: {e}")
85
  return "Failed to retrieve user points"
86
 
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)
94
 
95
  if result and result[0]:
96
  file_path = result[0][0].get('image')
97
  if file_path and os.path.exists(file_path):
98
+ update_user_points(user_chat_id, 10) # Award 10 points for each image generated
99
+ notify_webhook(user_chat_id) # Notify the webhook server
100
  return file_path, "The image was generated successfully."
101
  else:
102
  return None, "The image file is not available. Please try again later."
 
133
  get_points_button = gr.Button("Get Points")
134
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
135
 
136
+ # Hidden textbox to hold the user_chat_id
137
+ user_chat_id_input = gr.Textbox(visible=False, value="Guest", elem_id="username-input")
138
 
139
  # Set up interaction
140
  generate_button.click(
141
+ fn=lambda prompt, resolution_key, user_chat_id: gradio_interface(prompt, resolution_key, user_chat_id),
142
+ inputs=[prompt_input, resolution_dropdown, user_chat_id_input],
143
  outputs=[result_output, message_output]
144
  )
145
 
146
  get_points_button.click(
147
+ fn=lambda user_chat_id: get_user_points(user_chat_id),
148
+ inputs=[user_chat_id_input],
149
  outputs=points_output
150
  )
151