Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,7 +23,6 @@ Answer the question based on the above context: {question}
|
|
| 23 |
"""
|
| 24 |
|
| 25 |
def load_documents():
|
| 26 |
-
|
| 27 |
loader = DirectoryLoader(DATA_PATH, glob="*.pdf")
|
| 28 |
documents = loader.load()
|
| 29 |
return documents
|
|
@@ -64,7 +63,12 @@ def get_response(query_text):
|
|
| 64 |
if len(results) == 0 or results[0][1] < 0.7:
|
| 65 |
print(f"Unable to find matching results.")
|
| 66 |
return
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
| 69 |
|
| 70 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
|
@@ -74,25 +78,25 @@ def get_response(query_text):
|
|
| 74 |
model = ChatOpenAI()
|
| 75 |
response_text = model.predict(prompt)
|
| 76 |
|
| 77 |
-
sources = [doc.metadata.get("source", None) for doc, _score in results]
|
| 78 |
-
|
|
|
|
| 79 |
return formatted_response
|
| 80 |
|
| 81 |
-
def
|
| 82 |
documents = load_documents()
|
| 83 |
chunks = split_text(documents)
|
| 84 |
save_to_chroma(chunks)
|
| 85 |
-
response = get_response(query_text)
|
| 86 |
-
return response
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
-
iface = gr.Interface(fn=
|
| 92 |
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
|
| 93 |
outputs="text",
|
| 94 |
title="UK Insurance Law AI Tool")
|
| 95 |
|
| 96 |
|
| 97 |
|
|
|
|
| 98 |
iface.launch()
|
|
|
|
| 23 |
"""
|
| 24 |
|
| 25 |
def load_documents():
|
|
|
|
| 26 |
loader = DirectoryLoader(DATA_PATH, glob="*.pdf")
|
| 27 |
documents = loader.load()
|
| 28 |
return documents
|
|
|
|
| 63 |
if len(results) == 0 or results[0][1] < 0.7:
|
| 64 |
print(f"Unable to find matching results.")
|
| 65 |
return
|
| 66 |
+
|
| 67 |
+
sources = []
|
| 68 |
+
for doc, score in results:
|
| 69 |
+
source_with_page = f"{doc.metadata.get('source', None)} (Page {doc.page_number})"
|
| 70 |
+
sources.append(source_with_page)
|
| 71 |
+
|
| 72 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
| 73 |
|
| 74 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
|
|
|
| 78 |
model = ChatOpenAI()
|
| 79 |
response_text = model.predict(prompt)
|
| 80 |
|
| 81 |
+
#sources = [doc.metadata.get("source", None) for doc, _score in results]
|
| 82 |
+
|
| 83 |
+
formatted_response = f"Response: {response_text}\nSources: {', '.join(sources)}"
|
| 84 |
return formatted_response
|
| 85 |
|
| 86 |
+
def prepare():
|
| 87 |
documents = load_documents()
|
| 88 |
chunks = split_text(documents)
|
| 89 |
save_to_chroma(chunks)
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
+
iface = gr.Interface(fn=get_response,
|
| 95 |
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
|
| 96 |
outputs="text",
|
| 97 |
title="UK Insurance Law AI Tool")
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
+
prepare()
|
| 102 |
iface.launch()
|