measmonysuon commited on
Commit
cf24dc7
·
verified ·
1 Parent(s): 2d02379

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import gradio as gr
3
+ from gradio_client import Client
4
+ import os
5
+ import logging
6
+
7
+ # Initialize the client for image generation
8
+ client_image = Client("mukaist/DALLE-4K")
9
+
10
+ # Define resolutions
11
+ resolutions = {
12
+ "896x1152": (896, 1152),
13
+ "1024x1024": (1024, 1024),
14
+ "1216x832": (1216, 832)
15
+ }
16
+
17
+ # Define the default style
18
+ DEFAULT_STYLE = "3840 x 2160"
19
+
20
+ # Set up logging
21
+ logging.basicConfig(level=logging.INFO)
22
+ logger = logging.getLogger(__name__)
23
+
24
+ def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
25
+ resolution = resolutions.get(resolution_key, (1024, 1024))
26
+ width, height = resolution
27
+ 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"
28
+ try:
29
+ result = client_image.predict(
30
+ prompt=full_prompt,
31
+ 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",
32
+ use_negative_prompt=True,
33
+ style=style,
34
+ seed=0,
35
+ width=width,
36
+ height=height,
37
+ guidance_scale=5,
38
+ randomize_seed=True,
39
+ api_name="/run"
40
+ )
41
+ logger.info("Image generation successful.")
42
+ return result
43
+ except Exception as e:
44
+ logger.error(f"Error generating image: {e}")
45
+ return None
46
+
47
+ def gradio_interface(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, "The image was generated successfully."
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(username):
60
+ description = f"Welcome, {username}! Generate images based on prompts."
61
+
62
+ with gr.Blocks() as interface:
63
+ gr.HTML(f"<h3>{description}</h3>")
64
+
65
+ prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
66
+ resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024")
67
+ generate_button = gr.Button("Generate")
68
+
69
+ result_output = gr.Image(label="Generated Image", type="pil")
70
+ message_output = gr.Textbox(label="Result", placeholder="Results will be shown here", interactive=False)
71
+
72
+ generate_button.click(fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key),
73
+ inputs=[prompt_input, resolution_dropdown],
74
+ outputs=[result_output, message_output])
75
+
76
+ # Add custom CSS to hide the specific footer element
77
+ gr.HTML("""
78
+ <style>
79
+ footer.svelte-1rjryqp {
80
+ display: none !important;
81
+ }
82
+ </style>
83
+ """)
84
+
85
+ return interface
86
+
87
+ def launch_gradio(username=None):
88
+ if not username:
89
+ username = "Guest"
90
+
91
+ # Launch the Gradio interface
92
+ interface = create_gradio_interface(username)
93
+ interface.launch()
94
+
95
+ if __name__ == "__main__":
96
+ # Start the Gradio interface
97
+ launch_gradio(username="Guest")
98
+
99
+ # Start the Telegram bot
100
+ subprocess.Popen(["python3", "telegrabot.py"])