mfallahian commited on
Commit
62079b1
·
verified ·
1 Parent(s): 4c72764

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -11
app.py CHANGED
@@ -1,6 +1,6 @@
1
  from __future__ import annotations
2
 
3
- from typing import Dict, List
4
 
5
  import gradio as gr
6
 
@@ -32,7 +32,7 @@ def update_play_options(state_label: str):
32
 
33
  def toggle_online_focus(is_enabled: bool):
34
  """Toggle visibility of online focus textbox."""
35
- return gr.Textbox(visible=is_enabled)
36
 
37
 
38
  def workflow_markdown() -> str:
@@ -145,6 +145,98 @@ def _generate_play_plan_output_wrapper(
145
  )
146
 
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  def build_app() -> gr.Blocks:
149
  default_group = next(iter(TASK_GROUPS))
150
  default_task = TASK_GROUPS[default_group][0]
@@ -189,6 +281,7 @@ def build_app() -> gr.Blocks:
189
  label="Online search focus (optional)",
190
  placeholder="e.g., latest homeowner grant updates and insurer discount guidance",
191
  visible=False,
 
192
  )
193
  clear_chat = gr.Button("Clear chat")
194
  related_charts = gr.Gallery(
@@ -219,7 +312,7 @@ def build_app() -> gr.Blocks:
219
  ask_chat = gr.Button("Ask", variant="primary")
220
  # chat_sources = gr.Code(label="Source trace", language="markdown", lines=10, show_label=True)
221
  chat_sources = gr.Markdown(label="Source trace", value="Sources will appear here.", padding=True)
222
- chat_search_online.change(toggle_online_focus, inputs=chat_search_online, outputs=chat_online_focus)
223
  ask_chat.click(
224
  fn=_ask_report_chat_wrapper,
225
  inputs=[chat_input, chatbox, chat_states, chat_search_online, chat_online_focus],
@@ -303,19 +396,20 @@ def build_app() -> gr.Blocks:
303
  label="Online search focus (optional)",
304
  placeholder="e.g., latest Louisiana grant updates and insurer discount guidance",
305
  visible=False,
 
306
  )
307
  generate_task = gr.Button("Generate", variant="primary")
308
 
309
- with gr.Column(scale=5):
310
  task_output_m = gr.Markdown(label="Report", value="Generated output will appear here.", padding=True)
311
  task_output = gr.Code(label="Report", value="Generated output will appear here.",language="markdown", lines=13, show_label=True, visible=False,)
312
  gr.Markdown("---")
313
  task_sources = gr.Markdown(label="Source trace", value="Sources will appear here.", padding=True)
314
 
315
  task_group.change(update_task_type_options, inputs=task_group, outputs=task_type)
316
- search_online.change(toggle_online_focus, inputs=search_online, outputs=online_focus)
317
  generate_task.click(
318
- fn=_generate_task_output_wrapper,
319
  inputs=[
320
  task_group,
321
  task_type,
@@ -331,7 +425,8 @@ def build_app() -> gr.Blocks:
331
  online_focus,
332
  temperature,
333
  ],
334
- outputs=[task_output, task_output_m, task_sources],
 
335
  )
336
 
337
  with gr.Tab("Playbook Planner"):
@@ -374,6 +469,7 @@ def build_app() -> gr.Blocks:
374
  label="Online search focus (optional)",
375
  placeholder="e.g., best practices for contractor co-marketing in Louisiana",
376
  visible=False,
 
377
  )
