Abid Ali Awan commited on
Commit
66b553a
1 Parent(s): 2987ac4

improve the exception handling

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -50,25 +50,34 @@ llm = Groq(model="llama-3.1-70b-versatile", api_key=groq_key)
50
  # File processing function
51
  def load_files(file_path: str):
52
  global vector_index
53
- document = SimpleDirectoryReader(
54
- input_files=[file_path], file_extractor=file_extractor
55
- ).load_data()
 
 
 
 
 
56
  vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
57
- print(f"Parsing done for {file_path}")
58
  filename = os.path.basename(file_path)
59
- return f"Ready to provide responses based on {filename}"
60
 
61
 
62
  # Respond function
63
  def respond(message, history):
64
- # Use the preloaded LLM
65
- query_engine = vector_index.as_query_engine(streaming=True, llm=llm)
66
- streaming_response = query_engine.query(message)
67
- partial_text = ""
68
- for new_text in streaming_response.response_gen:
69
- partial_text += new_text
70
- # Yield an empty string to cleanup the message textbox and the updated conversation history
71
- yield partial_text
 
 
 
 
72
 
73
 
74
  # Clear function
 
50
  # File processing function
51
  def load_files(file_path: str):
52
  global vector_index
53
+ if not file_path:
54
+ return "No file path provided. Please upload a file."
55
+
56
+ valid_extensions = ', '.join(file_extractor.keys())
57
+ if not any(file_path.endswith(ext) for ext in file_extractor):
58
+ return f"The parser can only parse the following file types: {valid_extensions}"
59
+
60
+ document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
61
  vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
62
+ print(f"Parsing completed for: {file_path}")
63
  filename = os.path.basename(file_path)
64
+ return f"Ready to provide responses based on: {filename}"
65
 
66
 
67
  # Respond function
68
  def respond(message, history):
69
+ try:
70
+ # Use the preloaded LLM
71
+ query_engine = vector_index.as_query_engine(streaming=True, llm=llm)
72
+ streaming_response = query_engine.query(message)
73
+ partial_text = ""
74
+ for new_text in streaming_response.response_gen:
75
+ partial_text += new_text
76
+ # Yield an empty string to cleanup the message textbox and the updated conversation history
77
+ yield partial_text
78
+ except (AttributeError, NameError):
79
+ print("An error occurred while processing your request.")
80
+ yield "Please upload the file to begin chat."
81
 
82
 
83
  # Clear function