Spaces:
Runtime error
Runtime error
Update flask_app.py
Browse files- flask_app.py +10 -3
flask_app.py
CHANGED
|
@@ -165,14 +165,21 @@ def model_inference(retriever, question, llm):
|
|
| 165 |
match = re.findall(r"Answer:\s*(.*)", response, re.DOTALL)
|
| 166 |
final_answer = "Answer:\n " + match[-1].strip() if match else "Answer:\n The answer is not found in the context provided."
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
references = (
|
| 170 |
-
f"Source: {retrieved_docs[0].metadata['source']
|
| 171 |
if retrieved_docs else "No references found."
|
| 172 |
)
|
| 173 |
-
|
| 174 |
return f"{final_answer}\nReferences: {references}\nContext: {context}"
|
| 175 |
|
|
|
|
| 176 |
# # Example usage:
|
| 177 |
# formatted_output = model_inference(retriever, "What is the main purpose of the Khyber Pakhtunkhwa Arms Act, 2013?", llm)
|
| 178 |
|
|
|
|
| 165 |
match = re.findall(r"Answer:\s*(.*)", response, re.DOTALL)
|
| 166 |
final_answer = "Answer:\n " + match[-1].strip() if match else "Answer:\n The answer is not found in the context provided."
|
| 167 |
|
| 168 |
+
|
| 169 |
+
# Extract document title by removing file extension and trailing numbers/versions
|
| 170 |
+
def extract_title(file_name):
|
| 171 |
+
base_name = os.path.splitext(file_name)[0] # Remove file extension
|
| 172 |
+
title = re.sub(r'\s*(final\d*|v\d+|rev\d+)$', '', base_name, flags=re.IGNORECASE)
|
| 173 |
+
return title.strip()
|
| 174 |
+
|
| 175 |
references = (
|
| 176 |
+
f"Source: {extract_title(retrieved_docs[0].metadata['source'])}"
|
| 177 |
if retrieved_docs else "No references found."
|
| 178 |
)
|
| 179 |
+
|
| 180 |
return f"{final_answer}\nReferences: {references}\nContext: {context}"
|
| 181 |
|
| 182 |
+
|
| 183 |
# # Example usage:
|
| 184 |
# formatted_output = model_inference(retriever, "What is the main purpose of the Khyber Pakhtunkhwa Arms Act, 2013?", llm)
|
| 185 |
|