Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
import os
|
|
|
4 |
from collections.abc import Collection
|
5 |
from langchain.memory import ChatMessageHistory
|
6 |
from langchain_community.chat_message_histories import (
|
@@ -147,12 +148,19 @@ for msg in history.messages:
|
|
147 |
|
148 |
# preprocessing context
|
149 |
def format_docs_with_metadata(docs):
|
150 |
-
formatted_docs = []
|
151 |
-
for i, doc in enumerate(docs, start=1):
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
return "\n\n".join(formatted_docs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
def stream_data(response):
|
158 |
for word in response.split(" "):
|
@@ -169,6 +177,7 @@ if prompt := st.chat_input():
|
|
169 |
|
170 |
with st.chat_message("AI"):
|
171 |
st.write_stream(stream_data(res['answer']))
|
172 |
-
with st.
|
173 |
-
st.
|
174 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
import os
|
4 |
+
import pandas as pd
|
5 |
from collections.abc import Collection
|
6 |
from langchain.memory import ChatMessageHistory
|
7 |
from langchain_community.chat_message_histories import (
|
|
|
148 |
|
149 |
# preprocessing context
|
150 |
def format_docs_with_metadata(docs):
|
151 |
+
# formatted_docs = []
|
152 |
+
# for i, doc in enumerate(docs, start=1):
|
153 |
+
# metadata_str = "\n".join([f"**{key}**: `{value}`\n" for key, value in doc.metadata.items() if key != "embedding"])
|
154 |
+
# formatted_doc = f"- {doc.page_content}\n\n**Metadata:**\n{metadata_str}"
|
155 |
+
# formatted_docs.append(formatted_doc)
|
156 |
+
# return "\n\n".join(formatted_docs)
|
157 |
+
rows = []
|
158 |
+
for doc in docs:
|
159 |
+
row = {'Content': doc.page_content}
|
160 |
+
row.update({k: v for k, v in doc.metadata.items() if k != 'embedding'})
|
161 |
+
rows.append(row)
|
162 |
+
return pd.DataFrame(rows)
|
163 |
+
|
164 |
|
165 |
def stream_data(response):
|
166 |
for word in response.split(" "):
|
|
|
177 |
|
178 |
with st.chat_message("AI"):
|
179 |
st.write_stream(stream_data(res['answer']))
|
180 |
+
with st.expander("View Source"):
|
181 |
+
st.subheader("Source Alerts 📢")
|
182 |
+
df = format_docs_as_table(res['context'])
|
183 |
+
st.table(df)
|