measmonysuon commited on
Commit
dea0635
1 Parent(s): f5bcaca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -10
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")
@@ -44,22 +45,33 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
44
  logger.error(f"Error generating image: {e}")
45
  return None
46
 
47
- def gradio_interface(username, prompt, resolution_key):
48
  result = generate_image(prompt, resolution_key)
49
 
50
  if result and result[0]:
51
  file_path = result[0][0].get('image')
52
  if file_path and os.path.exists(file_path):
53
- return file_path, f"The image was generated successfully for {username}."
54
  else:
55
  return None, "The image file is not available. Please try again later."
56
  else:
57
  return None, "There was an error processing your photo. Please try again later."
58
 
59
- def create_gradio_interface():
60
  with gr.Blocks() as interface:
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Create input components
62
- username_input = gr.Textbox(label="Username", placeholder="Enter your username here...")
63
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
64
  resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
65
  generate_button = gr.Button("Generate")
@@ -70,8 +82,8 @@ def create_gradio_interface():
70
 
71
  # Set up interaction
72
  generate_button.click(
73
- fn=lambda username, prompt, resolution_key: gradio_interface(username, prompt, resolution_key),
74
- inputs=[username_input, prompt_input, resolution_dropdown],
75
  outputs=[result_output, message_output]
76
  )
77
 
@@ -85,9 +97,23 @@ def create_gradio_interface():
85
 
86
  return interface
87
 
88
- def launch_gradio():
89
- interface = create_gradio_interface()
90
  interface.launch()
91
 
92
- # Launch the Gradio interface
93
- 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")
 
45
  logger.error(f"Error generating image: {e}")
46
  return None
47
 
48
+ def gradio_interface(prompt, resolution_key):
49
  result = generate_image(prompt, resolution_key)
50
 
51
  if result and result[0]:
52
  file_path = result[0][0].get('image')
53
  if file_path and os.path.exists(file_path):
54
+ return file_path, "The image was generated successfully."
55
  else:
56
  return None, "The image file is not available. Please try again later."
57
  else:
58
  return None, "There was an error processing your photo. Please try again later."
59
 
60
+ def create_gradio_interface(username):
61
  with gr.Blocks() as interface:
62
+ # Personalized HTML content
63
+ gr.HTML(f"""
64
+ <script src="https://telegram.org/js/telegram-web-app.js"></script>
65
+ <script>
66
+ document.addEventListener('DOMContentLoaded', function() {{
67
+ window.Telegram.WebApp.init();
68
+ window.Telegram.WebApp.setTitle("Welcome, {username}!");
69
+ }});
70
+ </script>
71
+ <h3>Welcome, {username}! Generate images based on prompts.</h3>
72
+ """)
73
+
74
  # Create input components
 
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")
 
82
 
83
  # Set up interaction
84
  generate_button.click(
85
+ fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key),
86
+ inputs=[prompt_input, resolution_dropdown],
87
  outputs=[result_output, message_output]
88
  )
89
 
 
97
 
98
  return interface
99
 
100
+ def launch_gradio(username):
101
+ interface = create_gradio_interface(username)
102
  interface.launch()
103
 
104
+ def extract_username_from_url(url):
105
+ parsed_url = urlparse(url)
106
+ params = parse_qs(parsed_url.query)
107
+ logger.debug(f"Parsed URL parameters: {params}") # Debugging information
108
+ return params.get('username', ["Guest"])[0]
109
+
110
+ def get_username_from_url(url):
111
+ # Extract username from the actual URL
112
+ username = extract_username_from_url(url)
113
+ logger.info(f"Extracted username: {username}")
114
+ return username
115
+
116
+ # Replace this URL with your actual URL where the Gradio app is hosted
117
+ url = 'https://measmonysuon-imagen.hf.space/?username=measmony' # This should be dynamically obtained if needed
118
+ username = get_username_from_url(url)
119
+ launch_gradio(username)