tasal9 commited on
Commit
7c58507
·
1 Parent(s): b7d1634

Refactor predict function to streamline prompt building and enhance output formatting; update UI components for better user experience and status reporting

Browse files
Files changed (1) hide show
  1. app.py +39 -36
app.py CHANGED
@@ -147,38 +147,26 @@ def predict(instruction: str,
147
  mode: str):
148
  """Generate text using the cached pipeline and return output or error message."""
149
  if not instruction or not instruction.strip():
150
- return "⚠️ مهرباني وکړئ یوه لارښوونه ولیکئ." # please provide an instruction
151
 
152
- # Fast path: echo/useless mode avoids loading large models during testing.
 
 
 
 
 
 
153
  active_mode = (mode or "").strip().lower() or ECHO_MODE
154
  if active_mode in ("echo", "useless"):
155
- prompt = instruction.strip()
156
- if input_text and input_text.strip():
157
- prompt += "\n" + input_text.strip()
158
  if active_mode == "echo":
159
- return prompt
160
- else:
161
- return "This is a useless placeholder response."
162
-
163
- # Build a simple prompt: instruction (+ input if provided)
164
- prompt = instruction.strip()
165
- if input_text and input_text.strip():
166
- prompt += "\n" + input_text.strip()
167
-
168
- def _filter_generation_kwargs(kwargs: dict) -> dict:
169
- allowed = {
170
- "max_new_tokens",
171
- "num_beams",
172
- "do_sample",
173
- "temperature",
174
- "top_p",
175
- "num_return_sequences",
176
- }
177
- return {k: v for k, v in kwargs.items() if k in allowed}
178
 
179
  try:
180
  gen = get_generator()
181
- gen_kwargs = {
182
  "max_new_tokens": int(max_new_tokens),
183
  "num_beams": int(num_beams) if not do_sample else 1,
184
  "do_sample": bool(do_sample),
@@ -186,19 +174,23 @@ def predict(instruction: str,
186
  "top_p": float(top_p),
187
  "num_return_sequences": max(1, int(num_return_sequences)),
188
  }
189
-
190
- gen_kwargs = _filter_generation_kwargs(gen_kwargs)
191
  outputs = gen(prompt, **gen_kwargs)
192
 
 
 
193
  texts = []
194
- for out in outputs if isinstance(outputs, list) else [outputs]:
195
- text = out.get("generated_text", "").strip()
196
- texts.append(text)
197
-
 
 
 
198
  if not texts:
199
- return "⚠️ No response generated."
200
- return "\n\n---\n\n".join(texts)
201
-
202
  except Exception as e:
203
  logger.exception("Generation failed: %s", e)
204
  return f"⚠️ Generation failed: {e}"
@@ -215,7 +207,6 @@ def build_ui():
215
  که د موډ بدلول غواړئ لاندې د Mode selector څخه استفاده وکړئ.
216
  """
217
  )
218
-
219
  with gr.Row():
220
  with gr.Column(scale=2):
221
  instruction_dropdown = gr.Dropdown(
@@ -230,7 +221,7 @@ def build_ui():
230
  label="لارښوونه",
231
  )
232
  input_text = gr.Textbox(lines=2, placeholder="اختیاري متن...", label="متن")
233
- output = gr.Textbox(label="ځواب", interactive=False, lines=8)
234
  generate_btn = gr.Button("جوړول", variant="primary")
235
  mode_selector = gr.Dropdown(
236
  choices=["off", "echo", "useless"],
@@ -238,6 +229,8 @@ def build_ui():
238
  label="Mode (off=real, echo=return prompt, useless=fixed)",
239
  interactive=True,
240
  )
 
 
241
 
242
  with gr.Column(scale=1):
243
  gr.Markdown("### د تولید تنظیمات")
@@ -250,12 +243,22 @@ def build_ui():
250
 
251
  instruction_dropdown.change(lambda x: x, inputs=instruction_dropdown, outputs=instruction_textbox)
252
 
 
 
 
 
 
253
  generate_btn.click(
254
  fn=predict,
255
  inputs=[instruction_textbox, input_text, max_new_tokens, num_beams, do_sample, temperature, top_p, num_return_sequences, mode_selector],
256
  outputs=output,
257
  )
258
 
 
 
 
 
 
259
  return demo
260
 
261
 
 
147
  mode: str):
148
  """Generate text using the cached pipeline and return output or error message."""
149
  if not instruction or not instruction.strip():
150
+ return "⚠️ مهرباني وکړئ یوه لارښوونه ولیکئ."
151
 
152
+ def build_prompt() -> str:
153
+ base = instruction.strip()
154
+ if input_text and input_text.strip():
155
+ return base + "\n" + input_text.strip()
156
+ return base
157
+
158
+ prompt = build_prompt()
159
  active_mode = (mode or "").strip().lower() or ECHO_MODE
160
  if active_mode in ("echo", "useless"):
 
 
 
161
  if active_mode == "echo":
162
+ return f"### Prompt\n\n````\n{prompt}\n````\n\n### Output\n\n````\n{prompt}\n````"
163
+ return f"### Prompt\n\n````\n{prompt}\n````\n\n### Output\n\nThis is a useless placeholder response."
164
+
165
+ allowed_keys = {"max_new_tokens", "num_beams", "do_sample", "temperature", "top_p", "num_return_sequences"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  try:
168
  gen = get_generator()
169
+ raw_kwargs = {
170
  "max_new_tokens": int(max_new_tokens),
171
  "num_beams": int(num_beams) if not do_sample else 1,
172
  "do_sample": bool(do_sample),
 
174
  "top_p": float(top_p),
175
  "num_return_sequences": max(1, int(num_return_sequences)),
176
  }
177
+ gen_kwargs = {k: v for k, v in raw_kwargs.items() if k in allowed_keys}
 
178
  outputs = gen(prompt, **gen_kwargs)
179
 
180
+ if not isinstance(outputs, list):
181
+ outputs = [outputs]
182
  texts = []
183
+ for out in outputs:
184
+ if isinstance(out, dict):
185
+ text = out.get("generated_text", "").strip()
186
+ else:
187
+ text = str(out).strip()
188
+ if text:
189
+ texts.append(text)
190
  if not texts:
191
+ return f"### Prompt\n\n````\n{prompt}\n````\n\n### Output\n\n⚠️ No response generated."
192
+ joined = "\n\n---\n\n".join(texts)
193
+ return f"### Prompt\n\n````\n{prompt}\n````\n\n### Output\n\n{joined}"
194
  except Exception as e:
195
  logger.exception("Generation failed: %s", e)
196
  return f"⚠️ Generation failed: {e}"
 
207
  که د موډ بدلول غواړئ لاندې د Mode selector څخه استفاده وکړئ.
208
  """
209
  )
 
