Spaces:
Paused
Paused
Update app.py
Browse files200 unmodified lines201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254139 unmodified lines394395396397398399400401413 unmodified lines815816817818819820821822823824825826827828829830200 unmodified linesreturn "\n\n".join(prompts)def send_to_model(*args, **kwargs):try:with gr.Progress() as progress:progress(0, "Preparing to send to model...")result = send_to_model_impl(*args, **kwargs)progress(1, "Complete!")return resultexcept Exception as e:return f"Error: {str(e)}", Nonedef send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,groq_model_choice, groq_api_key, openai_api_key):try:if model_selection == "Clipboard only":return "Use copy/paste for processing", []if model_selection == "HuggingFace Inference":if not hf_api_key:return "Error: HuggingFace API key required", []if not hf_model_choice:return "Error: Select a HuggingFace model", []model_id = hf_custom_model if hf_model_choice == "Custom Model" else model_registry.hf_models[hf_model_choice]summary = send_to_hf_inference(prompt, model_id, hf_api_key)elif model_selection == "Groq API":if not groq_api_key:return "Error: Groq API key required", []if not groq_model_choice:return "Error: Select a Groq model", []summary = send_to_groq(prompt, groq_model_choice, groq_api_key)elif model_selection == "OpenAI ChatGPT":if not openai_api_key:return "Error: OpenAI API key required", []summary = send_to_openai(prompt, openai_api_key)else:return "Error: Invalid model selection", []# Save summary for downloadwith 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)return error_msg, []def send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:try:139 unmodified lines.gradio-container {max-width: 90%; margin: 0 auto;}@media (max-width: 768px) {.gradio-container {max-width: 98%; padding: 10px;} .gr-row {flex-direction: column;} .gr-col {width: 100%; margin-bottom: 10px;}}""") as demo:gr.Markdown("# 📄 Smart PDF Summarizer")# State variablespdf_content = gr.State("")snippets = gr.State([])413 unmodified linessend_to_model_btn.click(send_to_model,inputs=[generated_prompt,model_choice,hf_model,hf_custom_model,hf_api_key,groq_model,groq_api_key,openai_api_key],outputs=[summary_output, download_summary])groq_refresh_btn.click(200 unmodified lines201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246139 unmodified lines386387388389390391413 unmodified lines805806807808809810811812813814200 unmodified linesreturn "\n\n".join(prompts)def send_to_model(*args, **kwargs): # Correct the outputs heretry:with gr.Progress() as progress:progress(0, "Preparing to send to model...")summary, download_file = send_to_model_impl(*args, **kwargs) # Get both outputsprogress(1, "Complete!")return summary, download_file # Return both outputsexcept Exception as e:return f"Error: {str(e)}", None # Return error message and None for the filedef send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,groq_model_choice, groq_api_key, openai_api_key, openai_model_choice): # Added openai_model_choicetry:if model_selection == "Clipboard only":return "Use copy/paste for processing", Noneif model_selection == "HuggingFace Inference":# ... (Existing logic)elif model_selection == "Groq API":# ... (Existing logic)elif model_selection == "OpenAI ChatGPT":if not openai_api_key:return "Error: OpenAI API key required", Nonesummary = send_to_openai(prompt, openai_api_key, model=openai_model_choice) # Use openai_model_choiceelse:return "Error: Invalid model selection", Nonewith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(summary)download_file = f.namereturn summary, download_fileexcept Exception as e:error_msg = f"Error processing request: {str(e)}"logging.error(error_msg)return error_msg, None # Return the error messagedef send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:try:139 unmodified lines.gradio-container {max-width: 90%; margin: 0 auto;}@media (max-width: 768px) {.gradio-container {max-width: 98%; padding: 10px;} .gr-row {flex-direction: column;} .gr-col {width: 100%; margin-bottom: 10px;}}""") as demo:# State variablespdf_content = gr.State("")snippets = gr.State([])413 unmodified linessend_to_model_btn.click(send_to_model,inputs=[generated_prompt, model_choice, hf_model, hf_custom_model, hf_api_key,groq_model, groq_api_key, openai_api_key, openai_model # Add openai_model as input],outputs=[summary_output, download_summary] # Correct outputs)groq_refresh_btn.click(
app.py+41-57