cstr commited on
Commit
5246944
·
verified ·
1 Parent(s): f97296a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -51
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
- # Call implementation with proper error handling
217
- summary, download_file = send_to_model_impl(
218
- prompt=prompt,
219
- model_selection=model_selection,
220
- hf_model_choice=hf_model_choice,
221
- hf_custom_model=hf_custom_model,
222
- hf_api_key=hf_api_key,
223
- groq_model_choice=groq_model_choice,
224
- groq_api_key=groq_api_key,
225
- openai_api_key=openai_api_key,
226
- openai_model_choice=openai_model_choice
227
- )
228
 
229
- progress(1, "Complete!")
230
- return summary, download_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  except Exception as e:
233
- error_msg = f"Error processing request: {str(e)}"
234
- logging.error(error_msg)
235
- return error_msg, None
 
 
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
- try:
241
- if model_selection == "Clipboard only":
242
- return "Text copied to clipboard. Use paste for processing.", None
243
 
244
- elif model_selection == "HuggingFace Inference":
245
- if not hf_api_key:
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
- if summary.startswith("Error"):
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
- if summary.startswith("Error"):
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
- if summary.startswith("Error"):
273
- return summary, None
274
-
275
  else:
276
  return "Error: Invalid model selection", None
277
 
278
- # If we get here, we have a valid summary. Create download file.
279
- if summary and not summary.startswith("Error"):
 
 
 
 
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 = f"Error in model processing: {str(e)}"
288
- logging.error(error_msg)
289
- return error_msg, None
 
 
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."""