File size: 2,110 Bytes
7a01703
 
77dd5f1
 
 
7a01703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77dd5f1
7a01703
 
c5147b4
77dd5f1
7a01703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77dd5f1
7a01703
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
from typing import Any
import gradio as gr
from somnium import Somnium

logging.basicConfig(
    level=logging.INFO, 
    format="%(asctime)s - %(levelname)s - %(message)s"
)

try:
    STYLES_DICT = Somnium.Styles()
    STYLE_CHOICES = list(STYLES_DICT.keys())
except Exception as e:
    logging.error(f"Failed to load Somnium styles: {e}")
    STYLES_DICT = {}
    STYLE_CHOICES = []

def generate_image(prompt: str, style_id: str) -> Any:
    if not prompt or not prompt.strip():
        raise gr.Error("Please enter a valid prompt.")
    if not style_id or style_id not in STYLES_DICT:
        raise gr.Error("Please select a valid style.")

    try:
        logging.info(f"Generating image with style: '{style_id}'")
        image = Somnium.Generate(prompt, STYLES_DICT[style_id])
        return image
    except Exception as e:
        logging.error(f"Generation failed: {e}", exc_info=True)
        raise gr.Error("Generation failed. Please try a different prompt or check for NSFW content.")


with gr.Blocks(title="Somnium Image Generator") as demo:
    
    gr.Markdown(
        """
        # 🌌 Somnium Image Generator
        Enter a prompt and select a style to generate your image.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            prompt_input = gr.Textbox(
                label="Enter Prompt:", 
                placeholder="A futuristic city at sunset...",
                lines=5
            )
            style_dropdown = gr.Dropdown(
                choices=STYLE_CHOICES, 
                label="Select Style:",
                value=STYLE_CHOICES[0] if STYLE_CHOICES else None
            )
            generate_btn = gr.Button("Generate Image", variant="primary")
            
        with gr.Column(scale=1):
            image_output = gr.Image(
                label="Output", 
                buttons=["download"] 
            )

    generate_btn.click(
        fn=generate_image,
        inputs=[prompt_input, style_dropdown],
        outputs=image_output
    )

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft())