measmonysuon commited on
Commit
f8957c5
1 Parent(s): 1d33068

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from gradio_client import Client
3
  import os
4
  import logging
 
5
 
6
  # Initialize the client for image generation
7
  client_image = Client("mukaist/DALLE-4K")
@@ -55,8 +56,22 @@ def gradio_interface(prompt, resolution_key):
55
  else:
56
  return None, "There was an error processing your photo. Please try again later."
57
 
58
- def create_gradio_interface():
59
  with gr.Blocks() as interface:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
61
  resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
62
  generate_button = gr.Button("Generate")
@@ -68,7 +83,6 @@ def create_gradio_interface():
68
  inputs=[prompt_input, resolution_dropdown],
69
  outputs=[result_output, message_output])
70
 
71
- # Add custom CSS to hide the specific footer element
72
  gr.HTML("""
73
  <style>
74
  footer.svelte-1rjryqp {
@@ -79,10 +93,23 @@ def create_gradio_interface():
79
 
80
  return interface
81
 
82
- def launch_gradio():
83
  # Launch the Gradio interface
84
- interface = create_gradio_interface()
85
  interface.launch()
86
 
87
- if __name__ == "__main__":
88
- launch_gradio()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from gradio_client import Client
3
  import os
4
  import logging
5
+ from urllib.parse import parse_qs, urlparse
6
 
7
  # Initialize the client for image generation
8
  client_image = Client("mukaist/DALLE-4K")
 
56
  else:
57
  return None, "There was an error processing your photo. Please try again later."
58
 
59
+ def create_gradio_interface(username):
60
  with gr.Blocks() as interface:
61
+ gr.HTML(f"""
62
+ <script src="https://telegram.org/js/telegram-web-app.js"></script>
63
+ <script>
64
+ document.addEventListener('DOMContentLoaded', function() {{
65
+ window.Telegram.WebApp.init();
66
+ window.Telegram.WebApp.setTitle("Welcome, {username}!");
67
+ document.getElementById('my-button').addEventListener('click', function() {{
68
+ window.Telegram.WebApp.close();
69
+ }});
70
+ }});
71
+ </script>
72
+ <h3>Welcome, {username}! Generate images based on prompts.</h3>
73
+ """)
74
+
75
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
76
  resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
77
  generate_button = gr.Button("Generate")
 
83
  inputs=[prompt_input, resolution_dropdown],
84
  outputs=[result_output, message_output])
85
 
 
86
  gr.HTML("""
87
  <style>
88
  footer.svelte-1rjryqp {
 
93
 
94
  return interface
95
 
96
+ def launch_gradio(username):
97
  # Launch the Gradio interface
98
+ interface = create_gradio_interface(username)
99
  interface.launch()
100
 
101
+ def extract_username_from_url(url):
102
+ parsed_url = urlparse(url)
103
+ params = parse_qs(parsed_url.query)
104
+ return params.get('username', ["Guest"])[0]
105
+
106
+ def get_username_from_url():
107
+ # Placeholder URL, replace with actual URL or method to get URL in deployment
108
+ url = 'https://measmonysuon-webapp.hf.space/?username=Guest'
109
+ username = extract_username_from_url(url)
110
+ logger.info(f"Extracted username: {username}")
111
+ return username
112
+
113
+ # Launch Gradio with the extracted or default username
114
+ username = get_username_from_url()
115
+ launch_gradio(username)