Spaces:
Runtime error
Runtime error
levisstrauss commited on
Commit Β·
0caeb0d
1
Parent(s): 4e9e142
Fix the download button
Browse files
app.py
CHANGED
|
@@ -940,7 +940,12 @@ def create_interface():
|
|
| 940 |
"""Create the Gradio interface."""
|
| 941 |
|
| 942 |
workflow = None
|
| 943 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 944 |
def validate_api_key(api_key: str) -> str:
|
| 945 |
"""Validate the API key format."""
|
| 946 |
if not api_key or len(api_key) < 20:
|
|
@@ -950,88 +955,96 @@ def create_interface():
|
|
| 950 |
def load_example(example_name: str) -> str:
|
| 951 |
"""Load an example product specification."""
|
| 952 |
return EXAMPLE_SPECS.get(example_name, "")
|
| 953 |
-
|
| 954 |
def run_planner(
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
) -> Tuple[str, str, str, str, str]:
|
| 959 |
"""Run the project planner workflow."""
|
| 960 |
-
|
| 961 |
if not api_key or len(api_key) < 20:
|
| 962 |
return (
|
| 963 |
"β Error: Please enter a valid OpenAI API key",
|
| 964 |
-
"", "", "", ""
|
| 965 |
)
|
| 966 |
-
|
| 967 |
if not product_spec or len(product_spec) < 100:
|
| 968 |
return (
|
| 969 |
"β Error: Please provide a product specification (minimum 100 characters)",
|
| 970 |
-
"", "", "", ""
|
| 971 |
)
|
| 972 |
-
|
| 973 |
try:
|
| 974 |
planner = ProjectPlannerWorkflow(api_key)
|
| 975 |
-
|
| 976 |
results = ("", "", "", "", "")
|
| 977 |
progress(0, desc="Starting workflow...")
|
| 978 |
-
|
| 979 |
step_progress = [0.1, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0]
|
| 980 |
step_idx = 0
|
| 981 |
-
|
| 982 |
for status, stories, features, tasks, plan in planner.run_workflow(product_spec):
|
| 983 |
if step_idx < len(step_progress):
|
| 984 |
progress(step_progress[step_idx], desc=status)
|
| 985 |
step_idx += 1
|
| 986 |
results = (status, stories, features, tasks, plan)
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
|
|
|
| 990 |
except Exception as e:
|
| 991 |
return (
|
| 992 |
f"β Error: {str(e)}",
|
| 993 |
-
"", "", "", ""
|
| 994 |
)
|
| 995 |
-
|
| 996 |
-
def export_markdown(
|
| 997 |
"""Export results as markdown."""
|
| 998 |
-
if not any([
|
|
|
|
| 999 |
return None
|
| 1000 |
-
|
| 1001 |
content = f"""# Project Plan Export
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1014 |
filename = f"project_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
|
| 1015 |
-
with open(filename, "w") as f:
|
| 1016 |
f.write(content)
|
| 1017 |
-
return filename
|
| 1018 |
-
|
| 1019 |
-
def export_json(
|
| 1020 |
"""Export results as JSON."""
|
| 1021 |
-
if not any([
|
|
|
|
| 1022 |
return None
|
| 1023 |
-
|
| 1024 |
data = {
|
| 1025 |
"generated_at": datetime.now().isoformat(),
|
| 1026 |
-
"user_stories":
|
| 1027 |
"features": features,
|
| 1028 |
"tasks": tasks
|
| 1029 |
}
|
| 1030 |
-
|
| 1031 |
filename = f"project_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 1032 |
-
with open(filename, "w") as f:
|
| 1033 |
-
json.dump(data, f, indent=2)
|
| 1034 |
-
return filename
|
| 1035 |
|
| 1036 |
# Build the interface
|
| 1037 |
with gr.Blocks(css=CUSTOM_CSS, title="AI Project Planner", theme=gr.themes.Base()) as app:
|
|
@@ -1132,7 +1145,7 @@ def create_interface():
|
|
| 1132 |
export_md_btn = gr.Button("π Download Markdown", variant="secondary")
|
| 1133 |
export_json_btn = gr.Button("π Download JSON", variant="secondary")
|
| 1134 |
|
| 1135 |
-
export_file = gr.File(label="Download", visible=False)
|
| 1136 |
|
| 1137 |
# Footer
|
| 1138 |
gr.HTML("""
|
|
@@ -1144,6 +1157,7 @@ def create_interface():
|
|
| 1144 |
</div>
|
| 1145 |
""")
|
| 1146 |
|
|
|
|
| 1147 |
# Event handlers
|
| 1148 |
generate_btn.click(
|
| 1149 |
fn=run_planner,
|
|
@@ -1153,20 +1167,23 @@ def create_interface():
|
|
| 1153 |
user_stories_output,
|
| 1154 |
features_output,
|
| 1155 |
tasks_output,
|
| 1156 |
-
full_plan_output
|
|
|
|
|
|
|
|
|
|
| 1157 |
],
|
| 1158 |
show_progress=True
|
| 1159 |
)
|
| 1160 |
-
|
| 1161 |
export_md_btn.click(
|
| 1162 |
fn=export_markdown,
|
| 1163 |
-
inputs=[
|
| 1164 |
outputs=[export_file]
|
| 1165 |
)
|
| 1166 |
-
|
| 1167 |
export_json_btn.click(
|
| 1168 |
fn=export_json,
|
| 1169 |
-
inputs=[
|
| 1170 |
outputs=[export_file]
|
| 1171 |
)
|
| 1172 |
|
|
|
|
| 940 |
"""Create the Gradio interface."""
|
| 941 |
|
| 942 |
workflow = None
|
| 943 |
+
|
| 944 |
+
# Add these state variables
|
| 945 |
+
stored_stories = gr.State("")
|
| 946 |
+
stored_features = gr.State("")
|
| 947 |
+
stored_tasks = gr.State("")
|
| 948 |
+
|
| 949 |
def validate_api_key(api_key: str) -> str:
|
| 950 |
"""Validate the API key format."""
|
| 951 |
if not api_key or len(api_key) < 20:
|
|
|
|
| 955 |
def load_example(example_name: str) -> str:
|
| 956 |
"""Load an example product specification."""
|
| 957 |
return EXAMPLE_SPECS.get(example_name, "")
|
| 958 |
+
|
| 959 |
def run_planner(
|
| 960 |
+
api_key: str,
|
| 961 |
+
product_spec: str,
|
| 962 |
+
progress=gr.Progress()
|
| 963 |
+
) -> Tuple[str, str, str, str, str, str, str, str]:
|
| 964 |
"""Run the project planner workflow."""
|
| 965 |
+
|
| 966 |
if not api_key or len(api_key) < 20:
|
| 967 |
return (
|
| 968 |
"β Error: Please enter a valid OpenAI API key",
|
| 969 |
+
"", "", "", "", "", "", ""
|
| 970 |
)
|
| 971 |
+
|
| 972 |
if not product_spec or len(product_spec) < 100:
|
| 973 |
return (
|
| 974 |
"β Error: Please provide a product specification (minimum 100 characters)",
|
| 975 |
+
"", "", "", "", "", "", ""
|
| 976 |
)
|
| 977 |
+
|
| 978 |
try:
|
| 979 |
planner = ProjectPlannerWorkflow(api_key)
|
| 980 |
+
|
| 981 |
results = ("", "", "", "", "")
|
| 982 |
progress(0, desc="Starting workflow...")
|
| 983 |
+
|
| 984 |
step_progress = [0.1, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0]
|
| 985 |
step_idx = 0
|
| 986 |
+
|
| 987 |
for status, stories, features, tasks, plan in planner.run_workflow(product_spec):
|
| 988 |
if step_idx < len(step_progress):
|
| 989 |
progress(step_progress[step_idx], desc=status)
|
| 990 |
step_idx += 1
|
| 991 |
results = (status, stories, features, tasks, plan)
|
| 992 |
+
|
| 993 |
+
# Return: status, display stories, display features, display tasks, display plan, stored stories, stored features, stored tasks
|
| 994 |
+
return (results[0], results[1], results[2], results[3], results[4], results[1], results[2], results[3])
|
| 995 |
+
|
| 996 |
except Exception as e:
|
| 997 |
return (
|
| 998 |
f"β Error: {str(e)}",
|
| 999 |
+
"", "", "", "", "", "", ""
|
| 1000 |
)
|
| 1001 |
+
|
| 1002 |
+
def export_markdown(stories: str, features: str, tasks: str):
|
| 1003 |
"""Export results as markdown."""
|
| 1004 |
+
if not any([stories, features, tasks]):
|
| 1005 |
+
gr.Warning("No content to export. Generate a plan first.")
|
| 1006 |
return None
|
| 1007 |
+
|
| 1008 |
content = f"""# Project Plan Export
|
| 1009 |
+
Generated: {datetime.now().strftime("%Y-%m-%d %H:%M")}
|
| 1010 |
+
|
| 1011 |
+
## User Stories
|
| 1012 |
+
|
| 1013 |
+
{stories}
|
| 1014 |
+
|
| 1015 |
+
## Product Features
|
| 1016 |
+
|
| 1017 |
+
{features}
|
| 1018 |
+
|
| 1019 |
+
## Engineering Tasks
|
| 1020 |
+
|
| 1021 |
+
{tasks}
|
| 1022 |
+
|
| 1023 |
+
---
|
| 1024 |
+
*Exported from AI Project Planner*
|
| 1025 |
+
"""
|
| 1026 |
filename = f"project_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
|
| 1027 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 1028 |
f.write(content)
|
| 1029 |
+
return gr.File(value=filename, visible=True)
|
| 1030 |
+
|
| 1031 |
+
def export_json(stories: str, features: str, tasks: str):
|
| 1032 |
"""Export results as JSON."""
|
| 1033 |
+
if not any([stories, features, tasks]):
|
| 1034 |
+
gr.Warning("No content to export. Generate a plan first.")
|
| 1035 |
return None
|
| 1036 |
+
|
| 1037 |
data = {
|
| 1038 |
"generated_at": datetime.now().isoformat(),
|
| 1039 |
+
"user_stories": stories,
|
| 1040 |
"features": features,
|
| 1041 |
"tasks": tasks
|
| 1042 |
}
|
| 1043 |
+
|
| 1044 |
filename = f"project_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 1045 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 1046 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 1047 |
+
return gr.File(value=filename, visible=True)
|
| 1048 |
|
| 1049 |
# Build the interface
|
| 1050 |
with gr.Blocks(css=CUSTOM_CSS, title="AI Project Planner", theme=gr.themes.Base()) as app:
|
|
|
|
| 1145 |
export_md_btn = gr.Button("π Download Markdown", variant="secondary")
|
| 1146 |
export_json_btn = gr.Button("π Download JSON", variant="secondary")
|
| 1147 |
|
| 1148 |
+
export_file = gr.File(label="Download", visible=True, interactive=False)
|
| 1149 |
|
| 1150 |
# Footer
|
| 1151 |
gr.HTML("""
|
|
|
|
| 1157 |
</div>
|
| 1158 |
""")
|
| 1159 |
|
| 1160 |
+
# Event handlers
|
| 1161 |
# Event handlers
|
| 1162 |
generate_btn.click(
|
| 1163 |
fn=run_planner,
|
|
|
|
| 1167 |
user_stories_output,
|
| 1168 |
features_output,
|
| 1169 |
tasks_output,
|
| 1170 |
+
full_plan_output,
|
| 1171 |
+
stored_stories,
|
| 1172 |
+
stored_features,
|
| 1173 |
+
stored_tasks
|
| 1174 |
],
|
| 1175 |
show_progress=True
|
| 1176 |
)
|
| 1177 |
+
|
| 1178 |
export_md_btn.click(
|
| 1179 |
fn=export_markdown,
|
| 1180 |
+
inputs=[stored_stories, stored_features, stored_tasks],
|
| 1181 |
outputs=[export_file]
|
| 1182 |
)
|
| 1183 |
+
|
| 1184 |
export_json_btn.click(
|
| 1185 |
fn=export_json,
|
| 1186 |
+
inputs=[stored_stories, stored_features, stored_tasks],
|
| 1187 |
outputs=[export_file]
|
| 1188 |
)
|
| 1189 |
|