SOLRICKS commited on
Commit
60d4900
·
verified ·
1 Parent(s): d6d93e8

Add Gradio image generator app

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+
5
+ API_BASE_URL = os.getenv("SOLRICKS_API_BASE_URL", "https://api.solricks.com")
6
+ API_KEY = os.getenv("SOLRICKS_SPACE_API_KEY", "")
7
+
8
+ API_ENDPOINT = f"{API_BASE_URL}/api/workflows/visual-ai/run"
9
+
10
+ ALLOWED_RESOLUTIONS = ["1024x1024", "768x1344", "1344x768"]
11
+ ALLOWED_STYLES = ["Portrait", "Realistic", "Cinematic", "Product"]
12
+
13
+
14
+ def generate_image(prompt, style, resolution):
15
+ prompt = (prompt or "").strip()
16
+
17
+ if not prompt:
18
+ return None, "Please enter a prompt."
19
+
20
+ if len(prompt) > 500:
21
+ return None, "Prompt is too long. Please keep it under 500 characters."
22
+
23
+ if resolution not in ALLOWED_RESOLUTIONS:
24
+ return None, "Invalid resolution."
25
+
26
+ if style not in ALLOWED_STYLES:
27
+ return None, "Invalid style."
28
+
29
+ payload = {
30
+ "prompt": prompt,
31
+ "style": style,
32
+ "resolution": resolution,
33
+ "enhancePrompt": False,
34
+ "seedMode": "Random",
35
+ "source": "huggingface-space",
36
+ }
37
+
38
+ headers = {
39
+ "Content-Type": "application/json",
40
+ }
41
+
42
+ if API_KEY:
43
+ headers["Authorization"] = f"Bearer {API_KEY}"
44
+
45
+ try:
46
+ response = requests.post(
47
+ API_ENDPOINT,
48
+ json=payload,
49
+ headers=headers,
50
+ timeout=240,
51
+ )
52
+
53
+ try:
54
+ data = response.json()
55
+ except Exception:
56
+ return None, "Solricks backend returned an invalid response."
57
+
58
+ if response.status_code == 429:
59
+ return None, "Too many requests. Please wait a few minutes and try again."
60
+
61
+ if not response.ok:
62
+ return None, data.get("error") or data.get("detail") or "Generation failed."
63
+
64
+ image_url = data.get("image_url")
65
+
66
+ if not image_url:
67
+ return None, "No image was returned."
68
+
69
+ if image_url.startswith("/"):
70
+ image_url = f"{API_BASE_URL}{image_url}"
71
+
72
+ return image_url, "Done."
73
+
74
+ except requests.exceptions.Timeout:
75
+ return None, "Generation timed out. Please try again."
76
+ except Exception as error:
77
+ return None, f"Error: {error}"
78
+
79
+
80
+ with gr.Blocks(title="SOLRICKS Image Generator") as demo:
81
+ gr.Markdown(
82
+ """
83
+ # SOLRICKS Image Generator
84
+
85
+ Free visual AI preview powered by the Solricks backend.
86
+
87
+ This public demo is rate-limited and intended for short preview generations.
88
+ """
89
+ )
90
+
91
+ prompt = gr.Textbox(
92
+ label="Prompt",
93
+ placeholder="Describe the image you want to generate...",
94
+ lines=4,
95
+ max_lines=6,
96
+ )
97
+
98
+ with gr.Row():
99
+ style = gr.Dropdown(
100
+ label="Style",
101
+ choices=ALLOWED_STYLES,
102
+ value="Portrait",
103
+ )
104
+
105
+ resolution = gr.Dropdown(
106
+ label="Resolution",
107
+ choices=ALLOWED_RESOLUTIONS,
108
+ value="1024x1024",
109
+ )
110
+
111
+ generate_button = gr.Button("Generate Image")
112
+
113
+ output_image = gr.Image(
114
+ label="Generated Image",
115
+ type="filepath",
116
+ )
117
+
118
+ status = gr.Textbox(
119
+ label="Status",
120
+ interactive=False,
121
+ )
122
+
123
+ gr.Examples(
124
+ examples=[
125
+ ["cinematic portrait of a futuristic woman, dramatic lighting, realistic details", "Portrait", "768x1344"],
126
+ ["luxury product photo of a perfume bottle, black background, studio lighting", "Product", "1024x1024"],
127
+ ["realistic AI robot in a dark premium technology lab, cinematic lighting", "Cinematic", "1344x768"],
128
+ ],
129
+ inputs=[prompt, style, resolution],
130
+ )
131
+
132
+ generate_button.click(
133
+ fn=generate_image,
134
+ inputs=[prompt, style, resolution],
135
+ outputs=[output_image, status],
136
+ )
137
+
138
+ demo.queue(max_size=10).launch()