Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -63,18 +63,22 @@ def process_document_and_answer(context, question, history=""):
|
|
| 63 |
# Streamlit UI
|
| 64 |
st.title("Gemini Document Q&A")
|
| 65 |
|
| 66 |
-
# Initialize session state
|
| 67 |
if 'conversation_history' not in st.session_state:
|
| 68 |
st.session_state.conversation_history = ""
|
| 69 |
|
| 70 |
if 'document_content' not in st.session_state:
|
| 71 |
st.session_state.document_content = None
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# File Upload
|
| 74 |
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
| 75 |
|
| 76 |
if uploaded_file is not None:
|
| 77 |
-
# Save the uploaded file to a temporary location
|
| 78 |
temp_file_path = "temp." + uploaded_file.name.split('.')[-1].lower()
|
| 79 |
with open(temp_file_path, "wb") as temp_file:
|
| 80 |
temp_file.write(uploaded_file.read())
|
|
@@ -84,31 +88,29 @@ if uploaded_file is not None:
|
|
| 84 |
|
| 85 |
if "Error processing file" in st.session_state.document_content:
|
| 86 |
st.error(st.session_state.document_content)
|
| 87 |
-
st.session_state.document_content = None
|
|
|
|
| 88 |
|
| 89 |
if st.session_state.document_content:
|
| 90 |
st.subheader("Document Content Preview:")
|
| 91 |
-
st.text(st.session_state.document_content[:500] + "..." if len(st.session_state.document_content) > 500 else st.session_state.document_content)
|
| 92 |
-
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
question = st.text_input("Ask a question about the document (type 'exit' to end):", key=f"question_{len(st.session_state.conversation_history)}") # Unique key for each input
|
| 97 |
|
| 98 |
-
if question
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.write("Answer:", answer)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
-
# Display conversation history
|
| 112 |
-
if st.session_state.conversation_history:
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 63 |
# Streamlit UI
|
| 64 |
st.title("Gemini Document Q&A")
|
| 65 |
|
| 66 |
+
# Initialize session state
|
| 67 |
if 'conversation_history' not in st.session_state:
|
| 68 |
st.session_state.conversation_history = ""
|
| 69 |
|
| 70 |
if 'document_content' not in st.session_state:
|
| 71 |
st.session_state.document_content = None
|
| 72 |
|
| 73 |
+
if 'question_count' not in st.session_state:
|
| 74 |
+
st.session_state.question_count = 0
|
| 75 |
+
|
| 76 |
+
|
| 77 |
# File Upload
|
| 78 |
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
| 79 |
|
| 80 |
if uploaded_file is not None:
|
| 81 |
+
# Save the uploaded file to a temporary location
|
| 82 |
temp_file_path = "temp." + uploaded_file.name.split('.')[-1].lower()
|
| 83 |
with open(temp_file_path, "wb") as temp_file:
|
| 84 |
temp_file.write(uploaded_file.read())
|
|
|
|
| 88 |
|
| 89 |
if "Error processing file" in st.session_state.document_content:
|
| 90 |
st.error(st.session_state.document_content)
|
| 91 |
+
st.session_state.document_content = None
|
| 92 |
+
|
| 93 |
|
| 94 |
if st.session_state.document_content:
|
| 95 |
st.subheader("Document Content Preview:")
|
| 96 |
+
st.text(st.session_state.document_content[:500] + "..." if len(st.session_state.document_content) > 500 else st.session_state.document_content)
|
|
|
|
| 97 |
|
| 98 |
+
# Question Input
|
| 99 |
+
question = st.text_input("Ask a question about the document (type 'exit' to end):", key=f"question_{st.session_state.question_count}")
|
|
|
|
| 100 |
|
| 101 |
+
if question:
|
| 102 |
+
if question.lower() == "exit":
|
| 103 |
+
st.write("Ending conversation.")
|
| 104 |
+
else:
|
| 105 |
+
answer = process_document_and_answer(st.session_state.document_content, question, st.session_state.conversation_history)
|
| 106 |
+
st.write("Answer:", answer)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# Update conversation history and question count
|
| 109 |
+
st.session_state.conversation_history += f"\nQuestion: {question}\nAnswer: {answer}"
|
| 110 |
+
st.session_state.question_count += 1
|
| 111 |
|
| 112 |
|
| 113 |
+
# Display conversation history
|
| 114 |
+
if st.session_state.conversation_history:
|
| 115 |
+
st.subheader("Conversation History:")
|
| 116 |
+
st.text(st.session_state.conversation_history)
|