measmonysuon commited on
Commit
1d33068
1 Parent(s): 69e0a80

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ # Define resolutions
10
+ resolutions = {
11
+ "896x1152": (896, 1152),
12
+ "1024x1024": (1024, 1024),
13
+ "1216x832": (1216, 832)
14
+ }
15
+
16
+ # Define the default style
17
+ DEFAULT_STYLE = "3840 x 2160"
18
+
19
+ # Set up logging
20
+ logging.basicConfig(level=logging.INFO)
21
+ logger = logging.getLogger(__name__)
22
+
23
+ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
24
+ resolution = resolutions.get(resolution_key, (1024, 1024))
25
+ width, height = resolution
26
+ 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"
27
+ try:
28
+ result = client_image.predict(
29
+ prompt=full_prompt,
30
+ 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",
31
+ use_negative_prompt=True,
32
+ style=style,
33
+ seed=0,
34
+ width=width,
35
+ height=height,
36
+ guidance_scale=5,
37
+ randomize_seed=True,
38
+ api_name="/run"
39
+ )
40
+ logger.info("Image generation successful.")
41
+ return result
42
+ except Exception as e:
43
+ logger.error(f"Error generating image: {e}")
44
+ return None
45
+
46
+ def gradio_interface(prompt, resolution_key):
47
+ result = generate_image(prompt, resolution_key)
48
+
49
+ if result and result[0]:
50
+ file_path = result[0][0].get('image')
51
+ if file_path and os.path.exists(file_path):
52
+ return file_path, "The image was generated successfully."
53
+ else:
54
+ return None, "The image file is not available. Please try again later."
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")
63
+
64
+ result_output = gr.Image(label="Generated Image", type="pil")
65
+ message_output = gr.Textbox(label="Result", placeholder="Results will be shown here", interactive=False)
66
+
67
+ generate_button.click(fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key),
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 {
75
+ display: none !important;
76
+ }
77
+ </style>
78
+ """)
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()