Sayandip commited on
Commit
563637e
·
verified ·
1 Parent(s): 4fa8847

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -40
app.py CHANGED
@@ -84,53 +84,59 @@ def export_conversation_to_pdf(conversation_history):
84
 
85
  def main():
86
  st.set_page_config(page_title="Gemini Chat QA", layout="wide")
87
- st.title("📄 Chat with Your Document using Gemini (Sayandip+Dhiraj+Fazal)")
88
 
89
  # Initialize session state attributes if they don't exist
90
  if "conversation" not in st.session_state:
91
  st.session_state.conversation = []
92
- if "document_text" not in st.session_state:
93
- st.session_state.document_text = ""
94
  if "chat_active" not in st.session_state:
95
  st.session_state.chat_active = True
96
  if "chat_history" not in st.session_state: # Initialize chat_history
97
  st.session_state.chat_history = []
98
 
99
- uploaded_file = st.file_uploader(
100
- "Upload a file (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png):",
101
- type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex", "jpg", "jpeg", "png"]
 
102
  )
103
 
104
- if uploaded_file and not st.session_state.document_text:
105
- filetype = uploaded_file.name.split(".")[-1].lower()
106
- try:
107
- if filetype == "txt":
108
- st.session_state.document_text = uploaded_file.read().decode("utf-8")
109
- elif filetype == "docx":
110
- st.session_state.document_text = extract_text_from_docx(uploaded_file)
111
- elif filetype == "csv":
112
- st.session_state.document_text = extract_text_from_csv(uploaded_file)
113
- elif filetype == "xlsx":
114
- st.session_state.document_text = extract_text_from_xlsx(uploaded_file)
115
- elif filetype == "pptx":
116
- st.session_state.document_text = extract_text_from_pptx(uploaded_file)
117
- elif filetype == "pdf":
118
- st.session_state.document_text = extract_text_from_pdf(uploaded_file)
119
- elif filetype in ["html", "htm"]:
120
- st.session_state.document_text = extract_text_from_html(uploaded_file)
121
- elif filetype == "tex":
122
- st.session_state.document_text = extract_text_from_tex(uploaded_file)
123
- elif filetype in ["jpg", "jpeg", "png"]:
124
- image_data = process_image(uploaded_file)
125
- st.session_state.document_text = "Image uploaded and processed."
126
- st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
127
- st.session_state.image_data = image_data # Store the image data for later use
128
- else:
129
- st.error("Unsupported file format.")
130
- except Exception as e:
131
- st.error(f"Failed to extract text: {e}")
132
-
133
- if st.session_state.document_text:
 
 
 
 
 
134
  st.markdown("### 💬 Conversation")
135
  for user_q, gemini_a in st.session_state.conversation:
136
  st.markdown(f"**You:** {user_q}")
@@ -138,7 +144,7 @@ def main():
138
 
139
  if st.session_state.chat_active:
140
  with st.form(key="chat_form", clear_on_submit=True):
141
- user_input = st.text_input("Ask a question about the document (type 'exit' to stop):")
142
  submit = st.form_submit_button("Send")
143
 
144
  if submit and user_input:
@@ -157,9 +163,9 @@ def main():
157
  )
158
  content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
159
 
160
- # Add file text content
161
- if st.session_state.document_text.strip():
162
- content_blocks.append({"text": f"Context:\n{st.session_state.document_text}"})
163
 
164
  # Add image blocks (if image data exists)
165
  if "image_data" in st.session_state:
 
84
 
85
  def main():
86
  st.set_page_config(page_title="Gemini Chat QA", layout="wide")
87
+ st.title("📄 Chat with Your Documents using Gemini (Sayandip+Dhiraj+Fazal)")
88
 
89
  # Initialize session state attributes if they don't exist
90
  if "conversation" not in st.session_state:
91
  st.session_state.conversation = []
92
+ if "documents_text" not in st.session_state:
93
+ st.session_state.documents_text = []
94
  if "chat_active" not in st.session_state:
95
  st.session_state.chat_active = True
96
  if "chat_history" not in st.session_state: # Initialize chat_history
97
  st.session_state.chat_history = []
98
 
99
+ uploaded_files = st.file_uploader(
100
+ "Upload files (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png):",
101
+ type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex", "jpg", "jpeg", "png"],
102
+ accept_multiple_files=True
103
  )
104
 
105
+ # Process each uploaded file
106
+ if uploaded_files:
107
+ all_text_content = ""
108
+ for uploaded_file in uploaded_files:
109
+ filetype = uploaded_file.name.split(".")[-1].lower()
110
+ try:
111
+ if filetype == "txt":
112
+ all_text_content += uploaded_file.read().decode("utf-8") + "\n"
113
+ elif filetype == "docx":
114
+ all_text_content += extract_text_from_docx(uploaded_file) + "\n"
115
+ elif filetype == "csv":
116
+ all_text_content += extract_text_from_csv(uploaded_file) + "\n"
117
+ elif filetype == "xlsx":
118
+ all_text_content += extract_text_from_xlsx(uploaded_file) + "\n"
119
+ elif filetype == "pptx":
120
+ all_text_content += extract_text_from_pptx(uploaded_file) + "\n"
121
+ elif filetype == "pdf":
122
+ all_text_content += extract_text_from_pdf(uploaded_file) + "\n"
123
+ elif filetype in ["html", "htm"]:
124
+ all_text_content += extract_text_from_html(uploaded_file) + "\n"
125
+ elif filetype == "tex":
126
+ all_text_content += extract_text_from_tex(uploaded_file) + "\n"
127
+ elif filetype in ["jpg", "jpeg", "png"]:
128
+ image_data = process_image(uploaded_file)
129
+ st.session_state.image_data = image_data # Store the image data for later use
130
+ all_text_content += "Image uploaded and processed.\n"
131
+ st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
132
+ else:
133
+ st.error(f"Unsupported file format: {uploaded_file.name}")
134
+ except Exception as e:
135
+ st.error(f"Failed to extract text from {uploaded_file.name}: {e}")
136
+
137
+ st.session_state.documents_text = all_text_content # Store the compiled text from all documents
138
+
139
+ if st.session_state.documents_text:
140
  st.markdown("### 💬 Conversation")
141
  for user_q, gemini_a in st.session_state.conversation:
142
  st.markdown(f"**You:** {user_q}")
 
144
 
145
  if st.session_state.chat_active:
146
  with st.form(key="chat_form", clear_on_submit=True):
147
+ user_input = st.text_input("Ask a question about the documents (type 'exit' to stop):")
148
  submit = st.form_submit_button("Send")
149
 
150
  if submit and user_input:
 
163
  )
164
  content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
165
 
166
+ # Add all documents' text content
167
+ if st.session_state.documents_text.strip():
168
+ content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
169
 
170
  # Add image blocks (if image data exists)
171
  if "image_data" in st.session_state: