Spaces:
Paused
Paused
Update app.py
Browse files205 unmodified lines2062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812821 unmodified line284285286287288289290291292205 unmodified linesdef send_to_model(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,groq_model_choice, groq_api_key, openai_api_key, openai_model_choice):"""Wrapper function for send_to_model_impl with proper error handling."""if not prompt:return "Error: No prompt provided", Nonetry:with gr.Progress() as progress:progress(0, "Preparing to send to model...")# Call implementation with proper error handlingsummary, download_file = send_to_model_impl(prompt=prompt,model_selection=model_selection,hf_model_choice=hf_model_choice,hf_custom_model=hf_custom_model,hf_api_key=hf_api_key,groq_model_choice=groq_model_choice,groq_api_key=groq_api_key,openai_api_key=openai_api_key,openai_model_choice=openai_model_choice)progress(1, "Complete!")return summary, download_fileexcept Exception as e:error_msg = f"Error processing request: {str(e)}"logging.error(error_msg)return error_msg, 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, openai_model_choice):"""Implementation of model sending with improved error handling."""try:if model_selection == "Clipboard only":return "Text copied to clipboard. Use paste for processing.", Noneelif model_selection == "HuggingFace Inference":if not hf_api_key:return "Error: HuggingFace API key required", Noneif not hf_model_choice:return "Error: Please select a HuggingFace model", Nonemodel_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)if summary.startswith("Error"):return summary, Noneelif model_selection == "Groq API":if not groq_api_key:return "Error: Groq API key required", Noneif not groq_model_choice:return "Error: Please select a Groq model", Nonesummary = send_to_groq(prompt, groq_model_choice, groq_api_key)if summary.startswith("Error"):return summary, Noneelif model_selection == "OpenAI ChatGPT":if not openai_api_key:return "Error: OpenAI API key required", Noneif not openai_model_choice:return "Error: Please select an OpenAI model", Nonesummary = send_to_openai(prompt, openai_api_key, model=openai_model_choice)if summary.startswith("Error"):return summary, Noneelse:return "Error: Invalid model selection", None# If we get here, we have a valid summary. Create download file.if summary and not summary.startswith("Error"):with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(summary)return summary, f.name1 unmodified linereturn summary, Noneexcept Exception as e:error_msg = f"Error in model processing: {str(e)}"logging.error(error_msg)return error_msg, Nonedef send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:"""Send prompt to HuggingFace Inference API with better error handling."""205 unmodified lines2062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902911 unmodified line293294295296297298299300301302303205 unmodified linesdef send_to_model(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,groq_model_choice, groq_api_key, openai_api_key, openai_model_choice):"""Wrapper function for send_to_model_impl with proper error handling."""if not prompt or not prompt.strip():return "Error: No prompt provided", Nonetry:with gr.Progress() as progress:progress(0, "Preparing to send to model...")# Basic input validationif model_selection not in ["Clipboard only", "HuggingFace Inference", "Groq API", "OpenAI ChatGPT"]:return "Error: Invalid model selection", None# Model-specific validationif model_selection == "HuggingFace Inference" and not hf_api_key:return "Error: HuggingFace API key required", Noneelif model_selection == "Groq API" and not groq_api_key:return "Error: Groq API key required", Noneelif model_selection == "OpenAI ChatGPT" and not openai_api_key:return "Error: OpenAI API key required", None# Call implementation with error handlingprogress(0.5, "Processing with model...")try:summary, download_file = send_to_model_impl(prompt=prompt.strip(),model_selection=model_selection,hf_model_choice=hf_model_choice,hf_custom_model=hf_custom_model,hf_api_key=hf_api_key,groq_model_choice=groq_model_choice,groq_api_key=groq_api_key,openai_api_key=openai_api_key,openai_model_choice=openai_model_choice)if summary is None or not isinstance(summary, str):return "Error: No response from model", Noneprogress(1, "Complete!")return summary, download_fileexcept Exception as e:error_msg = str(e)if not error_msg: # Handle empty error messageserror_msg = "Unknown error occurred"return f"Error: {error_msg}", Noneexcept Exception as e:error_msg = str(e)if not error_msg: # Handle empty error messageserror_msg = "Unknown error occurred"logging.error(f"Error in send_to_model: {error_msg}")return f"Error: {error_msg}", 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, openai_model_choice):"""Implementation of model sending with improved error handling."""if model_selection == "Clipboard only":return "Text copied to clipboard. Use paste for processing.", Nonetry:if model_selection == "HuggingFace Inference":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":summary = send_to_groq(prompt, groq_model_choice, groq_api_key)elif model_selection == "OpenAI ChatGPT":summary = send_to_openai(prompt, openai_api_key, model=openai_model_choice)else:return "Error: Invalid model selection", None# Validate responseif not summary or not isinstance(summary, str):return "Error: Invalid response from model", None# Create download file for valid responsesif not summary.startswith("Error"):with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(summary)return summary, f.name1 unmodified linereturn summary, Noneexcept Exception as e:error_msg = str(e)if not error_msg: # Handle empty error messageserror_msg = "Unknown error occurred"logging.error(f"Error in send_to_model_impl: {error_msg}")return f"Error: {error_msg}", Nonedef send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:"""Send prompt to HuggingFace Inference API with better error handling."""
app.py+62-51