Spaces:
Sleeping
Sleeping
Update app.py
Browse files471 unmodified lines47247347447547647747847948048148248348448548686 unmodified lines57357457557657757845 unmodified lines62462562662762862963063163263352 unmodified lines6866876886896906916926 unmodified lines69970070170270370470527 unmodified lines73373473573673773873974074174274374474550 unmodified lines7967977987998008018028038043 unmodified lines80880981081181281381481581681781881982082171 unmodified lines8938948958968978988999004 unmodified lines90590690790890991013 unmodified lines924925926927928929930931932933471 unmodified lineswith gr.Column(scale=1):gr.Markdown("### π§ Cognitive Instructions")cognitive_instructions = []for category, instructions in ADVANCED_CATEGORIES.items():with gr.Accordion(f"π {category}", open=False):checkbox_group = gr.CheckboxGroup(choices=instructions,label=category,value=[])cognitive_instructions.append(checkbox_group)# Style & Filtering Tabwith gr.TabItem("π¨ Style & Filtering"):86 unmodified linesclear_chat_btn = gr.Button("ποΈ Clear", variant="secondary")save_prompt_btn = gr.Button("πΎ Save Prompt", variant="secondary")chatbot = gr.Chatbot(label="π€ AI Assistant",type="messages",45 unmodified lineslines=8,interactive=False)# State managementchat_history = gr.State([])current_template_vars = gr.State({})# Helper functionsdef update_template_info(category, template_name):52 unmodified linesreturn "", "", {}def create_final_prompt(system, main_prompt, cognitive_selected, examples, tone, length, format_pref, role_persona, custom_system):components = []# System prompt6 unmodified lines# Add cognitive instructionsall_selected = []for selected_list in cognitive_selected:if selected_list:all_selected.extend(selected_list)27 unmodified linesreturn "\n\n".join(components)def process_chat_message(message, history, model, temp, system, cognitive_selected, tone, length, format_pref, role, custom_sys, banned_w, banned_p, *examples):if not message.strip():return history, history, "", {}, ""# Create final promptfinal_prompt = create_final_prompt(system, message, cognitive_selected, examples,tone, length, format_pref, role, custom_sys)50 unmodified linesreturn response_a, response_bdef save_current_prompt(prompt_name, final_prompt, model, stats):if not prompt_name or not final_prompt:return "β Please provide a name and ensure there's a prompt to save"prompt_data = {"prompt": final_prompt,3 unmodified linesstate.save_prompt(prompt_name, prompt_data)# Update dropdown choiceschoices = list(state.saved_prompts.keys())return f"β Saved prompt '{prompt_name}'", gr.update(choices=choices)def load_saved_prompt(prompt_name):if prompt_name in state.saved_prompts:return state.saved_prompts[prompt_name]return {}def refresh_analytics():analytics = calculate_analytics()71 unmodified linesprocess_chat_message,inputs=[chat_input, chat_history, selected_model, temperature,custom_system, cognitive_instructions, tone_style, length_preference,format_preference, role_persona, custom_system, banned_words, banned_patterns,example_1_input, example_1_output, example_2_input, example_2_output],outputs=[chatbot, chat_history, final_prompt_preview, prompt_stats, optimization_display]4 unmodified linesoutputs=[chatbot, chat_history])# A/B Testingtest_btn.click(run_ab_test,13 unmodified linesoutputs=[export_data])# Auto-refresh analytics on loaddemo.load(refresh_analytics,outputs=[analytics_display, analytics_json])if __name__ == "__main__":471 unmodified lines47247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150286 unmodified lines58959059159259359459559645 unmodified lines64264364464564664764864965065165265365465552 unmodified lines7087097107117127137146 unmodified lines72172272372472572672727 unmodified lines75575675775875976076176276376476576676750 unmodified lines8188198208218228238248258268278288293 unmodified lines83383483583683783883984084184284384484584684784884985085185285385485571 unmodified lines9279289299309319329339344 unmodified lines93994094194294394494594694794894995013 unmodified lines964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993471 unmodified lineswith gr.Column(scale=1):gr.Markdown("### π§ Cognitive Instructions")# Create individual components instead of a listwith gr.Accordion("π Cognitive Techniques", open=False):cognitive_techniques = gr.CheckboxGroup(choices=ADVANCED_CATEGORIES["Cognitive Techniques"],label="Cognitive Techniques",value=[])with gr.Accordion("π Output Structure", open=False):output_structure = gr.CheckboxGroup(choices=ADVANCED_CATEGORIES["Output Structure"],label="Output Structure",value=[])with gr.Accordion("π Quality Assurance", open=False):quality_assurance = gr.CheckboxGroup(choices=ADVANCED_CATEGORIES["Quality Assurance"],label="Quality Assurance",value=[])with gr.Accordion("π Audience Adaptation", open=False):audience_adaptation = gr.CheckboxGroup(choices=ADVANCED_CATEGORIES["Audience Adaptation"],label="Audience Adaptation",value=[])# Style & Filtering Tabwith gr.TabItem("π¨ Style & Filtering"):86 unmodified linesclear_chat_btn = gr.Button("ποΈ Clear", variant="secondary")save_prompt_btn = gr.Button("πΎ Save Prompt", variant="secondary")save_status = gr.Textbox(label="πΎ Save Status", visible=False, interactive=False)chatbot = gr.Chatbot(label="π€ AI Assistant",type="messages",45 unmodified lineslines=8,interactive=False)import_status = gr.Textbox(label="π₯ Import Status",interactive=False)# State managementchat_history = gr.State([])# Helper functionsdef update_template_info(category, template_name):52 unmodified linesreturn "", "", {}def create_final_prompt(system, main_prompt, cognitive_tech, output_struct, quality_assur, audience_adapt, examples, tone, length, format_pref, role_persona, custom_system):components = []# System prompt6 unmodified lines# Add cognitive instructionsall_selected = []for selected_list in [cognitive_tech, output_struct, quality_assur, audience_adapt]:if selected_list:all_selected.extend(selected_list)27 unmodified linesreturn "\n\n".join(components)def process_chat_message(message, history, model, temp, system, cognitive_tech, output_struct, quality_assur, audience_adapt, tone, length, format_pref, role, custom_sys, banned_w, banned_p, *examples):if not message.strip():return history, history, "", {}, ""# Create final promptfinal_prompt = create_final_prompt(system, message, cognitive_tech, output_struct, quality_assur, audience_adapt, examples,tone, length, format_pref, role, custom_sys)50 unmodified linesreturn response_a, response_bdef save_current_prompt(final_prompt, model, stats):# Simple prompt saving - in a real app you'd want a name inputprompt_name = f"Prompt_{len(state.saved_prompts)+1}_{datetime.now().strftime('%H%M')}"if not final_prompt:return gr.update(value="β No prompt to save", visible=True)prompt_data = {"prompt": final_prompt,3 unmodified linesstate.save_prompt(prompt_name, prompt_data)return gr.update(value=f"β Saved as '{prompt_name}'", visible=True)def load_saved_prompt(prompt_name):if prompt_name in state.saved_prompts:return state.saved_prompts[prompt_name]return {}def delete_saved_prompt(prompt_name):if prompt_name in state.saved_prompts:del state.saved_prompts[prompt_name]choices = list(state.saved_prompts.keys())return gr.update(choices=choices, value=None), {}return gr.update(), {}def update_saved_prompts_dropdown():choices = list(state.saved_prompts.keys())return gr.update(choices=choices)def refresh_analytics():analytics = calculate_analytics()71 unmodified linesprocess_chat_message,inputs=[chat_input, chat_history, selected_model, temperature,custom_system, cognitive_techniques, output_structure, quality_assurance, audience_adaptation,tone_style, length_preference, format_preference, role_persona, custom_system, banned_words, banned_patterns,example_1_input, example_1_output, example_2_input, example_2_output],outputs=[chatbot, chat_history, final_prompt_preview, prompt_stats, optimization_display]4 unmodified linesoutputs=[chatbot, chat_history])save_prompt_btn.click(save_current_prompt,inputs=[final_prompt_preview, selected_model, prompt_stats],outputs=[save_status])# A/B Testingtest_btn.click(run_ab_test,13 unmodified linesoutputs=[export_data])# Load and delete saved promptsload_prompt_btn.click(load_saved_prompt,inputs=[saved_prompts_list],outputs=[saved_prompt_display])delete_prompt_btn.click(delete_saved_prompt,inputs=[saved_prompts_list],outputs=[saved_prompts_list, saved_prompt_display])# Import functionalityimport_file.upload(import_data,inputs=[import_file],outputs=[import_status])# Auto-refresh analytics on load and update saved promptsdemo.load(lambda: (refresh_analytics()[0], refresh_analytics()[1], gr.update(choices=list(state.saved_prompts.keys()))),outputs=[analytics_display, analytics_json, saved_prompts_list])if __name__ == "__main__":
app.py+85-25