Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,39 +10,63 @@ nest_asyncio.apply()
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
def get_answer(query,chain):
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
st.title("MULTIMODAL DOC QA")
|
| 18 |
-
|
|
|
|
| 19 |
if uploaded_file is not None:
|
| 20 |
# Save the uploaded file to a temporary location
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
f.write(uploaded_file.getbuffer())
|
| 23 |
|
| 24 |
# Get the absolute path of the saved file
|
| 25 |
-
path = os.path.abspath(
|
| 26 |
st.write(f"File saved to: {path}")
|
| 27 |
print(path)
|
| 28 |
|
| 29 |
-
st.write("Document uploaded
|
| 30 |
-
|
| 31 |
|
| 32 |
if st.button("Start Processing"):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
if user_input := st.chat_input("User Input"):
|
| 40 |
-
|
| 41 |
-
st.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
def get_answer(query, chain):
|
| 14 |
+
try:
|
| 15 |
+
response = chain.invoke(query)
|
| 16 |
+
return response['result']
|
| 17 |
+
except Exception as e:
|
| 18 |
+
st.error(f"Error in get_answer: {e}")
|
| 19 |
+
return None
|
| 20 |
|
| 21 |
st.title("MULTIMODAL DOC QA")
|
| 22 |
+
|
| 23 |
+
uploaded_file = st.file_uploader("File upload", type="pdf")
|
| 24 |
if uploaded_file is not None:
|
| 25 |
# Save the uploaded file to a temporary location
|
| 26 |
+
temp_file_path = os.path.join("temp", uploaded_file.name)
|
| 27 |
+
os.makedirs("temp", exist_ok=True) # Ensure the temp directory exists
|
| 28 |
+
with open(temp_file_path, "wb") as f:
|
| 29 |
f.write(uploaded_file.getbuffer())
|
| 30 |
|
| 31 |
# Get the absolute path of the saved file
|
| 32 |
+
path = os.path.abspath(temp_file_path)
|
| 33 |
st.write(f"File saved to: {path}")
|
| 34 |
print(path)
|
| 35 |
|
| 36 |
+
st.write("Document uploaded successfully!")
|
|
|
|
| 37 |
|
| 38 |
if st.button("Start Processing"):
|
| 39 |
+
if uploaded_file is not None:
|
| 40 |
+
with st.spinner("Processing"):
|
| 41 |
+
try:
|
| 42 |
+
client = create_vector_database(path)
|
| 43 |
+
image_vdb = extract_and_store_images(path)
|
| 44 |
+
chain = qa_bot(client)
|
| 45 |
+
st.session_state['chain'] = chain # Store chain in session state
|
| 46 |
+
st.session_state['image_vdb'] = image_vdb # Store image_vdb in session state
|
| 47 |
+
st.success("Processing complete.")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"Error during processing: {e}")
|
| 50 |
+
else:
|
| 51 |
+
st.error("Please upload a file before starting processing.")
|
| 52 |
|
| 53 |
if user_input := st.chat_input("User Input"):
|
| 54 |
+
if 'chain' in st.session_state and 'image_vdb' in st.session_state:
|
| 55 |
+
chain = st.session_state['chain']
|
| 56 |
+
image_vdb = st.session_state['image_vdb']
|
| 57 |
+
|
| 58 |
+
with st.chat_message("user"):
|
| 59 |
+
st.markdown(user_input)
|
| 60 |
+
|
| 61 |
+
with st.spinner("Generating Response..."):
|
| 62 |
+
response = get_answer(user_input, chain)
|
| 63 |
+
if response:
|
| 64 |
+
st.markdown(response)
|
| 65 |
+
try:
|
| 66 |
+
query_and_print_results(image_vdb, user_input)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"Error querying image database: {e}")
|
| 69 |
+
else:
|
| 70 |
+
st.error("Failed to generate response.")
|
| 71 |
+
else:
|
| 72 |
+
st.error("Please start processing before entering user input.")
|