Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,54 +8,152 @@ import os
|
|
| 8 |
|
| 9 |
# Load API Keys from Hugging Face Secrets or environment variables
|
| 10 |
openai_api_key = os.environ.get("OPENAI_API_KEY")
|
| 11 |
-
|
| 12 |
|
| 13 |
# Initialize expert roles
|
| 14 |
-
pee_expert = PromptEngineeringExpert(
|
| 15 |
-
cae_expert = CriticalAnalysisExpert()
|
| 16 |
dce_expert = DiscussionContinuityExpert()
|
| 17 |
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
business_info_df = pd.read_excel(business_info_file.name)
|
| 23 |
product_service_df = pd.read_excel(product_service_file.name)
|
| 24 |
-
|
| 25 |
-
# Convert pandas DataFrames to dictionaries/JSON
|
| 26 |
business_info_form_data = business_info_df.iloc[0].to_dict()
|
| 27 |
product_service_form_data = product_service_df.to_dict('records')
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
chatbot_prompt, knowledge_base, faq_section, prompt_critique_suggestions, knowledge_base_critique_suggestions, faq_section_critique_suggestions, summary, actions, cae_analysis_summary, dce_state, goals_next_iteration, current_work_efforts, proposed_work_efforts, end_iteration_message = dce_expert.run_iteration(
|
| 30 |
-
iteration_number=
|
| 31 |
expert_pee=pee_expert,
|
| 32 |
expert_cae=cae_expert,
|
| 33 |
business_info_form_data=business_info_form_data,
|
| 34 |
product_service_form_data=product_service_form_data
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
-
return
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
|
|
|
| 8 |
|
| 9 |
# Load API Keys from Hugging Face Secrets or environment variables
|
| 10 |
openai_api_key = os.environ.get("OPENAI_API_KEY")
|
| 11 |
+
gemini_api_key = os.environ.get("GEMINI_API_KEY")
|
| 12 |
|
| 13 |
# Initialize expert roles
|
| 14 |
+
pee_expert = PromptEngineeringExpert()
|
| 15 |
+
cae_expert = CriticalAnalysisExpert()
|
| 16 |
dce_expert = DiscussionContinuityExpert()
|
| 17 |
|
| 18 |
+
MAX_ITERATIONS = 3 # Define maximum number of iterations
|
| 19 |
|
| 20 |
+
|
| 21 |
+
def nova_workflow(business_info_file, product_service_file, history=None): # **اضافه کردن history برای مدیریت state**
|
| 22 |
+
if history is None: # **Initialization for the first iteration**
|
| 23 |
+
iteration_number = 1
|
| 24 |
+
history = [] # Initialize history list to store iteration outputs
|
| 25 |
business_info_df = pd.read_excel(business_info_file.name)
|
| 26 |
product_service_df = pd.read_excel(product_service_file.name)
|
|
|
|
|
|
|
| 27 |
business_info_form_data = business_info_df.iloc[0].to_dict()
|
| 28 |
product_service_form_data = product_service_df.to_dict('records')
|
| 29 |
+
else: # **Loading state from previous iteration**
|
| 30 |
+
iteration_number = history[-1]["iteration_number"] + 1 # Increment iteration number
|
| 31 |
|
| 32 |
+
if iteration_number > MAX_ITERATIONS: # **Check for max iterations limit**
|
| 33 |
+
return (
|
| 34 |
+
history,
|
| 35 |
+
"Maximum iterations reached. Final outputs are displayed below.",
|
| 36 |
+
gr.Button.update(visible=False), # Hide "Continue Iteration" button
|
| 37 |
+
gr.Button.update(visible=False), # Hide "Final Output" button
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# Run Nova System Workflow Iteration
|
| 43 |
chatbot_prompt, knowledge_base, faq_section, prompt_critique_suggestions, knowledge_base_critique_suggestions, faq_section_critique_suggestions, summary, actions, cae_analysis_summary, dce_state, goals_next_iteration, current_work_efforts, proposed_work_efforts, end_iteration_message = dce_expert.run_iteration(
|
| 44 |
+
iteration_number=iteration_number,
|
| 45 |
expert_pee=pee_expert,
|
| 46 |
expert_cae=cae_expert,
|
| 47 |
business_info_form_data=business_info_form_data,
|
| 48 |
product_service_form_data=product_service_form_data
|
| 49 |
)
|
| 50 |
|
| 51 |
+
iteration_output = { # Store iteration outputs in a dictionary
|
| 52 |
+
"iteration_number": iteration_number,
|
| 53 |
+
"chatbot_prompt": chatbot_prompt,
|
| 54 |
+
"knowledge_base": knowledge_base,
|
| 55 |
+
"faq_section": faq_section,
|
| 56 |
+
"prompt_critique_suggestions": prompt_critique_suggestions,
|
| 57 |
+
"knowledge_base_critique_suggestions": knowledge_base_critique_suggestions,
|
| 58 |
+
"faq_section_critique_suggestions": faq_section_critique_suggestions,
|
| 59 |
+
"summary": summary,
|
| 60 |
+
"actions": actions,
|
| 61 |
+
"cae_analysis_summary": cae_analysis_summary,
|
| 62 |
+
"dce_state": dce_state,
|
| 63 |
+
"goals_next_iteration": goals_next_iteration,
|
| 64 |
+
"current_work_efforts": current_work_efforts,
|
| 65 |
+
"proposed_work_efforts": proposed_work_efforts,
|
| 66 |
+
"end_iteration_message": end_iteration_message
|
| 67 |
+
}
|
| 68 |
|
| 69 |
+
history.append(iteration_output) # Append current iteration output to history
|
| 70 |
+
|
| 71 |
+
iteration_display = "" # Format iteration output for display
|
| 72 |
+
for iteration in history:
|
| 73 |
+
iteration_display += f"----\nIteration {iteration['iteration_number']}:\n----\n"
|
| 74 |
+
iteration_display += f"**DCE Summary:**\n{iteration['summary']}\n\n"
|
| 75 |
+
iteration_display += f"**PEE Output - Chatbot Prompt:**\n{iteration['chatbot_prompt']}\n\n"
|
| 76 |
+
iteration_display += f"**CAE Analysis - Prompt Critique:**\n{iteration['prompt_critique_suggestions']}\n\n"
|
| 77 |
+
iteration_display += f"**Knowledge Base:**\n{iteration['knowledge_base']}\n\n"
|
| 78 |
+
iteration_display += f"**FAQ Section:**\n{iteration['faq_section']}\n\n"
|
| 79 |
+
iteration_display += f"**CAE Analysis - Knowledge Base Critique:**\n{iteration['knowledge_base_critique_suggestions']}\n\n"
|
| 80 |
+
iteration_display += f"**CAE Analysis - FAQ Section Critique:**\n{iteration['faq_section_critique_suggestions']}\n\n"
|
| 81 |
+
iteration_display += f"**Actions:**\n{iteration['actions']}\n\n"
|
| 82 |
+
iteration_display += f"**DCE State:**\n{iteration['dce_state']}\n\n"
|
| 83 |
+
iteration_display += f"**Goals for next iteration:**\n{iteration['goals_next_iteration']}\n\n"
|
| 84 |
+
iteration_display += f"**Current Work Efforts:**\n{iteration['current_work_efforts']}\n\n"
|
| 85 |
+
iteration_display += f"**Proposed Work Efforts:**\n{iteration['proposed_work_efforts']}\n\n"
|
| 86 |
+
iteration_display += f"**End of Iteration:**\n{iteration['end_iteration_message']}\n\n"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
return (
|
| 90 |
+
history, # Return history to gr.State
|
| 91 |
+
iteration_display, # Return formatted iteration display
|
| 92 |
+
gr.Button.update(visible=True), # Show "Continue Iteration" button
|
| 93 |
+
gr.Button.update(visible=True), # Show "Final Output" button
|
| 94 |
+
)
|
| 95 |
|
| 96 |
except Exception as e:
|
| 97 |
+
return (
|
| 98 |
+
history, # Return history even in case of error
|
| 99 |
+
str(e), # Return error message in iteration display
|
| 100 |
+
gr.Button.update(visible=False), # Hide "Continue Iteration" button in case of error
|
| 101 |
+
gr.Button.update(visible=False), # Hide "Final Output" button in case of error
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def finalize_outputs(history): # **Function for "Final Output" button**
|
| 106 |
+
if history:
|
| 107 |
+
final_iteration = history[-1] # Get the last iteration output from history
|
| 108 |
+
return (
|
| 109 |
+
final_iteration['chatbot_prompt'],
|
| 110 |
+
final_iteration['knowledge_base'],
|
| 111 |
+
final_iteration['faq_section'],
|
| 112 |
+
gr.Button.update(visible=False), # Hide "Continue Iteration" button after finalization
|
| 113 |
+
gr.Button.update(visible=False), # Hide "Final Output" button after finalization
|
| 114 |
+
)
|
| 115 |
+
else:
|
| 116 |
+
return "No output generated yet.", "No output generated yet.", "No output generated yet.", gr.Button.update(visible=False), gr.Button.update(visible=False)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
with gr.Blocks() as iface: # Use gr.Blocks for more control over layout and state
|
| 120 |
+
history_state = gr.State([]) # gr.State to store iteration history
|
| 121 |
+
|
| 122 |
+
gr.Markdown("## Nova System - Chatbot Prompt & Knowledge Base Generator (Iterative)")
|
| 123 |
+
gr.Markdown("Upload Excel files containing business information and product/service details to generate a chatbot prompt, knowledge base, and FAQ section. **System now runs in iterative mode with user control.**")
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
business_info_file = gr.File(file_types=['.xlsx'], label="Upload Business Information Form (Excel File)")
|
| 127 |
+
product_service_file = gr.File(file_types=['.xlsx'], label="Upload Product/Service Information Form (Excel File)")
|
| 128 |
+
|
| 129 |
+
generate_button = gr.Button("Generate Prompt, Knowledge Base & Start Iterations")
|
| 130 |
+
iteration_output_display = gr.Markdown(label="Iteration Output") # Component to display iteration details
|
| 131 |
+
|
| 132 |
+
with gr.Row():
|
| 133 |
+
continue_iteration_button = gr.Button("Continue Iteration", visible=False) # "Continue Iteration" Button - Initially hidden
|
| 134 |
+
finalize_button = gr.Button("Final Output", visible=False) # "Final Output" Button - Initially hidden
|
| 135 |
+
|
| 136 |
+
chatbot_prompt_output = gr.Code(language="markdown", label="Final Chatbot Prompt (English - Markdown)")
|
| 137 |
+
knowledge_base_output = gr.Code(language="json", label="Knowledge Base (JSON-like)")
|
| 138 |
+
faq_section_output = gr.Code(language="json", label="FAQ Section (JSON-like)")
|
| 139 |
+
|
| 140 |
+
generate_button.click( # Event listener for "Generate" button
|
| 141 |
+
nova_workflow,
|
| 142 |
+
inputs=[business_info_file, product_service_file, history_state], # Pass history_state as input
|
| 143 |
+
outputs=[history_state, iteration_output_display, continue_iteration_button, finalize_button, chatbot_prompt_output, knowledge_base_output, faq_section_output] # Include history_state in outputs
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
continue_iteration_button.click( # Event listener for "Continue Iteration" button
|
| 147 |
+
nova_workflow,
|
| 148 |
+
inputs=[business_info_file, product_service_file, history_state], # Pass history_state as input
|
| 149 |
+
outputs=[history_state, iteration_output_display, continue_iteration_button, finalize_button, chatbot_prompt_output, knowledge_base_output, faq_section_output] # Include history_state in outputs
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
finalize_button.click( # Event listener for "Final Output" button
|
| 153 |
+
finalize_outputs,
|
| 154 |
+
inputs=[history_state], # Pass history_state as input
|
| 155 |
+
outputs=[chatbot_prompt_output, knowledge_base_output, faq_section_output, continue_iteration_button, finalize_button] # Buttons visibility in outputs
|
| 156 |
+
)
|
| 157 |
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|