measmonysuon commited on
Commit
a2d5af8
1 Parent(s): b84a77d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -1,8 +1,10 @@
 
1
  import gradio as gr
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")
@@ -21,14 +23,42 @@ DEFAULT_STYLE = "3840 x 2160"
21
  logging.basicConfig(level=logging.INFO)
22
  logger = logging.getLogger(__name__)
23
 
24
- # Helper function to extract username from URL
25
- def extract_username_from_url(url):
26
- parsed_url = urlparse(url)
27
- params = parse_qs(parsed_url.query)
28
- logger.debug(f"Parsed URL parameters: {params}") # Debugging information
29
- return params.get('username', ["Guest"])[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # Define the Gradio app
32
  def create_gradio_interface(username):
33
  with gr.Blocks() as interface:
34
  # Personalized HTML content
@@ -52,19 +82,6 @@ def create_gradio_interface(username):
52
  result_output = gr.Image(label="Generated Image", type="pil")
53
  message_output = gr.Textbox(label="Result", placeholder="Results will be shown here", interactive=False)
54
 
55
- # Define the function for image generation
56
- def gradio_interface(prompt, resolution_key):
57
- result = generate_image(prompt, resolution_key)
58
-
59
- if result and result[0]:
60
- file_path = result[0][0].get('image')
61
- if file_path and os.path.exists(file_path):
62
- return file_path, "The image was generated successfully."
63
- else:
64
- return None, "The image file is not available. Please try again later."
65
- else:
66
- return None, "There was an error processing your photo. Please try again later."
67
-
68
  # Set up interaction
69
  generate_button.click(
70
  fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key),
@@ -82,13 +99,12 @@ def create_gradio_interface(username):
82
 
83
  return interface
84
 
85
- # Launch Gradio app
86
- def launch_gradio(username):
 
 
87
  interface = create_gradio_interface(username)
88
- interface.launch()
89
 
90
- # Example URL (this should be dynamically obtained in your use case)
91
- url = 'https://measmonysuon-imagen.hf.space/?username=Guest_001'
92
- username = extract_username_from_url(url)
93
- logger.info(f"Extracted username: {username}")
94
- launch_gradio(username)
 
1
+ from flask import Flask, request, redirect
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")
 
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
29
+ full_prompt = f"{prompt}, Canon EOS R5, 4K, Photo-Realistic, appearing photorealistic with super fine details, high resolution, natural look, hyper realistic photography, cinematic lighting, --ar 64:37, --v 6.0, --style raw, --stylize 750"
30
+
31
+ try:
32
+ result = client_image.predict(
33
+ prompt=full_prompt,
34
+ negative_prompt="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
35
+ use_negative_prompt=True,
36
+ style=style,
37
+ seed=0,
38
+ width=width,
39
+ height=height,
40
+ guidance_scale=5,
41
+ randomize_seed=True,
42
+ api_name="/run"
43
+ )
44
+ logger.info("Image generation successful.")
45
+ return result
46
+ except Exception as e:
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
 
82
  result_output = gr.Image(label="Generated Image", type="pil")
83
  message_output = gr.Textbox(label="Result", placeholder="Results will be shown here", interactive=False)
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  # Set up interaction
86
  generate_button.click(
87
  fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key),
 
99
 
100
  return interface
101
 
102
+ @app.route('/gradio')
103
+ def serve_gradio():
104
+ username = request.args.get('username', 'Guest')
105
+ logger.info(f"Extracted username: {username}")
106
  interface = create_gradio_interface(username)
107
+ return interface.launch(inline=True)
108
 
109
+ if __name__ == '__main__':
110
+ app.run(debug=True)