Spaces:
Paused
Paused
Update app.py
Browse files200 unmodified lines201202203204205206207208209210211212204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246139 unmodified lines386387388397398389390391413 unmodified lines805806807818819820821822823824825808809810827811812813814200 unmodified lines return "\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 result except Exception as e: return f"Error: {str(e)}", Nonedef send_to_model(*args, **kwargs): # Correct the outputs here try: with gr.Progress() as progress: progress(0, "Preparing to send to model...") summary, download_file = send_to_model_impl(*args, **kwargs) # Get both outputs progress(1, "Complete!") return summary, download_file # Return both outputs except Exception as e: return f"Error: {str(e)}", None # Return error message and None for the file
def 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 download with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f: f.write(summary) return summary, download_file # Return the file for download_summary except Exception as e: error_msg = f"Error processing request: {str(e)}" logging.error(error_msg) return error_msg, [] groq_model_choice, groq_api_key, openai_api_key, openai_model_choice): # Added openai_model_choice try: if model_selection == "Clipboard only": return "Use copy/paste for processing", None
if 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", None summary = send_to_openai(prompt, openai_api_key, model=openai_model_choice) # Use openai_model_choice
else: return "Error: Invalid model selection", None
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f: f.write(summary) download_file = f.name
return summary, download_file
except Exception as e: error_msg = f"Error processing request: {str(e)}" logging.error(error_msg) return error_msg, None # Return the error message 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 variables pdf_content = gr.State("") snippets = gr.State([])413 unmodified lines send_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 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] outputs=[summary_output, download_summary] # Correct outputs )
groq_refresh_btn.click(app.py+41-57