File size: 10,392 Bytes
070969f
357d7d8
 
 
 
 
070969f
 
357d7d8
 
 
6cbca8b
 
abfb305
357d7d8
 
 
 
d9a4424
274af9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432f223
 
 
 
 
 
 
 
 
 
274af9b
 
 
 
 
 
 
 
432f223
274af9b
 
 
 
 
 
 
 
 
3676f8c
432f223
 
 
 
 
 
 
 
 
 
 
 
 
 
357d7d8
d9a4424
357d7d8
1e218ae
54f5094
3875b2a
 
 
 
a254664
3875b2a
 
 
 
357d7d8
7631a4c
 
 
 
 
 
 
 
abfb305
357d7d8
7631a4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a254664
8d61217
7631a4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a254664
7631a4c
 
 
 
 
 
a254664
 
 
 
7631a4c
 
 
 
 
a254664
 
3875b2a
7631a4c
 
 
 
 
a254664
 
7631a4c
a254664
 
7631a4c
 
 
a254664
1e218ae
a254664
 
 
cb47b15
a254664
 
 
5c52538
a254664
 
 
 
3875b2a
 
 
 
 
 
 
 
 
 
357d7d8
 
a254664
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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 process_video(video_file):
    video_bytes = video_file.read()
    filetype = video_file.name.split(".")[-1].lower()
    mime_map = {
        "avi": "video/x-msvideo",
        "mkv": "video/x-matroska",
        "flv": "video/x-flv",
        "wmv": "video/x-ms-wmv",
        "mpeg": "video/mpeg"
    }
    mime_type = mime_map.get(filetype, video_file.type)
    encoded_video = base64.b64encode(video_bytes).decode("utf-8")
    return {
        "inline_data": {
            "mime_type": mime_type,
            "data": encoded_video
        }
    }

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 Flash Chat Q&A", layout="wide")
    st.title("\U0001F4C4 Product Prototype with Document Support and Internet Search")

    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, video formats):",
        type=[
            "txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex",
            "jpg", "jpeg", "png",
            "mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"
        ],
        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"]:
                    st.session_state.image_data = process_image(uploaded_file)
                    all_text_content += "Image uploaded and processed.\n"
                    st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
                elif filetype in ["mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"]:
                    st.session_state.video_data = process_video(uploaded_file)
                    all_text_content += f"Video file '{uploaded_file.name}' uploaded and processed.\n"
                    st.video(uploaded_file)
                else:
                    st.error(f"Unsupported file format: {uploaded_file.name}")
            except Exception as e:
                st.error(f"Failed to extract/process {uploaded_file.name}: {e}")

        st.session_state.documents_text = all_text_content

    if st.session_state.documents_text:
        st.markdown("### \U0001F4AC 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("Let me think..."):
                            content_blocks = []

                            # System prompt inserted first here:
                            content_blocks.append({
                                "text": (
                                    "Your role is to analyze documents, images, and videos to help identify and classify driver states, "
                                    "including: drowsy, alert, yawning, microsleep, eyes/lips open or closed, and other distractions. "
                                    "The user may ask for timestamps of events in video, technical explanations from code/docs, "
                                    "or structured summaries. Prioritize your answers for this use case and act as a helpful assistant "
                                    "within this domain. "
                                    "Additionally, your classification may rely on the State Farm Distracted Driver Detection dataset, "
                                    "which defines 10 distraction categories such as texting, phone use, reaching behind, adjusting the radio, "
                                    "and safe driving. Use this labeling scheme when interpreting relevant images."
                                )
                            })

                            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)

                            if "video_data" in st.session_state:
                                content_blocks.append(st.session_state.video_data)

                            content_blocks.append({"text": f"Question: {user_input}"})

                            response = client.models.generate_content(
                                model="gemini-2.5-flash",
                                contents=content_blocks,
                                config={"tools": [{"google_search": {}}]}
                            )

                            st.session_state.conversation.append((user_input, response.text))
                            st.success("\U0001F4A1 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="\U0001F4E5 Export Conversation as PDF",
                    data=f,
                    file_name="Conversation.pdf",
                    mime="application/pdf"
                )

if __name__ == "__main__":
    main()