Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from src.pipeline import QAPipeline
|
3 |
from langchain_community.document_loaders import CSVLoader
|
4 |
import os
|
@@ -6,28 +7,25 @@ import tempfile
|
|
6 |
import asyncio
|
7 |
import nest_asyncio
|
8 |
|
|
|
9 |
nest_asyncio.apply()
|
10 |
|
|
|
11 |
os.environ["STREAMLIT_WATCHER_IGNORE_PATTERNS"] = "*/torch/*"
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
pipeline = QAPipeline()
|
14 |
|
15 |
st.title("Recipe Q&A")
|
16 |
-
|
17 |
-
# File upload section
|
18 |
-
# st.header("Upload CSV")
|
19 |
-
# uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
20 |
-
# if uploaded_file is not None:
|
21 |
-
# if st.button("Add Documents"):
|
22 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_file:
|
23 |
-
# tmp_file.write(uploaded_file.read())
|
24 |
-
# tmp_path = tmp_file.name
|
25 |
-
# loader = CSVLoader(file_path=tmp_path)
|
26 |
-
# data = loader.load()
|
27 |
-
# with st.spinner("Uploading documents..."):
|
28 |
-
# asyncio.run(pipeline.upload_documents(data))
|
29 |
-
# os.remove(tmp_path)
|
30 |
-
# st.success("Documents uploaded successfully.")
|
31 |
|
32 |
# Query section
|
33 |
st.header("Ask a Question")
|
@@ -38,19 +36,32 @@ if "response" not in st.session_state:
|
|
38 |
|
39 |
# Get answer
|
40 |
if st.button("Get Answer") and query:
|
|
|
41 |
with st.spinner("Getting answer..."):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
if st.session_state.response:
|
48 |
if st.session_state.response.web_search:
|
49 |
if st.button("Search the web for this?"):
|
|
|
50 |
with st.spinner("Searching web..."):
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import logging
|
3 |
from src.pipeline import QAPipeline
|
4 |
from langchain_community.document_loaders import CSVLoader
|
5 |
import os
|
|
|
7 |
import asyncio
|
8 |
import nest_asyncio
|
9 |
|
10 |
+
# Apply nest_asyncio
|
11 |
nest_asyncio.apply()
|
12 |
|
13 |
+
# Set Streamlit watcher ignore pattern for torch
|
14 |
os.environ["STREAMLIT_WATCHER_IGNORE_PATTERNS"] = "*/torch/*"
|
15 |
|
16 |
+
# Configure logger
|
17 |
+
logging.basicConfig(
|
18 |
+
filename="app.log",
|
19 |
+
filemode="a",
|
20 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
21 |
+
level=logging.INFO
|
22 |
+
)
|
23 |
+
logger = logging.getLogger(__name__)
|
24 |
+
|
25 |
pipeline = QAPipeline()
|
26 |
|
27 |
st.title("Recipe Q&A")
|
28 |
+
logger.info("App started.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Query section
|
31 |
st.header("Ask a Question")
|
|
|
36 |
|
37 |
# Get answer
|
38 |
if st.button("Get Answer") and query:
|
39 |
+
logger.info(f"Received query: {query}")
|
40 |
with st.spinner("Getting answer..."):
|
41 |
+
try:
|
42 |
+
response = asyncio.run(pipeline.answer_query_(query))
|
43 |
+
st.session_state.response = response
|
44 |
+
logger.info(f"Response: {response.answer}")
|
45 |
+
st.write("**Answer:**")
|
46 |
+
st.write(response.answer)
|
47 |
+
except Exception as e:
|
48 |
+
logger.error(f"Error while getting answer: {e}")
|
49 |
+
st.error("An error occurred while processing your query.")
|
50 |
|
51 |
if st.session_state.response:
|
52 |
if st.session_state.response.web_search:
|
53 |
if st.button("Search the web for this?"):
|
54 |
+
logger.info(f"Initiating web search for query: {query}")
|
55 |
with st.spinner("Searching web..."):
|
56 |
+
try:
|
57 |
+
web_response = asyncio.run(pipeline.search_web(query))
|
58 |
+
if web_response:
|
59 |
+
logger.info(f"Web response: {web_response}")
|
60 |
+
st.write("**Web Search Result:**")
|
61 |
+
st.write(web_response)
|
62 |
+
else:
|
63 |
+
logger.info("No web search results found.")
|
64 |
+
st.write("No web search result found.")
|
65 |
+
except Exception as e:
|
66 |
+
logger.error(f"Error during web search: {e}")
|
67 |
+
st.error("Web search failed.")
|