nananie143 commited on
Commit
bc8d692
·
1 Parent(s): 189fe15

Fixed stream output and app generation

Browse files
Files changed (1) hide show
  1. app.py +101 -31
app.py CHANGED
@@ -1009,22 +1009,82 @@ async def multi_agent_workflow(requirements: str) -> str:
1009
  raise
1010
 
1011
  # Update the app_generator function to handle async execution
1012
- async def app_generator(requirements: str) -> str:
1013
- """
1014
- Generate an app based on the provided requirements using AI Flows.
1015
- Args:
1016
- requirements (str): App requirements.
1017
- Returns:
1018
- str: Generated app code and API details.
1019
- """
1020
  try:
1021
  # Execute the multi-agent workflow
1022
  result = await multi_agent_workflow(requirements)
1023
- return result
 
 
 
1024
  except Exception as e:
1025
  logger.error(f"Failed to generate app: {str(e)}")
1026
  raise
1027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1028
  class StreamHandler:
1029
  """Handles streaming output for the Gradio interface."""
1030
 
@@ -1124,34 +1184,44 @@ with gr.Blocks(theme=gr.themes.Soft()) as ui:
1124
 
1125
  for msg, status in progress.tqdm(phases):
1126
  stream_handler.update(msg, status)
1127
- yield None, None, stream_handler.output[-1], status
1128
  await asyncio.sleep(1) # Non-blocking sleep
1129
 
1130
  # Generate the app
1131
  stream_handler.update(" Running AI Flow system...", "Processing")
1132
- result = await app_generator(requirements)
1133
-
1134
- # Extract download path and logs
1135
- download_path = None
1136
- logs = []
1137
 
1138
- for line in result.split('\n'):
1139
- if line.startswith("To download your project, use this path:"):
1140
- download_path = line.split(": ")[1].strip()
1141
- stream_handler.update(f" Generated download file: {Path(download_path).name}", "Processing")
1142
- if line.startswith("2025-") or line.startswith("INFO") or line.startswith("ERROR"):
1143
- logs.append(line)
1144
- stream_handler.update(line.split(" - ")[-1], "Processing")
1145
- yield None, None, "\n".join(stream_handler.output), "Processing"
1146
- await asyncio.sleep(0.1) # Small delay to prevent UI freezing
1147
-
1148
- if download_path and Path(download_path).exists():
1149
- stream_handler.update(" App generated successfully! Download is ready.", "Complete")
1150
- yield result, download_path, "\n".join(stream_handler.output), "Complete"
1151
- else:
1152
- error_msg = " Failed to generate download file"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1153
  stream_handler.update(error_msg, "Error")
1154
- yield result, None, "\n".join(stream_handler.output), "Error"
1155
 
1156
  except Exception as e:
1157
  error_msg = f" Error: {str(e)}"
 
1009
  raise
1010
 
1011
  # Update the app_generator function to handle async execution
1012
+ async def app_generator(requirements: str) -> Dict[str, str]:
1013
+ """Generate an app based on the provided requirements using AI Flows."""
 
 
 
 
 
 
1014
  try:
1015
  # Execute the multi-agent workflow
1016
  result = await multi_agent_workflow(requirements)
1017
+ return {
1018
+ "output": result,
1019
+ "download_path": None
1020
+ }
1021
  except Exception as e:
1022
  logger.error(f"Failed to generate app: {str(e)}")
1023
  raise
1024
 
