Spaces:
Runtime error
Runtime error
on1onmangoes
commited on
Commit
•
13469d1
1
Parent(s):
18809a3
Update app.py
Browse files
app.py
CHANGED
@@ -16,27 +16,6 @@ client = Client("on1onmangoes/CNIHUB101324v10", hf_token=HF_TOKEN)
|
|
16 |
# Update the conversation history within the function.
|
17 |
# Return the updated history along with any other required outputs.
|
18 |
|
19 |
-
def format_answer_with_documents(answer):
|
20 |
-
"""
|
21 |
-
This function formats the assistant's answer and separates the personal details from the relevant documents.
|
22 |
-
"""
|
23 |
-
# Extract the personal details and documents from the answer
|
24 |
-
personal_details = answer[0]
|
25 |
-
relevant_documents = answer[1:] # Assuming documents are included after the answer
|
26 |
-
|
27 |
-
# Format the personal details
|
28 |
-
formatted_answer = f"Based on the documents provided, the personal details are as follows:\n\n{personal_details}\n\n"
|
29 |
-
|
30 |
-
# If there are relevant documents, format them as a separate section
|
31 |
-
if relevant_documents:
|
32 |
-
formatted_answer += "Relevant Documents:\n"
|
33 |
-
for idx, document in enumerate(relevant_documents, start=1):
|
34 |
-
formatted_answer += f"{idx}. {document.metadata['heading']} (Page {int(document.metadata['page_number'])})\n"
|
35 |
-
formatted_answer += f" Source: {document.metadata['source']}\n"
|
36 |
-
formatted_answer += f" Snippet: {document.page_content[:200]}...\n\n" # Showing a snippet of content
|
37 |
-
|
38 |
-
return formatted_answer
|
39 |
-
|
40 |
def stream_chat_with_rag(
|
41 |
message: str,
|
42 |
history: list,
|
@@ -66,13 +45,13 @@ def stream_chat_with_rag(
|
|
66 |
# Call the API with the user's message
|
67 |
question = message
|
68 |
answer = client.predict(question=question, api_name="/answer_with_rag")
|
69 |
-
|
70 |
-
# Debugging: Print the response
|
71 |
-
print("
|
72 |
print(answer)
|
73 |
|
74 |
-
# Format the assistant's answer
|
75 |
-
formatted_answer =
|
76 |
|
77 |
# Update the conversation history with the new message and answer
|
78 |
history.append((message, formatted_answer))
|
@@ -81,6 +60,35 @@ def stream_chat_with_rag(
|
|
81 |
return formatted_answer
|
82 |
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
# this version works just issue with formatting
|
85 |
# def stream_chat_with_rag(
|
86 |
# message: str,
|
|
|
16 |
# Update the conversation history within the function.
|
17 |
# Return the updated history along with any other required outputs.
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def stream_chat_with_rag(
|
20 |
message: str,
|
21 |
history: list,
|
|
|
45 |
# Call the API with the user's message
|
46 |
question = message
|
47 |
answer = client.predict(question=question, api_name="/answer_with_rag")
|
48 |
+
|
49 |
+
# Debugging: Print the raw response
|
50 |
+
print("Raw answer from API:")
|
51 |
print(answer)
|
52 |
|
53 |
+
# Format the assistant's answer and the relevant documents separately
|
54 |
+
formatted_answer = format_answer_string(answer)
|
55 |
|
56 |
# Update the conversation history with the new message and answer
|
57 |
history.append((message, formatted_answer))
|
|
|
60 |
return formatted_answer
|
61 |
|
62 |
|
63 |
+
def format_answer_string(answer: str):
|
64 |
+
"""
|
65 |
+
This function takes the full string returned by the API (which includes both the assistant's answer and documents)
|
66 |
+
and splits it into separate sections: the assistant's answer and the relevant documents.
|
67 |
+
"""
|
68 |
+
# Step 1: Split the answer by the point where document context starts
|
69 |
+
split_marker = "Extracted Documents with Descriptive Context:"
|
70 |
+
if split_marker in answer:
|
71 |
+
assistant_answer, documents_section = answer.split(split_marker, 1)
|
72 |
+
else:
|
73 |
+
# If no documents found, return the whole answer as the assistant's response
|
74 |
+
return f"Assistant: {answer.strip()}"
|
75 |
+
|
76 |
+
# Step 2: Clean and format the assistant's answer
|
77 |
+
formatted_answer = f"Assistant: {assistant_answer.strip()}\n\n"
|
78 |
+
|
79 |
+
# Step 3: Format the documents section
|
80 |
+
formatted_answer += "Relevant Documents:\n"
|
81 |
+
document_entries = documents_section.split("Document ")
|
82 |
+
|
83 |
+
# Step 4: Reformat each document entry
|
84 |
+
for entry in document_entries[1:]: # Skip the first empty split
|
85 |
+
doc_number, rest = entry.split(":", 1)
|
86 |
+
formatted_answer += f"{doc_number}. {rest.strip()}\n\n"
|
87 |
+
|
88 |
+
return formatted_answer
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
# this version works just issue with formatting
|
93 |
# def stream_chat_with_rag(
|
94 |
# message: str,
|