Akhmad123 commited on
Commit
b8c35d5
Β·
verified Β·
1 Parent(s): b6aa4f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +344 -157
app.py CHANGED
@@ -2,11 +2,9 @@
2
  import gradio as gr
3
  import json
4
  from datetime import datetime
5
- from pathlib import Path
6
- from PIL import Image
7
 
8
  # ============================
9
- # USER DATABASE
10
  # ============================
11
  USER_DB = {
12
  "akhmad": {"password": "12345", "tier": "super"},
@@ -14,43 +12,6 @@ USER_DB = {
14
  "free_user": {"password": "00000", "tier": "free"},
15
  }
16
 
17
- # ============================
18
- # LOAD TEMPLATES
19
- # ============================
20
- TEMPLATES_PATH = Path("templates.json")
21
- if TEMPLATES_PATH.exists():
22
- with open(TEMPLATES_PATH, "r", encoding="utf-8") as f:
23
- TEMPLATES = json.load(f)
24
- else:
25
- TEMPLATES = {
26
- "text": [
27
- "{goal}\nAudience: {audience}\nTone: {tone}\nLength: {length}\nConstraints: {constraints}\n\nWrite a clear, structured output with headings and examples."
28
- ],
29
- "image": [
30
- "Scene: {goal}. Style: {tone}. Camera: 35mm; Lighting: golden hour; Colors: warm. Details: {constraints}. Negative: avoid text, watermarks."
31
- ],
32
- "code": [
33
- "Write code to {goal}. Language: {language}. Requirements: {constraints}. Include comments and tests."
34
- ]
35
- }
36
-
37
- # ============================
38
- # IMAGE CAPTIONING MODEL
39
- # ============================
40
- from transformers import BlipProcessor, BlipForConditionalGeneration
41
-
42
- processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
43
- model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
44
-
45
- def describe_image(img):
46
- if img is None:
47
- return ""
48
- image = Image.open(img)
49
- inputs = processor(image, return_tensors="pt")
50
- out = model.generate(**inputs)
51
- caption = processor.decode(out[0], skip_special_tokens=True)
52
- return caption
53
-
54
  # ============================
55
  # HELPERS
56
  # ============================
@@ -59,91 +20,135 @@ def normalize(v, default=""):
59
  return default
60
  return str(v).strip()
61
 
62
- def augment(prompt, idx, tier="free"):
63
- base = [
64
- "\nInclude examples and a short checklist.",
65
- "\nUse bullet points and a short summary.",
66
- "\nProvide 3 alternative phrasings.",
67
- "\nAdd a short FAQ section.",
68
- "\nInclude a call-to-action."
69
- ]
70
-
71
- premium_extra = [
72
- "\nOptimize for clarity and conversion.",
73
- "\nAdd variations for A/B testing.",
74
- ]
75
-
76
- super_extra = [
77
- "\nAdd SEO optimization with keywords.",
78
- "\nUse power words to increase impact.",
79
- "\nAdd a storytelling angle.",
80
- "\nProvide 5 headline variations.",
81
- ]
82
-
83
- if tier == "premium":
84
- base += premium_extra
85
- if tier == "super":
86
- base += premium_extra + super_extra
87
 
88
- if idx < len(base):
89
- return prompt + base[idx]
90
- return prompt
 
 
 
91
 
92
- def recommend(kind, tier="free"):
93
- base = {
94
- "text": "Use temperature 0.2–0.5 for factual text.",
95
- "image": "Use negative prompts to remove artifacts.",
96
- "code": "Validate output before production."
97
- }.get(kind, "General guidance.")
98
 
99
- if tier == "premium":
100
- return base + " Premium: cocok untuk workflow profesional."
101
- if tier == "super":
102
- return base + " Super Premium: optimized for expert-level output."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
- return base
105
 
106
  # ============================
107
- # GENERATOR
108
  # ============================
109
- def generate_variants(kind, goal, tone, audience, length, constraints, language, n, tier):
110
- templates = TEMPLATES.get(kind, TEMPLATES["text"])
111
- variants = []
112
-
113
- for i in range(n):
114
- tpl = templates[i % len(templates)]
115
- prompt = tpl.format(
116
- goal=goal,
117
- tone=tone,
118
- audience=audience,
119
- length=length,
120
- constraints=constraints,
121
- language=language
122
- )
123
- prompt = augment(prompt, i, tier)
124
- variants.append({
125
- "id": f"v{i+1}",
126
- "prompt": prompt,
127
- "notes": recommend(kind, tier)
128
- })
129
 
130
- return variants
 
 
131
 
132
- def generate_json(username, tier, goal, kind, tone, audience, length, constraints, language, n):
133
- limits = {"free": 5, "premium": 10, "super": 20}
134
- n = max(1, min(limits[tier], int(n)))
 
 
 
 
 
 
 
 
 
 
135
 
136
- variants = generate_variants(
137
- kind, goal, tone, audience, length, constraints, language, n, tier
138
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
  data = {
141
- "generated_at": datetime.utcnow().isoformat() + "Z",
 
142
  "user": username,
143
  "tier": tier,
144
- "kind": kind,
 
 
145
  "goal": goal,
146
- "variants": variants
147
  }
148
 
149
  return json.dumps(data, ensure_ascii=False, indent=2)
@@ -177,36 +182,185 @@ with gr.Blocks() as demo:
177
  # MAIN UI (hidden first)
178
  with gr.Group(visible=False) as main_ui:
179
 
180
- gr.Markdown("# 🌟 AIPromptLab β€” JSON Output Edition")
181
-
182
- input_mode = gr.Radio(["text", "image"], value="text", label="Input Mode")
183
-
184
- text_input = gr.Textbox(label="Text Input", visible=True)
185
- image_input = gr.Image(label="Upload Image", type="filepath", visible=False)
186
-
187
- def switch_input(mode):
188
- return (
189
- gr.update(visible=(mode=="text")),
190
- gr.update(visible=(mode=="image"))
191
- )
192
-
193
- input_mode.change(
194
- switch_input,
195
- [input_mode],
196
- [text_input, image_input]
197
- )
198
-
199
- kind = gr.Radio(["text", "image", "code"], value="text")
200
- tone = gr.Textbox(label="Tone")
201
- audience = gr.Textbox(label="Audience")
202
- length = gr.Textbox(label="Length")
203
- constraints = gr.Textbox(label="Constraints")
204
- language = gr.Textbox(label="Language", value="Indonesian")
205
- n = gr.Slider(1, 20, value=3, step=1, label="Jumlah Varian")
206
-
207
- generate_btn = gr.Button("πŸš€ Generate JSON")
208
- output_json = gr.Textbox(label="Output JSON", lines=25, elem_id="json-output")
209
- copy_btn = gr.Button("Copy JSON")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  # LOGIN LOGIC
212
  def handle_login(username, password):
@@ -226,35 +380,68 @@ with gr.Blocks() as demo:
226
  [login_msg, login_ui, main_ui, login_status, login_user, login_tier]
227
  )
228
 
229
- # GENERATE JSON
230
- def handle_generate(login_status, login_user, login_tier,
231
- input_mode, text_input, image_input,
232
- kind, tone, audience, length, constraints, language, n):
 
 
 
 
 
 
 
 
 
233
 
 
 
 
234
  if not login_status:
235
  return "❌ Anda belum login."
 
236
 
237
- if input_mode == "text":
238
- goal = text_input
239
- else:
240
- goal = describe_image(image_input)
 
 
241
 
242
- return generate_json(login_user, login_tier, goal, kind, tone, audience, length, constraints, language, n)
 
 
 
 
 
243
 
244
- generate_btn.click(
245
- handle_generate,
246
  [login_status, login_user, login_tier,
247
- input_mode, text_input, image_input,
248
- kind, tone, audience, length, constraints, language, n],
249
- output_json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  )
251
 
252
- # COPY JSON
253
- copy_btn.click(
254
  None,
255
  None,
256
  None,
257
- js="navigator.clipboard.writeText(document.getElementById('json-output').value)"
258
  )
259
 
260
  # LAUNCH
 
2
  import gradio as gr
3
  import json
4
  from datetime import datetime
 
 
5
 
6
  # ============================
7
+ # USER DATABASE (SIMPLE)
8
  # ============================
9
  USER_DB = {
10
  "akhmad": {"password": "12345", "tier": "super"},
 
12
  "free_user": {"password": "00000", "tier": "free"},
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # ============================
16
  # HELPERS
17
  # ============================
 
20
  return default
21
  return str(v).strip()
22
 
23
+ def now_iso():
24
+ return datetime.utcnow().isoformat() + "Z"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # ============================
27
+ # E-BOOK JSON GENERATOR
28
+ # ============================
29
+ def generate_ebook_json(username, tier,
30
+ goal, genre, tone, style,
31
+ platform, length):
32
 
33
+ goal = normalize(goal, "Islamic children's story about good manners.")
34
+ genre = normalize(genre, "Islamic Children Story")
35
+ tone = normalize(tone, "Friendly")
36
+ style = normalize(style, "Storybook style")
37
+ platform = normalize(platform, "KDP")
38
+ length = normalize(length, "Medium (30–50 pages)")
39
 
40
+ data = {
41
+ "type": "ebook",
42
+ "generated_at": now_iso(),
43
+ "user": username,
44
+ "tier": tier,
45
+ "platform": platform,
46
+ "genre": genre,
47
+ "tone": tone,
48
+ "writing_style": style,
49
+ "length": length,
50
+ "goal": goal,
51
+ "prompts": {
52
+ "title_prompt": (
53
+ f"Generate 10 title ideas for an {genre} ebook about '{goal}'. "
54
+ f"Tone: {tone}. Style: {style}. Platform: {platform}. "
55
+ "Titles should be short, catchy, and suitable for children."
56
+ ),
57
+ "outline_prompt": (
58
+ f"Create a detailed chapter outline for an {genre} ebook about '{goal}'. "
59
+ f"Tone: {tone}. Style: {style}. Length: {length}. "
60
+ "Include chapter titles and 2–3 bullet points per chapter."
61
+ ),
62
+ "chapter_prompt": (
63
+ f"Write Chapter 1 of an {genre} ebook about '{goal}'. "
64
+ f"Tone: {tone}. Style: {style}. Length: {length}. "
65
+ "Use simple language suitable for children and include dialogues."
66
+ )
67
+ }
68
+ }
69
 
70
+ return json.dumps(data, ensure_ascii=False, indent=2)
71
 
72
  # ============================
73
+ # IMAGE DESIGN JSON GENERATOR
74
  # ============================
75
+ def generate_image_json(username, tier,
76
+ goal, design_style, color_palette,
77
+ platform, aspect_ratio):
78
+
79
+ goal = normalize(goal, "Islamic children's story cover about kindness.")
80
+ design_style = normalize(design_style, "Pastel Islamic")
81
+ color_palette = normalize(color_palette, "Pastel")
82
+ platform = normalize(platform, "Midjourney")
83
+ aspect_ratio = normalize(aspect_ratio, "1:1")
84
+
85
+ prompt = (
86
+ f"{goal}, {design_style} style, {color_palette} colors, "
87
+ f"high quality, aspect ratio {aspect_ratio}, soft lighting, kid-friendly, Islamic theme."
88
+ )
 
 
 
 
 
 
89
 
90
+ negative_prompt = (
91
+ "no text, no watermark, no logo, no distortion, no blur, no low quality, no violence."
92
+ )
93
 
94
+ data = {
95
+ "type": "image_design",
96
+ "generated_at": now_iso(),
97
+ "user": username,
98
+ "tier": tier,
99
+ "platform": platform,
100
+ "goal": goal,
101
+ "style": design_style,
102
+ "color_palette": color_palette,
103
+ "aspect_ratio": aspect_ratio,
104
+ "prompt": prompt,
105
+ "negative_prompt": negative_prompt
106
+ }
107
 
108
+ return json.dumps(data, ensure_ascii=False, indent=2)
109
+
110
+ # ============================
111
+ # VIDEO AI JSON GENERATOR
112
+ # ============================
113
+ def generate_video_json(username, tier,
114
+ goal, video_style, camera_movement,
115
+ platform, duration):
116
+
117
+ goal = normalize(goal, "Islamic children's story animation about honesty.")
118
+ video_style = normalize(video_style, "Pastel Kids Animation")
119
+ camera_movement = normalize(camera_movement, "Slow Zoom")
120
+ platform = normalize(platform, "Pika Labs")
121
+ duration = normalize(duration, "10 seconds")
122
+
123
+ scenes = [
124
+ {
125
+ "scene": 1,
126
+ "description": (
127
+ f"A child reading an Islamic storybook about '{goal}' in a cozy room, "
128
+ f"{video_style.lower()}, soft pastel colors, warm lighting."
129
+ ),
130
+ "camera": camera_movement
131
+ },
132
+ {
133
+ "scene": 2,
134
+ "description": (
135
+ "Close-up of the book pages glowing softly, Arabic patterns in the background, "
136
+ "gentle motion, peaceful atmosphere."
137
+ ),
138
+ "camera": "Static Shot"
139
+ }
140
+ ]
141
 
142
  data = {
143
+ "type": "video_ai",
144
+ "generated_at": now_iso(),
145
  "user": username,
146
  "tier": tier,
147
+ "platform": platform,
148
+ "style": video_style,
149
+ "duration": duration,
150
  "goal": goal,
151
+ "scenes": scenes
152
  }
153
 
154
  return json.dumps(data, ensure_ascii=False, indent=2)
 
182
  # MAIN UI (hidden first)
183
  with gr.Group(visible=False) as main_ui:
184
 
185
+ gr.Markdown("# 🌟 AIPromptLab β€” Multi Output JSON Prompt Generator")
186
+
187
+ with gr.Tabs():
188
+
189
+ # ============================
190
+ # TAB 1: E-BOOK
191
+ # ============================
192
+ with gr.Tab("E-Book Prompt"):
193
+
194
+ ebook_goal = gr.Textbox(label="Goal / Ide E-book")
195
+ ebook_genre = gr.Dropdown(
196
+ label="Genre",
197
+ choices=[
198
+ "Islamic Children Story",
199
+ "Parenting",
200
+ "Motivation",
201
+ "Fantasy",
202
+ "Education",
203
+ "Adventure",
204
+ "Mystery"
205
+ ],
206
+ value="Islamic Children Story"
207
+ )
208
+ ebook_tone = gr.Dropdown(
209
+ label="Tone",
210
+ choices=[
211
+ "Friendly",
212
+ "Inspirational",
213
+ "Educational",
214
+ "Storytelling",
215
+ "Formal",
216
+ "Casual"
217
+ ],
218
+ value="Friendly"
219
+ )
220
+ ebook_style = gr.Dropdown(
221
+ label="Writing Style",
222
+ choices=[
223
+ "Storybook style",
224
+ "Narrative",
225
+ "Descriptive",
226
+ "Persuasive",
227
+ "Expository",
228
+ "Dialog-based"
229
+ ],
230
+ value="Storybook style"
231
+ )
232
+ ebook_platform = gr.Dropdown(
233
+ label="Target Platform",
234
+ choices=[
235
+ "KDP",
236
+ "Wattpad",
237
+ "Google Play Books",
238
+ "PDF / E-Learning"
239
+ ],
240
+ value="KDP"
241
+ )
242
+ ebook_length = gr.Dropdown(
243
+ label="Length",
244
+ choices=[
245
+ "Short (10–20 pages)",
246
+ "Medium (30–50 pages)",
247
+ "Long (80–120 pages)"
248
+ ],
249
+ value="Medium (30–50 pages)"
250
+ )
251
+
252
+ ebook_generate_btn = gr.Button("πŸš€ Generate E-book JSON")
253
+ ebook_output = gr.Textbox(label="E-book JSON Output", lines=20, elem_id="ebook-json-output")
254
+ ebook_copy_btn = gr.Button("Copy E-book JSON")
255
+
256
+ # ============================
257
+ # TAB 2: IMAGE DESIGN
258
+ # ============================
259
+ with gr.Tab("Image Design Prompt"):
260
+
261
+ img_goal = gr.Textbox(label="Goal / Ide Desain Gambar")
262
+ img_style = gr.Dropdown(
263
+ label="Design Style",
264
+ choices=[
265
+ "Pastel Islamic",
266
+ "Flat Illustration",
267
+ "3D Render",
268
+ "Watercolor",
269
+ "Minimalist",
270
+ "Cartoon Kids",
271
+ "Realistic Photography"
272
+ ],
273
+ value="Pastel Islamic"
274
+ )
275
+ img_palette = gr.Dropdown(
276
+ label="Color Palette",
277
+ choices=[
278
+ "Pastel",
279
+ "Vibrant",
280
+ "Monochrome",
281
+ "Earth Tone",
282
+ "Neon"
283
+ ],
284
+ value="Pastel"
285
+ )
286
+ img_platform = gr.Dropdown(
287
+ label="Target Platform",
288
+ choices=[
289
+ "Midjourney",
290
+ "DALLΒ·E",
291
+ "Stable Diffusion",
292
+ "Canva Design Brief"
293
+ ],
294
+ value="Midjourney"
295
+ )
296
+ img_ratio = gr.Dropdown(
297
+ label="Aspect Ratio",
298
+ choices=[
299
+ "1:1",
300
+ "9:16",
301
+ "16:9",
302
+ "4:5"
303
+ ],
304
+ value="1:1"
305
+ )
306
+
307
+ img_generate_btn = gr.Button("πŸš€ Generate Image Design JSON")
308
+ img_output = gr.Textbox(label="Image Design JSON Output", lines=20, elem_id="image-json-output")
309
+ img_copy_btn = gr.Button("Copy Image JSON")
310
+
311
+ # ============================
312
+ # TAB 3: VIDEO AI
313
+ # ============================
314
+ with gr.Tab("Video AI Prompt"):
315
+
316
+ vid_goal = gr.Textbox(label="Goal / Ide Video AI")
317
+ vid_style = gr.Dropdown(
318
+ label="Video Style",
319
+ choices=[
320
+ "Cinematic",
321
+ "Pastel Kids Animation",
322
+ "Anime",
323
+ "Realistic",
324
+ "Claymation",
325
+ "Watercolor Motion"
326
+ ],
327
+ value="Pastel Kids Animation"
328
+ )
329
+ vid_camera = gr.Dropdown(
330
+ label="Camera Movement",
331
+ choices=[
332
+ "Slow Zoom",
333
+ "Pan Left",
334
+ "Pan Right",
335
+ "Dolly In",
336
+ "Static Shot"
337
+ ],
338
+ value="Slow Zoom"
339
+ )
340
+ vid_platform = gr.Dropdown(
341
+ label="Target Platform",
342
+ choices=[
343
+ "Pika Labs",
344
+ "Runway Gen-2",
345
+ "Kaiber",
346
+ "Luma Dream Machine"
347
+ ],
348
+ value="Pika Labs"
349
+ )
350
+ vid_duration = gr.Dropdown(
351
+ label="Duration",
352
+ choices=[
353
+ "5 seconds",
354
+ "10 seconds",
355
+ "15 seconds",
356
+ "30 seconds"
357
+ ],
358
+ value="10 seconds"
359
+ )
360
+
361
+ vid_generate_btn = gr.Button("πŸš€ Generate Video AI JSON")
362
+ vid_output = gr.Textbox(label="Video AI JSON Output", lines=20, elem_id="video-json-output")
363
+ vid_copy_btn = gr.Button("Copy Video JSON")
364
 
365
  # LOGIN LOGIC
366
  def handle_login(username, password):
 
380
  [login_msg, login_ui, main_ui, login_status, login_user, login_tier]
381
  )
382
 
383
+ # E-BOOK GENERATE
384
+ def handle_ebook_generate(login_status, login_user, login_tier,
385
+ goal, genre, tone, style, platform, length):
386
+ if not login_status:
387
+ return "❌ Anda belum login."
388
+ return generate_ebook_json(login_user, login_tier, goal, genre, tone, style, platform, length)
389
+
390
+ ebook_generate_btn.click(
391
+ handle_ebook_generate,
392
+ [login_status, login_user, login_tier,
393
+ ebook_goal, ebook_genre, ebook_tone, ebook_style, ebook_platform, ebook_length],
394
+ ebook_output
395
+ )
396
 
397
+ # IMAGE GENERATE
398
+ def handle_image_generate(login_status, login_user, login_tier,
399
+ goal, design_style, color_palette, platform, aspect_ratio):
400
  if not login_status:
401
  return "❌ Anda belum login."
402
+ return generate_image_json(login_user, login_tier, goal, design_style, color_palette, platform, aspect_ratio)
403
 
404
+ img_generate_btn.click(
405
+ handle_image_generate,
406
+ [login_status, login_user, login_tier,
407
+ img_goal, img_style, img_palette, img_platform, img_ratio],
408
+ img_output
409
+ )
410
 
411
+ # VIDEO GENERATE
412
+ def handle_video_generate(login_status, login_user, login_tier,
413
+ goal, video_style, camera_movement, platform, duration):
414
+ if not login_status:
415
+ return "❌ Anda belum login."
416
+ return generate_video_json(login_user, login_tier, goal, video_style, camera_movement, platform, duration)
417
 
418
+ vid_generate_btn.click(
419
+ handle_video_generate,
420
  [login_status, login_user, login_tier,
421
+ vid_goal, vid_style, vid_camera, vid_platform, vid_duration],
422
+ vid_output
423
+ )
424
+
425
+ # COPY BUTTONS (JS)
426
+ ebook_copy_btn.click(
427
+ None,
428
+ None,
429
+ None,
430
+ js="navigator.clipboard.writeText(document.getElementById('ebook-json-output').value)"
431
+ )
432
+
433
+ img_copy_btn.click(
434
+ None,
435
+ None,
436
+ None,
437
+ js="navigator.clipboard.writeText(document.getElementById('image-json-output').value)"
438
  )
439
 
440
+ vid_copy_btn.click(
 
441
  None,
442
  None,
443
  None,
444
+ js="navigator.clipboard.writeText(document.getElementById('video-json-output').value)"
445
  )
446
 
447
  # LAUNCH