menikev commited on
Commit
207f66e
·
verified ·
1 Parent(s): 949826f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import os
3
  from langchain_community.document_loaders import PyPDFLoader
4
  from langchain_community.vectorstores import FAISS
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
@@ -16,7 +15,7 @@ warnings.filterwarnings('ignore')
16
 
17
  # Set your Hugging Face API token here.
18
  # For deployment on Hugging Face, you can set this as an environment variable.
19
-
20
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_YOUR_HUGGINGFACE_TOKEN"
21
 
22
  ## LLM - Using an open-source model from Hugging Face
@@ -78,6 +77,11 @@ def retriever(file_path):
78
  """
79
  splits = document_loader(file_path)
80
  chunks = text_splitter(splits)
 
 
 
 
 
81
  vectordb = vector_database(chunks)
82
  retriever = vectordb.as_retriever()
83
  return retriever
@@ -95,8 +99,11 @@ def retriever_qa(file, query):
95
  return "Please upload a valid PDF file before asking a question."
96
 
97
  llm = get_llm()
98
- retriever_obj = retriever(file_path)
99
-
 
 
 
100
  # Custom prompt to act as a conversational legal advisor
101
  prompt_template = f"""
102
  You are a friendly and professional legal advisor. Your goal is to provide concise and contextual legal advice based on the provided document.
 
1
  import gradio as gr
 
2
  from langchain_community.document_loaders import PyPDFLoader
3
  from langchain_community.vectorstores import FAISS
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
15
 
16
  # Set your Hugging Face API token here.
17
  # For deployment on Hugging Face, you can set this as an environment variable.
18
+ import os
19
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_YOUR_HUGGINGFACE_TOKEN"
20
 
21
  ## LLM - Using an open-source model from Hugging Face
 
77
  """
78
  splits = document_loader(file_path)
79
  chunks = text_splitter(splits)
80
+
81
+ # Add a check to ensure chunks are not empty
82
+ if not chunks:
83
+ raise ValueError("The uploaded document could not be processed. Please try another file.")
84
+
85
  vectordb = vector_database(chunks)
86
  retriever = vectordb.as_retriever()
87
  return retriever
 
99
  return "Please upload a valid PDF file before asking a question."
100
 
101
  llm = get_llm()
102
+ try:
103
+ retriever_obj = retriever(file_path)
104
+ except ValueError as e:
105
+ return str(e)
106
+
107
  # Custom prompt to act as a conversational legal advisor
108
  prompt_template = f"""
109
  You are a friendly and professional legal advisor. Your goal is to provide concise and contextual legal advice based on the provided document.