measmonysuon commited on
Commit
f5bcaca
1 Parent(s): 91bad7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -30
app.py CHANGED
@@ -1,11 +1,8 @@
1
- from flask import Flask, request, jsonify
2
  import gradio as gr
3
  from gradio_client import Client
4
  import os
5
  import logging
6
 
7
- app = Flask(__name__)
8
-
9
  # Initialize the client for image generation
10
  client_image = Client("mukaist/DALLE-4K")
11
 
@@ -47,33 +44,22 @@ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
47
  logger.error(f"Error generating image: {e}")
48
  return None
49
 
50
- def gradio_interface(prompt, resolution_key):
51
  result = generate_image(prompt, resolution_key)
52
 
53
  if result and result[0]:
54
  file_path = result[0][0].get('image')
55
  if file_path and os.path.exists(file_path):
56
- return file_path, "The image was generated successfully."
57
  else:
58
  return None, "The image file is not available. Please try again later."
59
  else:
60
  return None, "There was an error processing your photo. Please try again later."
61
 
62
- def create_gradio_interface(username):
63
  with gr.Blocks() as interface:
64
- # Personalized HTML content
65
- gr.HTML(f"""
66
- <script src="https://telegram.org/js/telegram-web-app.js"></script>
67
- <script>
68
- document.addEventListener('DOMContentLoaded', function() {{
69
- window.Telegram.WebApp.init();
70
- window.Telegram.WebApp.setTitle("Welcome, {username}!");
71
- }});
72
- </script>
73
- <h3>Welcome, {username}! Generate images based on prompts.</h3>
74
- """)
75
-
76
  # Create input components
 
77
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
78
  resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
79
  generate_button = gr.Button("Generate")
@@ -84,8 +70,8 @@ def create_gradio_interface(username):
84
 
85
  # Set up interaction
86
  generate_button.click(
87
- fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key),
88
- inputs=[prompt_input, resolution_dropdown],
89
  outputs=[result_output, message_output]
90
  )
91
 
@@ -99,14 +85,9 @@ def create_gradio_interface(username):
99
 
100
  return interface
101
 
102
- @app.route('/')
103
- def serve_gradio():
104
- username = request.args.get('username', 'Guest')
105
- logger.info(f"Extracted username: {username}")
106
-
107
- # Launch Gradio interface
108
- interface = create_gradio_interface(username)
109
- return gr.Interface.load(interface).launch(inline=True)
110
 
111
- if __name__ == '__main__':
112
- app.run(debug=True)
 
 
1
  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")
8
 
 
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
 
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
 
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()