File size: 2,210 Bytes
2214088 4fc875c 2214088 4fc875c 2214088 4fc875c 2214088 4fc875c 6a5fa85 4fc875c 2214088 4fc875c 2214088 4fc875c 2214088 4fc875c 2214088 4fc875c 2214088 4fc875c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import streamlit as st
import logging
from src.pipeline import QAPipeline
from langchain_community.document_loaders import CSVLoader
import os
import tempfile
import asyncio
import nest_asyncio
# Apply nest_asyncio
nest_asyncio.apply()
# Set Streamlit watcher ignore pattern for torch
os.environ["STREAMLIT_WATCHER_IGNORE_PATTERNS"] = "*/torch/*"
logging.basicConfig(
filename="/tmp/app.log",
filemode="a",
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.INFO
)
logger = logging.getLogger(__name__)
pipeline = QAPipeline()
st.title("Recipe Q&A")
logger.info("App started.")
# Query section
st.header("Ask a Question")
query = st.text_input("Enter your question:")
if "response" not in st.session_state:
st.session_state.response = None
# Get answer
if st.button("Get Answer") and query:
logger.info(f"Received query: {query}")
with st.spinner("Getting answer..."):
try:
response = asyncio.run(pipeline.answer_query_(query))
st.session_state.response = response
logger.info(f"Response: {response.answer}")
st.write("**Answer:**")
st.write(response.answer)
except Exception as e:
logger.error(f"Error while getting answer: {e}")
st.error("An error occurred while processing your query.")
if st.session_state.response:
if st.session_state.response.web_search:
if st.button("Search the web for this?"):
logger.info(f"Initiating web search for query: {query}")
with st.spinner("Searching web..."):
try:
web_response = asyncio.run(pipeline.search_web(query))
if web_response:
logger.info(f"Web response: {web_response}")
st.write("**Web Search Result:**")
st.write(web_response)
else:
logger.info("No web search results found.")
st.write("No web search result found.")
except Exception as e:
logger.error(f"Error during web search: {e}")
st.error("Web search failed.")
|