Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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 "
|
| 93 |
-
st.session_state.
|
| 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 |
-
|
| 100 |
-
"Upload
|
| 101 |
-
type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex", "jpg", "jpeg", "png"]
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 161 |
-
if st.session_state.
|
| 162 |
-
content_blocks.append({"text": f"Context:\n{st.session_state.
|
| 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:
|