Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -206,77 +206,86 @@ def build_prompts(snippets: List[str], prompt_instruction: str, custom_prompt: O
|
|
| 206 |
def send_to_model(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,
|
| 207 |
groq_model_choice, groq_api_key, openai_api_key, openai_model_choice):
|
| 208 |
"""Wrapper function for send_to_model_impl with proper error handling."""
|
| 209 |
-
if not prompt:
|
| 210 |
return "Error: No prompt provided", None
|
| 211 |
|
| 212 |
try:
|
| 213 |
with gr.Progress() as progress:
|
| 214 |
progress(0, "Preparing to send to model...")
|
| 215 |
|
| 216 |
-
#
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
)
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
except Exception as e:
|
| 233 |
-
error_msg =
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,
|
| 238 |
groq_model_choice, groq_api_key, openai_api_key, openai_model_choice):
|
| 239 |
"""Implementation of model sending with improved error handling."""
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
return "Error: HuggingFace API key required", None
|
| 247 |
-
if not hf_model_choice:
|
| 248 |
-
return "Error: Please select a HuggingFace model", None
|
| 249 |
-
|
| 250 |
model_id = hf_custom_model if hf_model_choice == "Custom Model" else model_registry.hf_models[hf_model_choice]
|
| 251 |
summary = send_to_hf_inference(prompt, model_id, hf_api_key)
|
| 252 |
-
|
| 253 |
-
return summary, None
|
| 254 |
-
|
| 255 |
elif model_selection == "Groq API":
|
| 256 |
-
if not groq_api_key:
|
| 257 |
-
return "Error: Groq API key required", None
|
| 258 |
-
if not groq_model_choice:
|
| 259 |
-
return "Error: Please select a Groq model", None
|
| 260 |
-
|
| 261 |
summary = send_to_groq(prompt, groq_model_choice, groq_api_key)
|
| 262 |
-
|
| 263 |
-
return summary, None
|
| 264 |
-
|
| 265 |
elif model_selection == "OpenAI ChatGPT":
|
| 266 |
-
if not openai_api_key:
|
| 267 |
-
return "Error: OpenAI API key required", None
|
| 268 |
-
if not openai_model_choice:
|
| 269 |
-
return "Error: Please select an OpenAI model", None
|
| 270 |
-
|
| 271 |
summary = send_to_openai(prompt, openai_api_key, model=openai_model_choice)
|
| 272 |
-
|
| 273 |
-
return summary, None
|
| 274 |
-
|
| 275 |
else:
|
| 276 |
return "Error: Invalid model selection", None
|
| 277 |
|
| 278 |
-
#
|
| 279 |
-
if summary
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
|
| 281 |
f.write(summary)
|
| 282 |
return summary, f.name
|
|
@@ -284,9 +293,11 @@ def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model
|
|
| 284 |
return summary, None
|
| 285 |
|
| 286 |
except Exception as e:
|
| 287 |
-
error_msg =
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
| 290 |
|
| 291 |
def send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:
|
| 292 |
"""Send prompt to HuggingFace Inference API with better error handling."""
|
|
|
|
| 206 |
def send_to_model(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,
|
| 207 |
groq_model_choice, groq_api_key, openai_api_key, openai_model_choice):
|
| 208 |
"""Wrapper function for send_to_model_impl with proper error handling."""
|
| 209 |
+
if not prompt or not prompt.strip():
|
| 210 |
return "Error: No prompt provided", None
|
| 211 |
|
| 212 |
try:
|
| 213 |
with gr.Progress() as progress:
|
| 214 |
progress(0, "Preparing to send to model...")
|
| 215 |
|
| 216 |
+
# Basic input validation
|
| 217 |
+
if model_selection not in ["Clipboard only", "HuggingFace Inference", "Groq API", "OpenAI ChatGPT"]:
|
| 218 |
+
return "Error: Invalid model selection", None
|
| 219 |
+
|
| 220 |
+
# Model-specific validation
|
| 221 |
+
if model_selection == "HuggingFace Inference" and not hf_api_key:
|
| 222 |
+
return "Error: HuggingFace API key required", None
|
| 223 |
+
elif model_selection == "Groq API" and not groq_api_key:
|
| 224 |
+
return "Error: Groq API key required", None
|
| 225 |
+
elif model_selection == "OpenAI ChatGPT" and not openai_api_key:
|
| 226 |
+
return "Error: OpenAI API key required", None
|
|
|
|
| 227 |
|
| 228 |
+
# Call implementation with error handling
|
| 229 |
+
progress(0.5, "Processing with model...")
|
| 230 |
+
try:
|
| 231 |
+
summary, download_file = send_to_model_impl(
|
| 232 |
+
prompt=prompt.strip(),
|
| 233 |
+
model_selection=model_selection,
|
| 234 |
+
hf_model_choice=hf_model_choice,
|
| 235 |
+
hf_custom_model=hf_custom_model,
|
| 236 |
+
hf_api_key=hf_api_key,
|
| 237 |
+
groq_model_choice=groq_model_choice,
|
| 238 |
+
groq_api_key=groq_api_key,
|
| 239 |
+
openai_api_key=openai_api_key,
|
| 240 |
+
openai_model_choice=openai_model_choice
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
if summary is None or not isinstance(summary, str):
|
| 244 |
+
return "Error: No response from model", None
|
| 245 |
+
|
| 246 |
+
progress(1, "Complete!")
|
| 247 |
+
return summary, download_file
|
| 248 |
+
|
| 249 |
+
except Exception as e:
|
| 250 |
+
error_msg = str(e)
|
| 251 |
+
if not error_msg: # Handle empty error messages
|
| 252 |
+
error_msg = "Unknown error occurred"
|
| 253 |
+
return f"Error: {error_msg}", None
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
+
error_msg = str(e)
|
| 257 |
+
if not error_msg: # Handle empty error messages
|
| 258 |
+
error_msg = "Unknown error occurred"
|
| 259 |
+
logging.error(f"Error in send_to_model: {error_msg}")
|
| 260 |
+
return f"Error: {error_msg}", None
|
| 261 |
|
| 262 |
def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,
|
| 263 |
groq_model_choice, groq_api_key, openai_api_key, openai_model_choice):
|
| 264 |
"""Implementation of model sending with improved error handling."""
|
| 265 |
+
|
| 266 |
+
if model_selection == "Clipboard only":
|
| 267 |
+
return "Text copied to clipboard. Use paste for processing.", None
|
| 268 |
|
| 269 |
+
try:
|
| 270 |
+
if model_selection == "HuggingFace Inference":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
model_id = hf_custom_model if hf_model_choice == "Custom Model" else model_registry.hf_models[hf_model_choice]
|
| 272 |
summary = send_to_hf_inference(prompt, model_id, hf_api_key)
|
| 273 |
+
|
|
|
|
|
|
|
| 274 |
elif model_selection == "Groq API":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
summary = send_to_groq(prompt, groq_model_choice, groq_api_key)
|
| 276 |
+
|
|
|
|
|
|
|
| 277 |
elif model_selection == "OpenAI ChatGPT":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
summary = send_to_openai(prompt, openai_api_key, model=openai_model_choice)
|
| 279 |
+
|
|
|
|
|
|
|
| 280 |
else:
|
| 281 |
return "Error: Invalid model selection", None
|
| 282 |
|
| 283 |
+
# Validate response
|
| 284 |
+
if not summary or not isinstance(summary, str):
|
| 285 |
+
return "Error: Invalid response from model", None
|
| 286 |
+
|
| 287 |
+
# Create download file for valid responses
|
| 288 |
+
if not summary.startswith("Error"):
|
| 289 |
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
|
| 290 |
f.write(summary)
|
| 291 |
return summary, f.name
|
|
|
|
| 293 |
return summary, None
|
| 294 |
|
| 295 |
except Exception as e:
|
| 296 |
+
error_msg = str(e)
|
| 297 |
+
if not error_msg: # Handle empty error messages
|
| 298 |
+
error_msg = "Unknown error occurred"
|
| 299 |
+
logging.error(f"Error in send_to_model_impl: {error_msg}")
|
| 300 |
+
return f"Error: {error_msg}", None
|
| 301 |
|
| 302 |
def send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:
|
| 303 |
"""Send prompt to HuggingFace Inference API with better error handling."""
|