Spaces:
Paused
Paused
Update app.py
Browse files243 unmodified lines244245246247248249250205 unmodified lines45645745845946046146223 unmodified lines48648748848949049149249349457 unmodified lines552553554555556557558559115 unmodified lines6756766776786796806818 unmodified lines69069169269369469569669769869970070170270370470570670770870971071171271371471571671771818 unmodified lines7377387397407417427437449 unmodified lines75475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778820 unmodified lines80981081181281381481581681781881982082182282382482582627 unmodified lines854855856857858859860861862863243 unmodified lineswith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(summary)return summary, [f.name]except Exception as e:error_msg = f"Error processing request: {str(e)}"logging.error(error_msg)205 unmodified linesmax_lines=50,show_copy_button=True)download_full_text = gr.Button("📥 Download Full Text")# Tab 2: Snippet Selectionwith gr.Tab("2️⃣ Snippet Selection"):23 unmodified lines)with gr.Row():copy_prompt_button = gr.Button("📋 Copy Prompt")download_prompt = gr.Button("📥 Download Prompt")download_snippet = gr.Button("📥 Download Selected Snippet")# Tab 3: Model Processingwith gr.Tab("3️⃣ Model Processing"):57 unmodified lines)with gr.Row():copy_summary_button = gr.Button("📋 Copy Summary")download_summary = gr.Button("📥 Download Summary")# Hidden components for file handlingdownload_files = gr.Files(label="📥 Downloads", visible=False)115 unmodified linesformatted_text,snippets_list,gr.update(choices=update_snippet_choices(snippets_list), value="Snippet 1 of " + str(len(snippets_list))),[download_file])except Exception as e:8 unmodified linesNone)def handle_snippet_selection(choice, snippets_list):"""Handle snippet selection and update prompt"""if not snippets_list:return ("No snippets available.", # progress_status"", # generated_promptNone # download_files)try:idx = get_snippet_index(choice)selected_snippet = snippets_list[idx]# Create downloadable snippetwith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(selected_snippet)return (f"Selected snippet {idx + 1}",selected_snippet,[f.name])except Exception as e:error_msg = f"Error selecting snippet: {str(e)}"logging.error(error_msg)18 unmodified lines# Save prompt for downloadwith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(prompt)return "Prompt generated!", prompt, [f.name]except Exception as e:logging.error(f"Error generating prompt: {e}")return f"Error: {str(e)}", "", None9 unmodified linesprocess_button.click(handle_pdf_process,inputs=[pdf_input, format_type, context_size],outputs=[ # List of outputs, not dictprogress_status,processed_text,pdf_content,snippets,snippet_selector,download_files])generate_prompt_btn.click(handle_prompt_generation,inputs=[generated_prompt, custom_prompt, snippet_selector, snippets],outputs=[progress_status,generated_prompt,download_files])# Snippet handlingsnippet_selector.change(handle_snippet_selection,inputs=[snippet_selector, snippets],outputs=[progress_status,generated_prompt,download_files])# Model selection20 unmodified linesoutputs=[context_size])# Download handlerscopy_prompt_button.click(fn=None,_js=copy_text_js("generated_prompt"), # Use the helper functionoutputs=progress_status)copy_summary_button.click(fn=None,_js=copy_text_js("summary_output"), # Use the helper functionoutputs=progress_status)def download_file(content: str, prefix: str) -> List[str]:if not content:return []27 unmodified linesgroq_api_key,openai_api_key],outputs=[summary_output,download_files])groq_refresh_btn.click(243 unmodified lines244245246247248249250205 unmodified lines45645745845946046146223 unmodified lines48648748848949049149249357 unmodified lines551552553554555556557115 unmodified lines6736746756766776786796808 unmodified lines68969069169269369469569669769869970070170270370470570670770870971071171271318 unmodified lines7327337347357367377387397407419 unmodified lines75175275375475575675775875976076176276376476576676776876977020 unmodified lines79179279379479579627 unmodified lines824825826827828829830243 unmodified lineswith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(summary)return summary, download_file # Return the file for download_summaryexcept Exception as e:error_msg = f"Error processing request: {str(e)}"logging.error(error_msg)205 unmodified linesmax_lines=50,show_copy_button=True)download_full_text = gr.File(label="📥 Download Full Text")# Tab 2: Snippet Selectionwith gr.Tab("2️⃣ Snippet Selection"):23 unmodified lines)with gr.Row():download_prompt = gr.File(label="📥 Download Prompt")download_snippet = gr.File(label="📥 Download Selected Snippet")# Tab 3: Model Processingwith gr.Tab("3️⃣ Model Processing"):57 unmodified lines)with gr.Row():download_summary = gr.File(label="📥 Download Summary")# Hidden components for file handlingdownload_files = gr.Files(label="📥 Downloads", visible=False)115 unmodified linesformatted_text,snippets_list,gr.update(choices=update_snippet_choices(snippets_list), value="Snippet 1 of " + str(len(snippets_list))),download_file # Return the file for download_full_text#[download_file])except Exception as e:8 unmodified linesNone)def handle_snippet_selection(choice, snippets_list): # Add download_snippet output"""Handle snippet selection, update prompt, and provide snippet download."""if not snippets_list:return "No snippets available.", "", None # Return None for downloadtry:idx = get_snippet_index(choice)selected_snippet = snippets_list[idx]with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(selected_snippet)snippet_download_file = f.name # Store the file pathreturn (f"Selected snippet {idx + 1}",selected_snippet,snippet_download_file # Return file for download)except Exception as e:error_msg = f"Error selecting snippet: {str(e)}"logging.error(error_msg)18 unmodified lines# Save prompt for downloadwith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(prompt)download_file = f.namereturn "Prompt generated!", prompt, download_file # Return the file for download_promptexcept Exception as e:logging.error(f"Error generating prompt: {e}")return f"Error: {str(e)}", "", None9 unmodified linesprocess_button.click(handle_pdf_process,inputs=[pdf_input, format_type, context_size],outputs=[progress_status, processed_text, pdf_content, snippets, snippet_selector, download_full_text])generate_prompt_btn.click(handle_prompt_generation,inputs=[generated_prompt, custom_prompt, snippet_selector, snippets],outputs=[progress_status, generated_prompt, download_prompt])# Snippet handlingsnippet_selector.change(handle_snippet_selection,inputs=[snippet_selector, snippets],outputs=[progress_status, generated_prompt, download_snippet] # Connect download_snippet)# Model selection20 unmodified linesoutputs=[context_size])def download_file(content: str, prefix: str) -> List[str]:if not content:return []27 unmodified linesgroq_api_key,openai_api_key],outputs=[summary_output, download_summary])groq_refresh_btn.click(
app.py+23-56