wakeupmh commited on
Commit
2dd9d2c
·
1 Parent(s): 11fa286
Files changed (1) hide show
  1. services/model_handler.py +74 -8
services/model_handler.py CHANGED
@@ -77,11 +77,16 @@ class LocalHuggingFaceModel(Model):
77
  def invoke(self, prompt: str, **kwargs) -> str:
78
  """Synchronous invoke method"""
79
  try:
80
- logging.info(f"Invoking model with prompt: {prompt[:100]}...")
81
 
82
- if not prompt or not prompt.strip():
 
 
 
 
 
83
  logging.warning("Empty prompt provided to invoke method")
84
- return "No input provided."
85
 
86
  inputs = self.tokenizer(prompt, return_tensors="pt", padding=True)
87
 
@@ -101,17 +106,17 @@ class LocalHuggingFaceModel(Model):
101
  # Check if output is empty
102
  if not decoded_output or not decoded_output.strip():
103
  logging.warning("Model generated empty output")
104
- return "The model did not generate any output. Please try with a different prompt."
105
 
106
  logging.info(f"Model generated output: {decoded_output[:100]}...")
107
- return decoded_output
108
  except Exception as e:
109
  logging.error(f"Error in local model generation: {str(e)}")
110
  if hasattr(e, 'args') and len(e.args) > 0:
111
  error_message = e.args[0]
112
  else:
113
  error_message = str(e)
114
- return f"Error during generation: {error_message}"
115
 
116
  def invoke_stream(self, prompt: str, **kwargs):
117
  """Synchronous streaming invoke method"""
@@ -268,7 +273,21 @@ class ModelHandler:
268
 
269
  def _format_prompt(self, role, instructions, query):
270
  """Format the prompt for the model"""
271
- return f"""Task: {role}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
  Instructions:
274
  {instructions}
@@ -276,7 +295,14 @@ Instructions:
276
  Input: {query}
277
 
278
  Output:"""
279
-
 
 
 
 
 
 
 
280
  @staticmethod
281
  @st.cache_resource
282
  def _load_model():
@@ -303,6 +329,12 @@ Output:"""
303
  def generate_answer(self, query: str) -> str:
304
  try:
305
  logging.info(f"Generating answer for query: {query}")
 
 
 
 
 
 
306
  # Format translation prompt
307
  translation_prompt = self._format_prompt(
308
  role="Translate the following text to English",
@@ -311,6 +343,11 @@ Output:"""
311
  )
312
  logging.info(f"Translation prompt: {translation_prompt}")
313
 
 
 
 
 
 
314
  # Get English translation
315
  translation = self.translator.run(prompt=translation_prompt, stream=False)
316
  logging.info(f"Translation result type: {type(translation)}")
@@ -327,6 +364,10 @@ Output:"""
327
  translation_content = str(translation)
328
  logging.info(f"Translation as string: {translation_content}")
329
 
 
 
 
 
330
 
331
  # Format research prompt
332
  research_prompt = self._format_prompt(
@@ -336,6 +377,11 @@ Output:"""
336
  )
337
  logging.info(f"Research prompt: {research_prompt}")
338
 
 
 
 
 
 
339
  # Get research results
340
  research_results = self.researcher.run(prompt=research_prompt, stream=False)
341
  logging.info(f"Research results type: {type(research_results)}")
@@ -352,6 +398,11 @@ Output:"""
352
  research_content = str(research_results)
353
  logging.info(f"Research as string: {research_content}")
354
 
 
 
 
 
 
355
  logging.info(f"Research results: {research_results}")
356
 
357
  # Format summary prompt
@@ -362,6 +413,11 @@ Output:"""
362
  )
363
  logging.info(f"Summary prompt: {summary_prompt}")
364
 
 
 
 
 
 
365
  # Get summary
366
  summary = self.summarizer.run(prompt=summary_prompt, stream=False)
367
  logging.info(f"Summary type: {type(summary)}")
@@ -378,6 +434,11 @@ Output:"""
378
  summary_content = str(summary)
379
  logging.info(f"Summary as string: {summary_content}")
380
 
 
 
 
 
 
381
  logging.info(f"Summary: {summary}")
382
 
383
  # Format presentation prompt
@@ -388,6 +449,11 @@ Output:"""
388
  )
389
  logging.info(f"Presentation prompt: {presentation_prompt}")
390
 
 
 
 
 
 
391
  # Get presentation
392
  presentation = self.presenter.run(prompt=presentation_prompt, stream=False)
393
  logging.info(f"Presentation type: {type(presentation)}")
 
77
  def invoke(self, prompt: str, **kwargs) -> str:
78
  """Synchronous invoke method"""
79
  try:
80
+ logging.info(f"Invoking model with prompt: {prompt[:100] if prompt else 'None'}...")
81
 
82
+ # Check if prompt is None or empty
83
+ if prompt is None:
84
+ logging.warning("None prompt provided to invoke method")
85
+ return Response("No input provided. Please provide a valid prompt.")
86
+
87
+ if not prompt.strip():
88
  logging.warning("Empty prompt provided to invoke method")
89
+ return Response("No input provided. Please provide a non-empty prompt.")
90
 
91
  inputs = self.tokenizer(prompt, return_tensors="pt", padding=True)
92
 
 
106
  # Check if output is empty
107
  if not decoded_output or not decoded_output.strip():
