Spaces:
Running
Running
UPDATE refined
Browse files
app.py
CHANGED
|
@@ -1,29 +1,70 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from somnium import Somnium
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
image = Somnium.Generate(prompt,
|
| 11 |
-
image + "69" # Secret line
|
| 12 |
return image
|
| 13 |
except Exception as e:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import Any
|
| 3 |
import gradio as gr
|
| 4 |
from somnium import Somnium
|
| 5 |
|
| 6 |
+
logging.basicConfig(
|
| 7 |
+
level=logging.INFO,
|
| 8 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
STYLES_DICT = Somnium.Styles()
|
| 13 |
+
STYLE_CHOICES = list(STYLES_DICT.keys())
|
| 14 |
+
except Exception as e:
|
| 15 |
+
logging.error(f"Failed to load Somnium styles: {e}")
|
| 16 |
+
STYLES_DICT = {}
|
| 17 |
+
STYLE_CHOICES = []
|
| 18 |
+
|
| 19 |
+
def generate_image(prompt: str, style_id: str) -> Any:
|
| 20 |
+
if not prompt or not prompt.strip():
|
| 21 |
+
raise gr.Error("Please enter a valid prompt.")
|
| 22 |
+
if not style_id or style_id not in STYLES_DICT:
|
| 23 |
+
raise gr.Error("Please select a valid style.")
|
| 24 |
+
|
| 25 |
try:
|
| 26 |
+
logging.info(f"Generating image with style: '{style_id}'")
|
| 27 |
+
image = Somnium.Generate(prompt, STYLES_DICT[style_id])
|
|
|
|
| 28 |
return image
|
| 29 |
except Exception as e:
|
| 30 |
+
logging.error(f"Generation failed: {e}", exc_info=True)
|
| 31 |
+
raise gr.Error("Generation failed. Please try a different prompt or check for NSFW content.")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
with gr.Blocks(title="Somnium Image Generator") as demo:
|
| 35 |
+
|
| 36 |
+
gr.Markdown(
|
| 37 |
+
"""
|
| 38 |
+
# 🌌 Somnium Image Generator
|
| 39 |
+
Enter a prompt and select a style to generate your image.
|
| 40 |
+
"""
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column(scale=1):
|
| 45 |
+
prompt_input = gr.Textbox(
|
| 46 |
+
label="Enter Prompt:",
|
| 47 |
+
placeholder="A futuristic city at sunset...",
|
| 48 |
+
lines=5
|
| 49 |
+
)
|
| 50 |
+
style_dropdown = gr.Dropdown(
|
| 51 |
+
choices=STYLE_CHOICES,
|
| 52 |
+
label="Select Style:",
|
| 53 |
+
value=STYLE_CHOICES[0] if STYLE_CHOICES else None
|
| 54 |
+
)
|
| 55 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 56 |
+
|
| 57 |
+
with gr.Column(scale=1):
|
| 58 |
+
image_output = gr.Image(
|
| 59 |
+
label="Output",
|
| 60 |
+
buttons=["download"]
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
generate_btn.click(
|
| 64 |
+
fn=generate_image,
|
| 65 |
+
inputs=[prompt_input, style_dropdown],
|
| 66 |
+
outputs=image_output
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch(theme=gr.themes.Soft())
|