1025
+ async def stream_output(requirements, progress=gr.Progress()):
1026
+ """Stream the output during app generation."""
1027
+ try:
1028
+ # Initialize
1029
+ stream_handler.update(" Starting app generation...", "Initializing")
1030
+ yield "Starting...", None, " Starting app generation...", "Initializing"
1031
+
1032
+ # Update progress
1033
+ phases = [
1034
+ (" Analyzing requirements...", "Analyzing"),
1035
+ (" Generating architecture...", "Designing"),
1036
+ (" Creating project structure...", "Creating"),
1037
+ (" Implementing features...", "Implementing"),
1038
+ (" Finalizing...", "Finalizing")
1039
+ ]
1040
+
1041
+ for msg, status in progress.tqdm(phases):
1042
+ stream_handler.update(msg, status)
1043
+ yield None, None, "\n".join(stream_handler.output), status
1044
+ await asyncio.sleep(1) # Non-blocking sleep
1045
+
1046
+ # Generate the app
1047
+ stream_handler.update(" Running AI Flow system...", "Processing")
1048
+ yield None, None, "\n".join(stream_handler.output), "Processing"
1049
+
1050
+ try:
1051
+ # Run the app generator with a timeout
1052
+ result = await asyncio.wait_for(
1053
+ app_generator(requirements),
1054
+ timeout=60 # 60 second timeout
1055
+ )
1056
+
1057
+ if result:
1058
+ # Create download file
1059
+ try:
1060
+ download_path = result.get("download_path")
1061
+ if download_path:
1062
+ stream_handler.update(" Generation completed successfully!", "Success")
1063
+ yield result.get("output", ""), download_path, "\n".join(stream_handler.output), "Success"
1064
+ else:
1065
+ error_msg = " Failed to create download file"
1066
+ stream_handler.update(error_msg, "Error")
1067
+ yield result.get("output", ""), None, "\n".join(stream_handler.output), "Error"
1068
+ except Exception as e:
1069
+ error_msg = f" Error creating download: {str(e)}"
1070
+ stream_handler.update(error_msg, "Error")
1071
+ yield result.get("output", ""), None, "\n".join(stream_handler.output), "Error"
1072
+ else:
1073
+ error_msg = " Failed to generate app"
1074
+ stream_handler.update(error_msg, "Error")
1075
+ yield "", None, "\n".join(stream_handler.output), "Error"
1076
+
1077
+ except asyncio.TimeoutError:
1078
+ error_msg = " Generation timed out after 60 seconds"
1079
+ stream_handler.update(error_msg, "Error")
1080
+ yield "", None, "\n".join(stream_handler.output), "Error"
1081
+
1082
+ except Exception as e:
1083
+ error_msg = f" Error: {str(e)}"
1084
+ logger.error(error_msg)
1085
+ stream_handler.update(error_msg, "Error")
1086
+ yield error_msg, None, "\n".join(stream_handler.output), "Error"
1087
+
1088
  class StreamHandler:
1089
  """Handles streaming output for the Gradio interface."""
1090
 
 
1184
 
1185
  for msg, status in progress.tqdm(phases):
1186
  stream_handler.update(msg, status)
1187
+ yield None, None, "\n".join(stream_handler.output), status
1188
  await asyncio.sleep(1) # Non-blocking sleep
1189
 
1190
  # Generate the app
1191
  stream_handler.update(" Running AI Flow system...", "Processing")
1192
+ yield None, None, "\n".join(stream_handler.output), "Processing"
 
 
 
 
1193
 
1194
+ try:
1195
+ # Run the app generator with a timeout
1196
+ result = await asyncio.wait_for(
1197
+ app_generator(requirements),
1198
+ timeout=60 # 60 second timeout
1199
+ )
1200
+
1201
+ if result:
1202
+ # Create download file
1203
+ try:
1204
+ download_path = result.get("download_path")
1205
+ if download_path:
1206
+ stream_handler.update(" Generation completed successfully!", "Success")
1207
+ yield result.get("output", ""), download_path, "\n".join(stream_handler.output), "Success"
1208
+ else:
1209
+ error_msg = " Failed to create download file"
1210
+ stream_handler.update(error_msg, "Error")
1211
+ yield result.get("output", ""), None, "\n".join(stream_handler.output), "Error"
1212
+ except Exception as e:
1213
+ error_msg = f" Error creating download: {str(e)}"
1214
+ stream_handler.update(error_msg, "Error")
1215
+ yield result.get("output", ""), None, "\n".join(stream_handler.output), "Error"
1216
+ else:
1217
+ error_msg = " Failed to generate app"
1218
+ stream_handler.update(error_msg, "Error")
1219
+ yield "", None, "\n".join(stream_handler.output), "Error"
1220
+
1221
+ except asyncio.TimeoutError:
1222
+ error_msg = " Generation timed out after 60 seconds"
1223
  stream_handler.update(error_msg, "Error")
1224
+ yield "", None, "\n".join(stream_handler.output), "Error"
1225
 
1226
  except Exception as e:
1227
  error_msg = f" Error: {str(e)}"