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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -30
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import json
4
  from datetime import datetime
5
  from pathlib import Path
 
6
 
7
  # ============================
8
  # USER DATABASE
@@ -33,6 +34,23 @@ else:
33
  ]
34
  }
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  # ============================
37
  # HELPERS
38
  # ============================
@@ -112,13 +130,6 @@ def generate_variants(kind, goal, tone, audience, length, constraints, language,
112
  return variants
113
 
114
  def generate_json(username, tier, goal, kind, tone, audience, length, constraints, language, n):
115
- goal = normalize(goal, "Describe the task clearly.")
116
- tone = normalize(tone, "neutral")
117
- audience = normalize(audience, "general")
118
- length = normalize(length, "medium")
119
- constraints = normalize(constraints, "none")
120
- language = normalize(language, "Indonesian")
121
-
122
  limits = {"free": 5, "premium": 10, "super": 20}
123
  n = max(1, min(limits[tier], int(n)))
124
 
@@ -154,9 +165,7 @@ with gr.Blocks() as demo:
154
  login_user = gr.State("")
155
  login_tier = gr.State("free")
156
 
157
- # ============================
158
- # LOGIN UI
159
- # ============================
160
  with gr.Group(visible=True) as login_ui:
161
  gr.Markdown("# πŸ” Login untuk Menggunakan AIPromptLab")
162
 
@@ -165,14 +174,28 @@ with gr.Blocks() as demo:
165
  login_btn = gr.Button("Login")
166
  login_msg = gr.Markdown("")
167
 
168
- # ============================
169
- # MAIN FEATURE UI (HIDDEN FIRST)
170
- # ============================
171
  with gr.Group(visible=False) as main_ui:
172
 
173
  gr.Markdown("# 🌟 AIPromptLab β€” JSON Output Edition")
174
 
175
- goal = gr.Textbox(label="Goal")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  kind = gr.Radio(["text", "image", "code"], value="text")
177
  tone = gr.Textbox(label="Tone")
178
  audience = gr.Textbox(label="Audience")
@@ -185,15 +208,13 @@ with gr.Blocks() as demo:
185
  output_json = gr.Textbox(label="Output JSON", lines=25, elem_id="json-output")
186
  copy_btn = gr.Button("Copy JSON")
187
 
188
- # ============================
189
- # LOGIN LOGIC
190
- # ============================
191
  def handle_login(username, password):
192
  ok, tier, msg = login(username, password)
193
  return (
194
  msg,
195
- gr.update(visible=not ok), # login UI hilang
196
- gr.update(visible=ok), # main UI muncul
197
  ok,
198
  username if ok else "",
199
  tier if ok else "free"
@@ -205,27 +226,30 @@ with gr.Blocks() as demo:
205
  [login_msg, login_ui, main_ui, login_status, login_user, login_tier]
206
  )
207
 
208
- # ============================
209
- # GENERATE JSON
210
- # ============================
211
  def handle_generate(login_status, login_user, login_tier,
212
- goal, kind, tone, audience, length, constraints, language, n):
 
213
 
214
  if not login_status:
215
  return "❌ Anda belum login."
216
 
 
 
 
 
 
217
  return generate_json(login_user, login_tier, goal, kind, tone, audience, length, constraints, language, n)
218
 
219
  generate_btn.click(
220
  handle_generate,
221
  [login_status, login_user, login_tier,
222
- goal, kind, tone, audience, length, constraints, language, n],
 
223
  output_json
224
  )
225
 
226
- # ============================
227
- # COPY JSON (SAFE FOR GRADIO 6.x)
228
- # ============================
229
  copy_btn.click(
230
  None,
231
  None,
@@ -233,9 +257,7 @@ with gr.Blocks() as demo:
233
  js="navigator.clipboard.writeText(document.getElementById('json-output').value)"
234
  )
235
 
236
- # ============================
237
- # LAUNCH (share=True FIX)
238
- # ============================
239
  demo.launch(
240
  server_name="0.0.0.0",
241
  server_port=7860,
 
3
  import json
4
  from datetime import datetime
5
  from pathlib import Path
6
+ from PIL import Image
7
 
8
  # ============================
9
  # USER DATABASE
 
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
  # ============================
 
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
 
 
165
  login_user = gr.State("")
166
  login_tier = gr.State("free")
167
 
168
+ # LOGIN UI
 
 
169
  with gr.Group(visible=True) as login_ui:
170
  gr.Markdown("# πŸ” Login untuk Menggunakan AIPromptLab")
171
 
 
174
  login_btn = gr.Button("Login")
175
  login_msg = gr.Markdown("")
176
 
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")
 
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):
213
  ok, tier, msg = login(username, password)
214
  return (
215
  msg,
216
+ gr.update(visible=not ok),
217
+ gr.update(visible=ok),
218
  ok,
219
  username if ok else "",
220
  tier if ok else "free"
 
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,
 
257
  js="navigator.clipboard.writeText(document.getElementById('json-output').value)"
258
  )
259
 
260
+ # LAUNCH
 
 
261
  demo.launch(
262
  server_name="0.0.0.0",
263
  server_port=7860,