378
  play_temperature = gr.Slider(
379
  minimum=0.0,
@@ -394,9 +490,9 @@ def build_app() -> gr.Blocks:
394
  gr.Markdown("---")
395
  play_sources = gr.Markdown(label="Source trace", value="Sources will appear here.", padding=True)
396
  state_single.change(update_play_options, inputs=state_single, outputs=play_selector)
397
- play_search_online.change(toggle_online_focus, inputs=play_search_online, outputs=play_online_focus)
398
  generate_play.click(
399
- fn=_generate_play_plan_output_wrapper,
400
  inputs=[
401
  state_single,
402
  play_selector,
@@ -408,7 +504,8 @@ def build_app() -> gr.Blocks:
408
  play_online_focus,
409
  play_temperature,
410
  ],
411
- outputs=[play_output, play_output_m, play_sources],
 
412
  )
413
 
414
 
@@ -428,4 +525,4 @@ demo = build_app()
428
 
429
 
430
  if __name__ == "__main__":
431
- demo.queue(default_concurrency_limit=4).launch(theme=gr.themes.Soft(), css=CUSTOM_CSS,)
 
1
  from __future__ import annotations
2
 
3
+ from typing import Dict, Iterator, List, Tuple
4
 
5
  import gradio as gr
6
 
 
32
 
33
  def toggle_online_focus(is_enabled: bool):
34
  """Toggle visibility of online focus textbox."""
35
+ return gr.update(visible=is_enabled)
36
 
37
 
38
  def workflow_markdown() -> str:
 
145
  )
146
 
147
 
148
+ def _generate_task_output_stream(
149
+ task_group: str,
150
+ task_type: str,
151
+ state_labels: List[str],
152
+ objective: str,
153
+ audience_notes: str,
154
+ channels: List[str],
155
+ tone: str,
156
+ output_format: str,
157
+ idea_count: int,
158
+ include_kpis: bool,
159
+ search_online: bool,
160
+ online_focus: str,
161
+ temperature: float,
162
+ ) -> Iterator[Tuple[str, str, str, dict]]:
163
+ """Stream task generation progress to UI before final content arrives."""
164
+ yield (
165
+ "",
166
+ "Generating report... this usually takes 1 to 3 minutes.",
167
+ "Collecting report snippets and source trace...",
168
+ gr.update(interactive=False),
169
+ )
170
+ try:
171
+ report_code, report_markdown, source_markdown = _generate_task_output_wrapper(
172
+ task_group=task_group,
173
+ task_type=task_type,
174
+ state_labels=state_labels,
175
+ objective=objective,
176
+ audience_notes=audience_notes,
177
+ channels=channels,
178
+ tone=tone,
179
+ output_format=output_format,
180
+ idea_count=idea_count,
181
+ include_kpis=include_kpis,
182
+ search_online=search_online,
183
+ online_focus=online_focus,
184
+ temperature=temperature,
185
+ )
186
+ except Exception as exc:
187
+ report_code = ""
188
+ report_markdown = f"Generation failed: {exc}"
189
+ source_markdown = ""
190
+ yield (
191
+ report_code,
192
+ report_markdown,
193
+ source_markdown,
194
+ gr.update(interactive=True),
195
+ )
196
+
197
+
198
+ def _generate_play_plan_output_stream(
199
+ state_label: str,
200
+ play_title: str,
201
+ objective: str,
202
+ constraints: str,
203
+ horizon: str,
204
+ budget_level: str,
205
+ search_online: bool,
206
+ online_focus: str,
207
+ temperature: float,
208
+ ) -> Iterator[Tuple[str, str, str, dict]]:
209
+ """Stream play-plan generation progress to UI before final content arrives."""
210
+ yield (
211
+ "",
212
+ "Generating play plan... this usually takes 1 to 3 minutes.",
213
+ "Collecting report snippets and source trace...",
214
+ gr.update(interactive=False),
215
+ )
216
+ try:
217
+ report_code, report_markdown, source_markdown = _generate_play_plan_output_wrapper(
218
+ state_label=state_label,
219
+ play_title=play_title,
220
+ objective=objective,
221
+ constraints=constraints,
222
+ horizon=horizon,
223
+ budget_level=budget_level,
224
+ search_online=search_online,
225
+ online_focus=online_focus,
226
+ temperature=temperature,
227
+ )
228
+ except Exception as exc:
229
+ report_code = ""
230
+ report_markdown = f"Generation failed: {exc}"
231
+ source_markdown = ""
232
+ yield (
233
+ report_code,
234
+ report_markdown,
235
+ source_markdown,
236
+ gr.update(interactive=True),
237
+ )
238
+
239
+
240
  def build_app() -> gr.Blocks:
