import gradio as gr from gradio_client import Client import os import logging from urllib.parse import parse_qs, urlparse client_image = Client("mukaist/DALLE-4K") resolutions = { "896x1152": (896, 1152), "1024x1024": (1024, 1024), "1216x832": (1216, 832) } DEFAULT_STYLE = "3840 x 2160" logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def generate_image(prompt, resolution_key, style=DEFAULT_STYLE): resolution = resolutions.get(resolution_key, (1024, 1024)) width, height = resolution 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" try: result = client_image.predict( prompt=full_prompt, 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", use_negative_prompt=True, style=style, seed=0, width=width, height=height, guidance_scale=5, randomize_seed=True, api_name="/run" ) logger.info("Image generation successful.") return result except Exception as e: logger.error(f"Error generating image: {e}") return None def gradio_interface(prompt, resolution_key): result = generate_image(prompt, resolution_key) if result and result[0]: file_path = result[0][0].get('image') if file_path and os.path.exists(file_path): return file_path, "The image was generated successfully." else: return None, "The image file is not available. Please try again later." else: return None, "There was an error processing your photo. Please try again later." def create_gradio_interface(username): prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...") resolution_dropdown = gr.Dropdown(choices=list(resolutions.keys()), label="Resolution", value="1024x1024") generate_button = gr.Button("Generate") result_output = gr.Image(label="Generated Image", type="pil") message_output = gr.Textbox(label="Result", placeholder="Results will be shown here", interactive=False) generate_button.click(fn=lambda prompt, resolution_key: gradio_interface(prompt, resolution_key), inputs=[prompt_input, resolution_dropdown], outputs=[result_output, message_output]) with gr.Blocks() as interface: gr.HTML(f"""

Welcome, {username}! Generate images based on prompts.

""") gr.HTML(""" """) return interface def launch_gradio(username): interface = create_gradio_interface(username) interface.launch() def extract_username_from_url(url): parsed_url = urlparse(url) params = parse_qs(parsed_url.query) return params.get('username', ["Guest"])[0] def get_username_from_url(): # Placeholder URL; replace with actual URL or method to get URL in deployment url = 'https://measmonysuon-imagen.hf.space/?username=MoMo' username = extract_username_from_url(url) logger.info(f"Extracted username: {username}") return username username = get_username_from_url() launch_gradio(username)