Sayandip commited on
Commit
e309944
·
verified ·
1 Parent(s): f7ad8ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
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 for conversation history
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 (important for Streamlit)
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 # Clear document content to prevent further processing
 
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) # Display a snippet
92
-
93
 
94
- # Conversation Loop
95
- while True:
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.lower() == "exit":
99
- st.write("Ending conversation.")
100
- break
101
-
102
- if question and st.session_state.document_content:
103
- answer = process_document_and_answer(st.session_state.document_content, question, st.session_state.conversation_history)
104
-
105
- st.write("Answer:", answer)
106
 
107
- # Update conversation history
108
- st.session_state.conversation_history += f"\nQuestion: {question}\nAnswer: {answer}"
 
109
 
110
 
111
- # Display conversation history
112
- if st.session_state.conversation_history:
113
- st.subheader("Conversation History:")
114
- st.text(st.session_state.conversation_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
+ 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)