108
  logging.warning("Model generated empty output")
109
+ return Response("The model did not generate any output. Please try with a different prompt.")
110
 
111
  logging.info(f"Model generated output: {decoded_output[:100]}...")
112
+ return Response(decoded_output)
113
  except Exception as e:
114
  logging.error(f"Error in local model generation: {str(e)}")
115
  if hasattr(e, 'args') and len(e.args) > 0:
116
  error_message = e.args[0]
117
  else:
118
  error_message = str(e)
119
+ return Response(f"Error during generation: {error_message}")
120
 
121
  def invoke_stream(self, prompt: str, **kwargs):
122
  """Synchronous streaming invoke method"""
 
273
 
274
  def _format_prompt(self, role, instructions, query):
275
  """Format the prompt for the model"""
276
+ # Validate inputs
277
+ if not role or not role.strip():
278
+ role = "Assistant"
279
+ logging.warning("Empty role provided to _format_prompt, using default: 'Assistant'")
280
+
281
+ if not instructions or not instructions.strip():
282
+ instructions = "Please process the following input."
283
+ logging.warning("Empty instructions provided to _format_prompt, using default instructions")
284
+
285
+ if not query or not query.strip():
286
+ query = "No input provided."
287
+ logging.warning("Empty query provided to _format_prompt, using placeholder text")
288
+
289
+ # Format the prompt
290
+ formatted_prompt = f"""Task: {role}
291
 
292
  Instructions:
293
  {instructions}
 
295
  Input: {query}
296
 
297
  Output:"""
298
+
299
+ # Ensure the prompt is not empty
300
+ if not formatted_prompt or not formatted_prompt.strip():
301
+ logging.error("Generated an empty prompt despite validation")
302
+ formatted_prompt = "Please provide a response."
303
+
304
+ return formatted_prompt
305
+
306
  @staticmethod
307
  @st.cache_resource
308
  def _load_model():
 
329
  def generate_answer(self, query: str) -> str:
330
  try:
331
  logging.info(f"Generating answer for query: {query}")
332
+
333
+ # Validate input query
334
+ if not query or not query.strip():
335
+ logging.error("Empty query provided")
336
+ return "Error: Please provide a non-empty query"
337
+
338
  # Format translation prompt
339
  translation_prompt = self._format_prompt(
340
  role="Translate the following text to English",
 
343
  )
344
  logging.info(f"Translation prompt: {translation_prompt}")
345
 
346
+ # Validate translation prompt
347
+ if not translation_prompt or not translation_prompt.strip():
348
+ logging.error("Empty translation prompt generated")
349
+ return "Error: Unable to generate translation prompt"
350
+
351
  # Get English translation
352
  translation = self.translator.run(prompt=translation_prompt, stream=False)
353
  logging.info(f"Translation result type: {type(translation)}")
 
364
  translation_content = str(translation)
365
  logging.info(f"Translation as string: {translation_content}")
366
 
367
+ # Validate translation content
368
+ if not translation_content or not translation_content.strip():
369
+ logging.error("Empty translation content")
370
+ return "Error: Empty translation result"
371
 
372
  # Format research prompt
373
  research_prompt = self._format_prompt(
 
377
  )
378
  logging.info(f"Research prompt: {research_prompt}")
379
 
380
+ # Validate research prompt
381
+ if not research_prompt or not research_prompt.strip():
382
+ logging.error("Empty research prompt generated")
383
+ return "Error: Unable to generate research prompt"
384
+
385
  # Get research results
386
  research_results = self.researcher.run(prompt=research_prompt, stream=False)
387
  logging.info(f"Research results type: {type(research_results)}")
 
398
  research_content = str(research_results)
399
  logging.info(f"Research as string: {research_content}")
400
 
401
+ # Validate research content
402
+ if not research_content or not research_content.strip():
403
+ logging.error("Empty research content")
404
+ return "Error: Empty research result"
405
+
406
  logging.info(f"Research results: {research_results}")
407
 
408
  # Format summary prompt
 
413
  )
414
  logging.info(f"Summary prompt: {summary_prompt}")
415
 
416
+ # Validate summary prompt
417
+ if not summary_prompt or not summary_prompt.strip():
418
+ logging.error("Empty summary prompt generated")
419
+ return "Error: Unable to generate summary prompt"
420
+
421
  # Get summary
422
  summary = self.summarizer.run(prompt=summary_prompt, stream=False)
423
  logging.info(f"Summary type: {type(summary)}")
 
434
  summary_content = str(summary)
435
  logging.info(f"Summary as string: {summary_content}")
436
 
437
+ # Validate summary content
438
+ if not summary_content or not summary_content.strip():
439
+ logging.error("Empty summary content")
440
+ return "Error: Empty summary result"
441
+
442
  logging.info(f"Summary: {summary}")
443
 
444
  # Format presentation prompt
 
449
  )
450
  logging.info(f"Presentation prompt: {presentation_prompt}")
451
 
452
+ # Validate presentation prompt
453
+ if not presentation_prompt or not presentation_prompt.strip():
454
+ logging.error("Empty presentation prompt generated")
455
+ return "Error: Unable to generate presentation prompt"
456
+
457
  # Get presentation
458
  presentation = self.presenter.run(prompt=presentation_prompt, stream=False)
459
  logging.info(f"Presentation type: {type(presentation)}")