File size: 8,135 Bytes
070969f 357d7d8 070969f 357d7d8 6cbca8b abfb305 357d7d8 d9a4424 357d7d8 6cbca8b d9a4424 6cbca8b abfb305 357d7d8 bd4bfed 357d7d8 d9a4424 357d7d8 5b28f0d 45efda5 cb47b15 357d7d8 563637e 357d7d8 d9a4424 cfd0b04 357d7d8 563637e abfb305 357d7d8 563637e d9a4424 563637e d9a4424 563637e cb47b15 d9a4424 cb47b15 563637e cb47b15 357d7d8 cb47b15 563637e cb47b15 d9a4424 cb47b15 5bb5a64 d9a4424 cb47b15 c4c7a20 cb47b15 c4c7a20 cb47b15 357d7d8 cb47b15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import streamlit as st
import os
import pandas as pd
import fitz # PyMuPDF
from google import genai
from google.genai import types
from docx import Document
from pptx import Presentation
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from bs4 import BeautifulSoup
import re
import base64
# Set Gemini API Key
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
# --------- File Extractors ---------
def extract_text_from_docx(docx_file):
document = Document(docx_file)
return "\n".join([para.text for para in document.paragraphs])
def extract_text_from_csv(csv_file):
df = pd.read_csv(csv_file)
return df.to_string(index=False)
def extract_text_from_xlsx(xlsx_file):
df = pd.read_excel(xlsx_file)
return df.to_string(index=False)
def extract_text_from_pptx(pptx_file):
prs = Presentation(pptx_file)
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
text_runs.append(shape.text)
return "\n".join(text_runs)
def extract_text_from_pdf(pdf_file):
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
text = ""
for page in doc:
text += page.get_text()
return text
def extract_text_from_html(html_file):
soup = BeautifulSoup(html_file.read(), "html.parser")
return soup.get_text()
def extract_text_from_tex(tex_file):
content = tex_file.read().decode("utf-8")
content = re.sub(r'\\[a-zA-Z]+\{[^}]*\}', '', content)
content = re.sub(r'\\[a-zA-Z]+', '', content)
return content
def process_image(image_file):
image_bytes = image_file.read()
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
return {
"inline_data": {
"mime_type": image_file.type,
"data": encoded_image
}
}
def export_conversation_to_pdf(conversation_history):
pdf_path = "Conversation.pdf"
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
styles = getSampleStyleSheet()
elements = []
for i, (user_q, gemini_a) in enumerate(conversation_history):
elements.append(Paragraph(f"<b>Q{i+1}:</b> {user_q}", styles["Normal"]))
elements.append(Spacer(1, 8))
elements.append(Paragraph(f"<b>A{i+1}:</b> {gemini_a.replace(chr(10), '<br/>')}", styles["Normal"]))
elements.append(Spacer(1, 16))
doc.build(elements)
return pdf_path
# --------- Main App ---------
def main():
st.set_page_config(page_title="Gemini 2.5 Pro Chat Q&A", layout="wide")
st.title("📄 Chat with Your Documents leveraging Internet Using Gemini 2.5 Pro (Team 18))")
if "conversation" not in st.session_state:
st.session_state.conversation = []
if "documents_text" not in st.session_state:
st.session_state.documents_text = []
if "chat_active" not in st.session_state:
st.session_state.chat_active = True
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
uploaded_files = st.file_uploader(
"Upload files (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png):",
type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex", "jpg", "jpeg", "png"],
accept_multiple_files=True
)
if uploaded_files:
all_text_content = ""
for uploaded_file in uploaded_files:
filetype = uploaded_file.name.split(".")[-1].lower()
try:
if filetype == "txt":
all_text_content += uploaded_file.read().decode("utf-8") + "\n"
elif filetype == "docx":
all_text_content += extract_text_from_docx(uploaded_file) + "\n"
elif filetype == "csv":
all_text_content += extract_text_from_csv(uploaded_file) + "\n"
elif filetype == "xlsx":
all_text_content += extract_text_from_xlsx(uploaded_file) + "\n"
elif filetype == "pptx":
all_text_content += extract_text_from_pptx(uploaded_file) + "\n"
elif filetype == "pdf":
all_text_content += extract_text_from_pdf(uploaded_file) + "\n"
elif filetype in ["html", "htm"]:
all_text_content += extract_text_from_html(uploaded_file) + "\n"
elif filetype == "tex":
all_text_content += extract_text_from_tex(uploaded_file) + "\n"
elif filetype in ["jpg", "jpeg", "png"]:
image_data = process_image(uploaded_file)
st.session_state.image_data = image_data
all_text_content += "Image uploaded and processed.\n"
st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
else:
st.error(f"Unsupported file format: {uploaded_file.name}")
except Exception as e:
st.error(f"Failed to extract text from {uploaded_file.name}: {e}")
st.session_state.documents_text = all_text_content
if st.session_state.documents_text:
st.markdown("### 💬 Conversation")
for user_q, gemini_a in st.session_state.conversation:
st.markdown(f"**You:** {user_q}")
st.markdown(f"**Gemini:**\n\n{gemini_a}")
if st.session_state.chat_active:
with st.form(key="chat_form", clear_on_submit=True):
user_input = st.text_input("Ask a question about the documents (type 'exit' to stop):")
submit = st.form_submit_button("Send")
if submit and user_input:
if user_input.strip().lower() == "exit":
st.session_state.chat_active = False
st.success("Chat ended. Reload to start again.")
else:
with st.spinner("Gemini is thinking..."):
content_blocks = []
if st.session_state.conversation:
history_text = "\n\n".join(
f"Q: {entry[0]}\nA: {entry[1]}"
for entry in st.session_state.conversation
)
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
if st.session_state.documents_text.strip():
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
if "image_data" in st.session_state:
content_blocks.append(st.session_state.image_data)
content_blocks.append({"text": f"Question: {user_input}"})
# ✅ Gemini with Internet Search
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=content_blocks,
config={"tools": [{"google_search": {}}]}
)
st.session_state.conversation.append((user_input, response.text))
st.success("💡 Answer:")
st.write(response.text)
st.session_state.chat_history.append({
"question": user_input,
"answer": response.text
})
if st.session_state.conversation:
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
with open(pdf_path, "rb") as f:
st.download_button(
label="📥 Export Conversation as PDF",
data=f,
file_name="Conversation.pdf",
mime="application/pdf"
)
if __name__ == "__main__":
main() |