measmonysuon commited on
Commit
ca3defd
1 Parent(s): 24ea1ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import logging
5
  import sqlite3
6
  import requests
 
7
 
8
  # Initialize the client for image generation
9
  client_image = Client("mukaist/DALLE-4K")
@@ -78,12 +79,18 @@ def get_user_points(username):
78
  try:
79
  response = requests.get(webhook_url, params=params)
80
  response.raise_for_status()
81
- return response.json()
82
  except requests.RequestException as e:
83
  logger.error(f"Error fetching user points: {e}")
84
- return {"error": "Failed to retrieve user points"}
85
 
86
- def gradio_interface(prompt, resolution_key, username):
 
 
 
 
 
 
87
  result = generate_image(prompt, resolution_key)
88
 
89
  if result and result[0]:
@@ -97,18 +104,21 @@ def gradio_interface(prompt, resolution_key, username):
97
  else:
98
  return None, "There was an error processing your photo. Please try again later."
99
 
100
- def create_gradio_interface(username):
101
  with gr.Blocks() as interface:
102
  # Personalized HTML content
103
- gr.HTML(f"""
104
  <script src="https://telegram.org/js/telegram-web-app.js"></script>
105
  <script>
106
- document.addEventListener('DOMContentLoaded', function() {{
107
  window.Telegram.WebApp.init();
108
- window.Telegram.WebApp.setTitle("Welcome, {username}!");
109
- }});
 
 
 
110
  </script>
111
- <h3>Welcome, {username}! Generate images based on prompts.</h3>
112
  """)
113
 
114
  # Create input components
@@ -124,15 +134,19 @@ def create_gradio_interface(username):
124
  get_points_button = gr.Button("Get Points")
125
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
126
 
 
 
 
127
  # Set up interaction
128
  generate_button.click(
129
- fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key, username),
130
- inputs=[prompt_input, resolution_dropdown],
131
  outputs=[result_output, message_output]
132
  )
133
 
134
  get_points_button.click(
135
- fn=lambda: get_user_points(username),
 
136
  outputs=points_output
137
  )
138
 
@@ -146,10 +160,10 @@ def create_gradio_interface(username):
146
 
147
  return interface
148
 
149
- def launch_gradio(username):
150
- interface = create_gradio_interface(username)
151
  interface.launch()
152
 
153
  if __name__ == '__main__':
154
- # Launch Gradio with a default username
155
- launch_gradio("Guest")
 
4
  import logging
5
  import sqlite3
6
  import requests
7
+ from urllib.parse import urlparse, parse_qs
8
 
9
  # Initialize the client for image generation
10
  client_image = Client("mukaist/DALLE-4K")
 
79
  try:
80
  response = requests.get(webhook_url, params=params)
81
  response.raise_for_status()
82
+ return response.json().get('points', "Error retrieving points")
83
  except requests.RequestException as e:
84
  logger.error(f"Error fetching user points: {e}")
85
+ return "Failed to retrieve user points"
86
 
87
+ def extract_username_from_url(url):
88
+ parsed_url = urlparse(url)
89
+ params = parse_qs(parsed_url.query)
90
+ return params.get('username', ["Guest"])[0]
91
+
92
+ def gradio_interface(prompt, resolution_key, url):
93
+ username = extract_username_from_url(url)
94
  result = generate_image(prompt, resolution_key)
95
 
96
  if result and result[0]:
 
104
  else:
105
  return None, "There was an error processing your photo. Please try again later."
106
 
107
+ def create_gradio_interface():
108
  with gr.Blocks() as interface:
109
  # Personalized HTML content
110
+ gr.HTML("""
111
  <script src="https://telegram.org/js/telegram-web-app.js"></script>
112
  <script>
113
+ document.addEventListener('DOMContentLoaded', function() {
114
  window.Telegram.WebApp.init();
115
+ const urlParams = new URLSearchParams(window.location.search);
116
+ const username = urlParams.get('username') || "Guest";
117
+ window.Telegram.WebApp.setTitle(`Welcome, ${username}!`);
118
+ document.getElementById('username').value = username; // Set hidden textbox with username
119
+ });
120
  </script>
121
+ <h3>Welcome! Generate images based on prompts.</h3>
122
  """)
123
 
124
  # Create input components
 
134
  get_points_button = gr.Button("Get Points")
135
  points_output = gr.Textbox(label="User Points", placeholder="User points will be displayed here", interactive=False)
136
 
137
+ # Hidden textbox to hold the username
138
+ username_input = gr.Textbox(visible=False, default="Guest", elem_id="username")
139
+
140
  # Set up interaction
141
  generate_button.click(
142
+ fn=lambda prompt, resolution_key, username: gradio_interface(prompt, resolution_key, username),
143
+ inputs=[prompt_input, resolution_dropdown, username_input],
144
  outputs=[result_output, message_output]
145
  )
146
 
147
  get_points_button.click(
148
+ fn=lambda username: get_user_points(username),
149
+ inputs=[username_input],
150
  outputs=points_output
151
  )
152
 
 
160
 
161
  return interface
162
 
163
+ def launch_gradio():
164
+ interface = create_gradio_interface()
165
  interface.launch()
166
 
167
  if __name__ == '__main__':
168
+ # Launch Gradio
169
+ launch_gradio()