210
  with gr.Row():
211
  with gr.Column(scale=2):
212
  instruction_dropdown = gr.Dropdown(
 
221
  label="لارښوونه",
222
  )
223
  input_text = gr.Textbox(lines=2, placeholder="اختیاري متن...", label="متن")
224
+ output = gr.Markdown(label="ځواب")
225
  generate_btn = gr.Button("جوړول", variant="primary")
226
  mode_selector = gr.Dropdown(
227
  choices=["off", "echo", "useless"],
 
229
  label="Mode (off=real, echo=return prompt, useless=fixed)",
230
  interactive=True,
231
  )
232
+ status_box = gr.Markdown(value="Loading status pending...", label="Status")
233
+ refresh_status = gr.Button("Refresh Status")
234
 
235
  with gr.Column(scale=1):
236
  gr.Markdown("### د تولید تنظیمات")
 
243
 
244
  instruction_dropdown.change(lambda x: x, inputs=instruction_dropdown, outputs=instruction_textbox)
245
 
246
+ def refresh():
247
+ return f"**Device:** {'GPU' if _detect_device() != -1 else 'CPU'} | **Offline:** {os.getenv('HF_HUB_OFFLINE','0')} | **Env Mode:** {ECHO_MODE}"
248
+
249
+ refresh_status.click(fn=refresh, inputs=None, outputs=status_box)
250
+
251
  generate_btn.click(
252
  fn=predict,
253
  inputs=[instruction_textbox, input_text, max_new_tokens, num_beams, do_sample, temperature, top_p, num_return_sequences, mode_selector],
254
  outputs=output,
255
  )
256
 
257
+ # Model load banner shown after interface loads (async)
258
+ def _post_load():
259
+ return "✅ Model interface ready. If this is the first run and model wasn't cached, initial generation may still warm up."
260
+ demo.load(_post_load, inputs=None, outputs=status_box, every=None)
261
+
262
  return demo
263
 
264