241
  default_group = next(iter(TASK_GROUPS))
242
  default_task = TASK_GROUPS[default_group][0]
 
281
  label="Online search focus (optional)",
282
  placeholder="e.g., latest homeowner grant updates and insurer discount guidance",
283
  visible=False,
284
+ value="",
285
  )
286
  clear_chat = gr.Button("Clear chat")
287
  related_charts = gr.Gallery(
 
312
  ask_chat = gr.Button("Ask", variant="primary")
313
  # chat_sources = gr.Code(label="Source trace", language="markdown", lines=10, show_label=True)
314
  chat_sources = gr.Markdown(label="Source trace", value="Sources will appear here.", padding=True)
315
+ chat_search_online.change(fn=toggle_online_focus, inputs=chat_search_online, outputs=chat_online_focus, show_progress="hidden")
316
  ask_chat.click(
317
  fn=_ask_report_chat_wrapper,
318
  inputs=[chat_input, chatbox, chat_states, chat_search_online, chat_online_focus],
 
396
  label="Online search focus (optional)",
397
  placeholder="e.g., latest Louisiana grant updates and insurer discount guidance",
398
  visible=False,
399
+ value="",
400
  )
401
  generate_task = gr.Button("Generate", variant="primary")
402
 
403
+ with gr.Column(scale=5, ):
404
  task_output_m = gr.Markdown(label="Report", value="Generated output will appear here.", padding=True)
405
  task_output = gr.Code(label="Report", value="Generated output will appear here.",language="markdown", lines=13, show_label=True, visible=False,)
406
  gr.Markdown("---")
407
  task_sources = gr.Markdown(label="Source trace", value="Sources will appear here.", padding=True)
408
 
409
  task_group.change(update_task_type_options, inputs=task_group, outputs=task_type)
410
+ search_online.change(fn=toggle_online_focus, inputs=search_online, outputs=online_focus, show_progress="hidden")
411
  generate_task.click(
412
+ fn=_generate_task_output_stream,
413
  inputs=[
414
  task_group,
415
  task_type,
 
425
  online_focus,
426
  temperature,
427
  ],
428
+ outputs=[task_output, task_output_m, task_sources, generate_task],
429
+ show_progress="full",
430
  )
431
 
432
  with gr.Tab("Playbook Planner"):
 
469
  label="Online search focus (optional)",
470
  placeholder="e.g., best practices for contractor co-marketing in Louisiana",
471
  visible=False,
472
+ value="",
473
  )
474
  play_temperature = gr.Slider(
475
  minimum=0.0,
 
490
  gr.Markdown("---")
491
  play_sources = gr.Markdown(label="Source trace", value="Sources will appear here.", padding=True)
492
  state_single.change(update_play_options, inputs=state_single, outputs=play_selector)
493
+ play_search_online.change(fn=toggle_online_focus, inputs=play_search_online, outputs=play_online_focus, show_progress="hidden")
494
  generate_play.click(
495
+ fn=_generate_play_plan_output_stream,
496
  inputs=[
497
  state_single,
498
  play_selector,
 
504
  play_online_focus,
505
  play_temperature,
506
  ],
507
+ outputs=[play_output, play_output_m, play_sources, generate_play],
508
+ show_progress="full",
509
  )
510
 
511
 
 
525
 
526
 
527
  if __name__ == "__main__":
528
+ demo.queue(default_concurrency_limit=10).launch(theme=gr.themes.Soft(), css=CUSTOM_CSS,)