vauth commited on
Commit
7a01703
·
verified ·
1 Parent(s): 9a3a24a

UPDATE refined

Browse files
Files changed (1) hide show
  1. app.py +63 -22
app.py CHANGED
@@ -1,29 +1,70 @@
 
 
1
  import gradio as gr
2
  from somnium import Somnium
3
 
4
- # define function to generate image
5
- def generate_image(prompt, style_id):
6
- if prompt == "" or style_id == "":
7
- raise gr.Error("Empty values")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  try:
9
- styles = Somnium.Styles()
10
- image = Somnium.Generate(prompt, styles[style_id])
11
- image + "69" # Secret line
12
  return image
13
  except Exception as e:
14
- raise gr.Error("Process failed or contains NSFW")
15
-
16
- # create interface
17
- iface = gr.Interface(
18
- fn=generate_image,
19
- inputs=[
20
- gr.Textbox(label="Enter Prompt:", max_lines=10),
21
- gr.Dropdown(list((Somnium.Styles()).keys()), label="Select Style:")
22
- ],
23
- outputs=gr.Image(show_download_button=False, show_share_button=False),
24
- allow_duplication=True,
25
- title="Somnium Image Generator"
26
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # run the interface
29
- iface.launch()
 
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())