File size: 1,823 Bytes
2214088
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from src.pipeline import QAPipeline
from langchain_community.document_loaders import CSVLoader
import os
import tempfile
import asyncio
import nest_asyncio

nest_asyncio.apply()

os.environ["STREAMLIT_WATCHER_IGNORE_PATTERNS"] = "*/torch/*"

pipeline = QAPipeline()

st.title("Recipe Q&A")

# File upload section
st.header("Upload CSV")
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is not None:
    if st.button("Add Documents"):
        with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_file:
            tmp_file.write(uploaded_file.read())
            tmp_path = tmp_file.name
        loader = CSVLoader(file_path=tmp_path)
        data = loader.load()
        with st.spinner("Uploading documents..."):
            asyncio.run(pipeline.upload_documents(data))
        os.remove(tmp_path)
        st.success("Documents uploaded successfully.")

# 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:
    with st.spinner("Getting answer..."):
        response = asyncio.run(pipeline.answer_query_(query))
        st.session_state.response = response  
        st.write("**Answer:**")
        st.write(response.answer)

if st.session_state.response:
    if st.session_state.response.web_search:
        if st.button("Search the web for this?"):
            with st.spinner("Searching web..."):
                web_response = asyncio.run(pipeline.search_web(query))
                if web_response:
                    st.write("**Web Search Result:**")
                    st.write(web_response)
                else:
                    st.write("No web search result found.")