Spaces:
Runtime error
Runtime error
File size: 27,090 Bytes
285d72c 8338bd9 af6f39f 285d72c 8e9804e 8338bd9 af6f39f 8338bd9 af6f39f 8338bd9 af6f39f 8338bd9 28f11ce 8338bd9 28f11ce 8338bd9 af6f39f 28f11ce af6f39f 28f11ce 68997b5 28f11ce 68997b5 28f11ce 9673f15 28f11ce 285d72c 68997b5 8338bd9 28f11ce 8338bd9 28f11ce 46f84a2 800c4e4 68997b5 8338bd9 217d90f 8338bd9 96393a7 8338bd9 28f11ce 8338bd9 8e9804e 8338bd9 96393a7 8338bd9 68997b5 8338bd9 68997b5 28f11ce 8338bd9 af6f39f 28f11ce 8338bd9 96393a7 8338bd9 217d90f 8338bd9 217d90f 28f11ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | from Project import *
from google_drive import *
from notion import upload_to_notion
from state import state
import gradio as gr
import json
from prompt_configs import PROMPTS
from typing import Any, Generator, Tuple
import pandas as pd
from io import StringIO
def generate_step_1(progress=gr.Progress()) -> Generator[Tuple[str, list], Any, None]:
"""Generate content using the Project instance"""
try:
state.step1_results = []
for status_msg, results in state.quotation_project.generate_prd_and_components(progress):
state.step1_results = results
yield status_msg, results
except Exception as e:
print(f"Error during generation: {str(e)}")
yield f"Error during generation: {str(e)}", []
def generate_step_2a(progress=gr.Progress()):
try:
state.step2a_results = []
for status_msg , results , quotation_cost in state.quotation_project.generate_mandays_estimate(progress):
state.step2a_results = results
yield status_msg, results , quotation_cost
except Exception as e:
print(f"Error during generation: {str(e)}")
yield f"Error during generation: {str(e)}", []
def generate_step_2b(progress=gr.Progress()):
try:
state.step2b_results = []
for status_msg , results in state.quotation_project.analyze_mvp_components(progress):
state.step2b_results = results
yield status_msg, results
except Exception as e:
print(f"Error during generation: {str(e)}")
yield f"Error during generation: {str(e)}", []
def generate_step_2c(progress=gr.Progress()):
try:
state.step2c_results = []
for status_msg , results in state.quotation_project.recalculate_mvp_mandays(progress):
state.step2c_results = results
yield status_msg, results
except Exception as e:
print(f"Error during generation: {str(e)}")
yield f"Error during generation: {str(e)}", []
def generate_step_2d(progress=gr.Progress()):
try:
state.step2d_results = []
for status_msg , results , quotation_cost in state.quotation_project.generate_mvp_mandays(progress):
state.step2d_results = results
yield status_msg, results , quotation_cost
except Exception as e:
print(f"Error during generation: {str(e)}")
yield f"Error during generation: {str(e)}", []
def generate_step_3(progress=gr.Progress()):
try:
state.step3_results = []
for status_msg , results in state.quotation_project.generate_final_documentation(progress):
state.step3_results = results
yield status_msg, results
except Exception as e:
print(f"Error during generation: {str(e)}")
yield f"Error during generation: {str(e)}", []
def recalculate_estimates(progress=gr.Progress()):
"""Handler for recalculate button click"""
try:
# Recalculate estimates
updated_summary = state.quotation_project.recalculate_mandays_costs()
return updated_summary
except Exception as e:
print(f"Error during recalculation: {str(e)}")
return gr.Markdown(value=f"Error during recalculation: {str(e)}")
def setup_all_handlers(step_buttons, all_components, progress_update, quotation_cost=None, recalc_btn=None, upload_drive_btn=None, upload_notion_btn=None, project_name=None, step1_results=None, step2a_results=None, step2b_results=None, step2c_results=None, step2d_results=None , step3_results=None):
"""Set up all step handlers with the provided UI components"""
try:
# Set up Step 1 handler
step_buttons['Step 1 : Scope & Components'].click(
fn=generate_step_1,
outputs=[progress_update, step1_results],
queue=True
)
step_buttons['Step 2.1 : Generate Mandays'].click(
fn=generate_step_2a,
outputs=[progress_update, step2a_results , quotation_cost],
queue=True
)
step_buttons['Step 2.2 : Analyze MVP Components'].click(
fn=generate_step_2b,
outputs=[progress_update , step2b_results],
queue=True
)
step_buttons['Step 2.3 : Recalculate MVP Mandays'].click(
fn=generate_step_2c,
outputs=[progress_update , step2c_results],
queue=True
)
step_buttons['Step 2.4 : Generate MVP Mandays'].click(
fn=generate_step_2d,
outputs=[progress_update , step2d_results , quotation_cost],
queue=True
)
step_buttons['Step 3 : Final Documentation'].click(
fn=generate_step_3,
outputs=[progress_update , step3_results],
queue=True
)
recalc_btn.click(
fn=recalculate_estimates,
outputs=[quotation_cost],
)
upload_drive_btn.click(
fn=upload_to_gdrive,
inputs=[project_name],
outputs=[progress_update]
)
upload_notion_btn.click(
fn=upload_to_notion,
inputs=[project_name],
outputs=[progress_update]
)
except Exception as e:
print(f"Error setting up handlers: {str(e)}")
return None
def create_step1_render(step1_results):
@gr.render(inputs=[step1_results])
def render_step1_results(results):
if not results:
return [gr.Markdown("No results generated yet.")]
result_components = []
for result_item in results:
try:
function_name = result_item.get("function_name")
result = result_item.get("result")
prompt_config = PROMPTS.get(function_name)
if prompt_config:
accordion = gr.Accordion(
label=prompt_config.description,
open=False
)
with accordion:
text_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_text')), None)
markdown_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_markdown')), None)
# Create textbox and markdown components
textbox = gr.Textbox(
value=result,
label=text_config.label,
lines=10,
interactive=True
)
markdown = gr.Markdown(
value=result,
show_copy_button=True,
label=markdown_config.label,
elem_classes=["scrollable-markdown"]
)
def update_project_attribute(new_text , fn_name = function_name):
for output in PROMPTS[fn_name].outputs:
setattr(state.quotation_project , output , new_text)
return new_text
textbox.change(fn=update_project_attribute, inputs=[textbox] , outputs=[markdown])
result_components.append(accordion)
continue
except Exception as e:
print(f"Error processing result: {str(e)}")
result_components.append(
gr.Markdown(f"Error displaying result: {str(e)}")
)
return result_components or [gr.Markdown("No valid results to display.")]
return render_step1_results
def create_step2a_render(step2a_results):
@gr.render(inputs=[step2a_results])
def render_step2a_results(results):
if not results:
return [gr.Markdown("No results generated yet.")]
result_components = []
for result_item in results:
try:
function_name = result_item.get("function_name")
result_data = result_item.get("result", {})
prompt_config = PROMPTS.get(function_name)
if prompt_config:
accordion = gr.Accordion(
label=prompt_config.description,
open=False
)
with accordion:
# Convert the data to pandas DataFrame first
try:
if isinstance(result_data, str) and ',' in result_data:
# Handle CSV string
clean_csv = result_data.replace('```csv', '').replace('```', '')
df = pd.read_csv(StringIO(clean_csv))
else:
# Handle dictionary data
records = result_data.get(function_name, [])
df = pd.DataFrame(records)
if not df.empty:
df_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_dataframe')), None)
df_component = gr.DataFrame(
value=df,
label=df_config.label if df_config else f"{function_name} Results",
interactive=df_config.interactive if df_config else False,
wrap=True
)
def update_project_attribute(updated_df, fn_name=function_name):
try:
# Convert DataFrame to dictionary format
updated_records = updated_df.to_dict('records')
updated_result = {fn_name: updated_records}
for output in PROMPTS[fn_name].outputs:
setattr(state.quotation_project, output, updated_result)
return updated_df
except Exception as e:
print(f"Error updating attribute: {str(e)}")
return updated_df
df_component.change(
fn=update_project_attribute,
inputs=[df_component],
outputs=[df_component]
)
else:
gr.Markdown("No data available")
except Exception as e:
print(f"Error processing DataFrame: {str(e)}")
gr.Markdown(f"Error creating DataFrame: {str(e)}")
result_components.append(accordion)
continue
except Exception as e:
print(f"Error processing result: {str(e)}")
result_components.append(
gr.Markdown(f"Error displaying result: {str(e)}")
)
return result_components or [gr.Markdown("No valid results to display.")]
return render_step2a_results
def create_step2b_render(step2b_results):
@gr.render(inputs=[step2b_results])
def render_step2b_results(results):
if not results:
return [gr.Markdown("No results generated yet.")]
result_components = []
for result_item in results:
try:
function_name = result_item.get("function_name")
result_data = result_item.get("result", {})
prompt_config = PROMPTS.get(function_name)
if prompt_config:
accordion = gr.Accordion(
label=prompt_config.description,
open=False
)
with accordion:
# Convert the data to pandas DataFrame first
try:
if isinstance(result_data, str) and ',' in result_data:
# Handle CSV string
clean_csv = result_data.replace('```csv', '').replace('```', '')
df = pd.read_csv(StringIO(clean_csv))
else:
# Handle dictionary data
records = result_data.get(function_name, [])
df = pd.DataFrame(records)
if not df.empty:
df_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_dataframe')), None)
df_component = gr.DataFrame(
value=df,
label=df_config.label if df_config else f"{function_name} Results",
interactive=df_config.interactive if df_config else False,
wrap=True
)
def update_project_attribute(updated_df, fn_name=function_name):
try:
# Convert DataFrame to dictionary format
updated_records = updated_df.to_dict('records')
updated_result = {fn_name: updated_records}
for output in PROMPTS[fn_name].outputs:
setattr(state.quotation_project, output, updated_result)
return updated_df
except Exception as e:
print(f"Error updating attribute: {str(e)}")
return updated_df
df_component.change(
fn=update_project_attribute,
inputs=[df_component],
outputs=[df_component]
)
else:
gr.Markdown("No data available")
except Exception as e:
print(f"Error processing DataFrame: {str(e)}")
gr.Markdown(f"Error creating DataFrame: {str(e)}")
result_components.append(accordion)
continue
except Exception as e:
print(f"Error processing result: {str(e)}")
result_components.append(
gr.Markdown(f"Error displaying result: {str(e)}")
)
return result_components or [gr.Markdown("No valid results to display.")]
return render_step2b_results
def create_step2c_render(step2c_results):
@gr.render(inputs=[step2c_results])
def render_step2c_results(results):
if not results:
return [gr.Markdown("No results generated yet.")]
result_components = []
for result_item in results:
try:
function_name = result_item.get("function_name")
result = result_item.get("result")
prompt_config = PROMPTS.get(function_name)
if prompt_config:
accordion = gr.Accordion(
label=prompt_config.description,
open=False
)
with accordion:
text_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_text')), None)
markdown_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_markdown')), None)
# Create textbox and markdown components
textbox = gr.Textbox(
value=result,
label=text_config.label,
lines=10,
interactive=True
)
markdown = gr.Markdown(
value=result,
show_copy_button=True,
label=markdown_config.label,
elem_classes=["scrollable-markdown"]
)
def update_project_attribute(new_text , fn_name = function_name):
for output in PROMPTS[fn_name].outputs:
setattr(state.quotation_project , output , new_text)
return new_text
textbox.change(fn=update_project_attribute, inputs=[textbox] , outputs=[markdown])
result_components.append(accordion)
continue
except Exception as e:
print(f"Error processing result: {str(e)}")
result_components.append(
gr.Markdown(f"Error displaying result: {str(e)}")
)
return result_components or [gr.Markdown("No valid results to display.")]
return render_step2c_results
def create_step2d_render(step2d_results):
@gr.render(inputs=[step2d_results])
def render_step2d_results(results):
if not results:
return [gr.Markdown("No results generated yet.")]
result_components = []
for result_item in results:
try:
function_name = result_item.get("function_name")
result_data = result_item.get("result", {})
prompt_config = PROMPTS.get(function_name)
if prompt_config:
accordion = gr.Accordion(
label=prompt_config.description,
open=False
)
with accordion:
try:
# Handle each section separately
for section_name, section_data in result_data.items():
if isinstance(section_data, list):
df = pd.DataFrame(section_data)
if not df.empty:
# Get appropriate DataFrame config based on section
df_config = None
if 'MVP Plan_Test' in section_name:
df_config = next((config for key, config in prompt_config.ui.items()
if 'plan_test_mandays_dataframe' in key), None)
elif 'MVP Dev' in section_name:
df_config = next((config for key, config in prompt_config.ui.items()
if 'dev_mandays_dataframe' in key), None)
elif 'MVP Intents' in section_name:
df_config = next((config for key, config in prompt_config.ui.items()
if 'intents_mandays_dataframe' in key), None)
df_component = gr.DataFrame(
value=df,
label=df_config.label if df_config else section_name,
interactive=df_config.interactive if df_config else False,
wrap=True
)
def update_project_attribute(updated_df, fn_name=function_name, section=section_name):
try:
# Get current result data
current_result = getattr(state.quotation_project,
PROMPTS[fn_name].outputs[0], {})
if not isinstance(current_result, dict):
current_result = {}
# Update specific section
current_result[section] = updated_df.to_dict('records')
# Update project attribute
for output in PROMPTS[fn_name].outputs:
setattr(state.quotation_project, output, current_result)
return updated_df
except Exception as e:
print(f"Error updating attribute for {section}: {str(e)}")
return updated_df
df_component.change(
fn=update_project_attribute,
inputs=[df_component],
outputs=[df_component]
)
else:
gr.Markdown(f"No data available for {section_name}")
elif 'error' in section_name:
gr.Markdown(f"Error in section: {section_data}")
except Exception as e:
print(f"Error processing DataFrames: {str(e)}")
gr.Markdown(f"Error creating DataFrames: {str(e)}")
result_components.append(accordion)
continue
except Exception as e:
print(f"Error processing result: {str(e)}")
result_components.append(
gr.Markdown(f"Error displaying result: {str(e)}")
)
return result_components or [gr.Markdown("No valid results to display.")]
return render_step2d_results
def create_step3_render(step3_results):
@gr.render(inputs=[step3_results])
def render_step3_results(results):
if not results:
return [gr.Markdown("No results generated yet.")]
result_components = []
for result_item in results:
try:
function_name = result_item.get("function_name")
result = result_item.get("result")
prompt_config = PROMPTS.get(function_name)
if prompt_config:
accordion = gr.Accordion(
label=prompt_config.description,
open=False
)
with accordion:
text_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_text')), None)
markdown_config = next((config for key, config in prompt_config.ui.items()
if key.endswith('_markdown')), None)
# Create textbox and markdown components
textbox = gr.Textbox(
value=result,
label=text_config.label,
lines=10,
interactive=True
)
markdown = gr.Markdown(
value=result,
show_copy_button=True,
label=markdown_config.label,
elem_classes=["scrollable-markdown"]
)
def update_project_attribute(new_text , fn_name = function_name):
for output in PROMPTS[fn_name].outputs:
setattr(state.quotation_project , output , new_text)
return new_text
textbox.change(fn=update_project_attribute, inputs=[textbox] , outputs=[markdown])
result_components.append(accordion)
continue
except Exception as e:
print(f"Error processing result: {str(e)}")
result_components.append(
gr.Markdown(f"Error displaying result: {str(e)}")
)
return result_components or [gr.Markdown("No valid results to display.")]
return render_step3_results |