Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import re
|
| 14 |
import base64
|
| 15 |
-
from streamlit.components.v1 import html
|
| 16 |
|
| 17 |
# Set Gemini API Key
|
| 18 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
@@ -100,87 +99,15 @@ def export_conversation_to_pdf(conversation_history):
|
|
| 100 |
doc.build(elements)
|
| 101 |
return pdf_path
|
| 102 |
|
| 103 |
-
# --------- New Media Capture Features ---------
|
| 104 |
-
def capture_photo():
|
| 105 |
-
# Allow the user to capture a photo
|
| 106 |
-
photo = st.camera_input("Capture Photo")
|
| 107 |
-
|
| 108 |
-
if photo is not None:
|
| 109 |
-
# Display the captured photo and process it
|
| 110 |
-
st.image(photo, caption="Captured Photo", use_container_width=True)
|
| 111 |
-
|
| 112 |
-
# Process the captured photo for Gemini analysis (image processing)
|
| 113 |
-
st.session_state.image_data = process_image(photo)
|
| 114 |
-
st.session_state.documents_text += "Captured Image added to context.\n"
|
| 115 |
-
|
| 116 |
-
# Notify the user and process it with Gemini
|
| 117 |
-
st.success("Photo Captured and Ready for Analysis!")
|
| 118 |
-
|
| 119 |
-
def capture_video():
|
| 120 |
-
# HTML and JS for recording video using the browser
|
| 121 |
-
video_recorder_html = """
|
| 122 |
-
<html>
|
| 123 |
-
<body>
|
| 124 |
-
<h2>Click to Record a Video</h2>
|
| 125 |
-
<video id="video" width="320" height="240" autoplay muted></video>
|
| 126 |
-
<br/>
|
| 127 |
-
<button id="startStopBtn">Start Recording</button>
|
| 128 |
-
<br/><br/>
|
| 129 |
-
<a id="downloadLink" style="display:none" download="captured_video.mp4">
|
| 130 |
-
<button>Download Recorded Video</button>
|
| 131 |
-
</a>
|
| 132 |
-
<script>
|
| 133 |
-
const videoElement = document.getElementById("video");
|
| 134 |
-
const startStopBtn = document.getElementById("startStopBtn");
|
| 135 |
-
const downloadLink = document.getElementById("downloadLink");
|
| 136 |
-
|
| 137 |
-
let mediaRecorder;
|
| 138 |
-
let recordedChunks = [];
|
| 139 |
-
|
| 140 |
-
// Set up video streaming from the webcam
|
| 141 |
-
navigator.mediaDevices.getUserMedia({ video: true })
|
| 142 |
-
.then(function (stream) {
|
| 143 |
-
videoElement.srcObject = stream;
|
| 144 |
-
mediaRecorder = new MediaRecorder(stream);
|
| 145 |
-
|
| 146 |
-
mediaRecorder.ondataavailable = function (e) {
|
| 147 |
-
recordedChunks.push(e.data);
|
| 148 |
-
};
|
| 149 |
-
|
| 150 |
-
mediaRecorder.onstop = function () {
|
| 151 |
-
const blob = new Blob(recordedChunks, { type: "video/mp4" });
|
| 152 |
-
const url = URL.createObjectURL(blob);
|
| 153 |
-
downloadLink.href = url;
|
| 154 |
-
downloadLink.style.display = 'block';
|
| 155 |
-
};
|
| 156 |
-
});
|
| 157 |
-
|
| 158 |
-
startStopBtn.onclick = function () {
|
| 159 |
-
if (mediaRecorder.state === "inactive") {
|
| 160 |
-
mediaRecorder.start();
|
| 161 |
-
startStopBtn.innerText = "Stop Recording";
|
| 162 |
-
} else {
|
| 163 |
-
mediaRecorder.stop();
|
| 164 |
-
startStopBtn.innerText = "Start Recording";
|
| 165 |
-
}
|
| 166 |
-
};
|
| 167 |
-
</script>
|
| 168 |
-
</body>
|
| 169 |
-
</html>
|
| 170 |
-
"""
|
| 171 |
-
html(video_recorder_html, height=400)
|
| 172 |
-
|
| 173 |
-
# --------- Main App ---------
|
| 174 |
# --------- Main App ---------
|
| 175 |
def main():
|
| 176 |
st.set_page_config(page_title="Gemini 2.0 Flash Chat Q&A", layout="wide")
|
| 177 |
st.title("\U0001F4C4 Team 18 Comprehensive Prototype Using Gemini 2.0 Flash")
|
| 178 |
|
| 179 |
-
# Initialize session state variables
|
| 180 |
if "conversation" not in st.session_state:
|
| 181 |
st.session_state.conversation = []
|
| 182 |
if "documents_text" not in st.session_state:
|
| 183 |
-
st.session_state.documents_text =
|
| 184 |
if "chat_active" not in st.session_state:
|
| 185 |
st.session_state.chat_active = True
|
| 186 |
if "chat_history" not in st.session_state:
|
|
@@ -196,7 +123,6 @@ def main():
|
|
| 196 |
accept_multiple_files=True
|
| 197 |
)
|
| 198 |
|
| 199 |
-
# Handle the files uploaded
|
| 200 |
if uploaded_files:
|
| 201 |
all_text_content = ""
|
| 202 |
for uploaded_file in uploaded_files:
|
|
@@ -231,22 +157,8 @@ def main():
|
|
| 231 |
except Exception as e:
|
| 232 |
st.error(f"Failed to extract/process {uploaded_file.name}: {e}")
|
| 233 |
|
| 234 |
-
st.session_state.documents_text
|
| 235 |
-
|
| 236 |
-
# Media Capture Section
|
| 237 |
-
st.sidebar.header("Capture Media")
|
| 238 |
-
capture_option = st.sidebar.radio("Select Media Capture Option", ["None", "Capture Photo", "Capture Video"])
|
| 239 |
-
|
| 240 |
-
if capture_option == "Capture Photo":
|
| 241 |
-
st.session_state.image_data = None
|
| 242 |
-
st.session_state.video_data = None
|
| 243 |
-
capture_photo()
|
| 244 |
-
elif capture_option == "Capture Video":
|
| 245 |
-
st.session_state.image_data = None
|
| 246 |
-
st.session_state.video_data = None
|
| 247 |
-
capture_video()
|
| 248 |
|
| 249 |
-
# Display conversation and chat
|
| 250 |
if st.session_state.documents_text:
|
| 251 |
st.markdown("### \U0001F4AC Conversation")
|
| 252 |
for user_q, gemini_a in st.session_state.conversation:
|
|
@@ -266,54 +178,52 @@ def main():
|
|
| 266 |
with st.spinner("Let me think..."):
|
| 267 |
content_blocks = []
|
| 268 |
|
|
|
|
| 269 |
content_blocks.append({
|
| 270 |
"text": (
|
| 271 |
"Your role is to analyze documents, images, and videos to help identify and classify driver states, "
|
| 272 |
"including: drowsy, alert, yawning, microsleep, eyes/lips open or closed, and other distractions. "
|
| 273 |
"The user may ask for timestamps of events in video, technical explanations from code/docs, "
|
| 274 |
"or structured summaries. Prioritize your answers for this use case and act as a helpful assistant "
|
| 275 |
-
"within this domain."
|
|
|
|
|
|
|
|
|
|
| 276 |
)
|
| 277 |
})
|
| 278 |
|
| 279 |
if st.session_state.conversation:
|
| 280 |
history_text = "\n\n".join(
|
| 281 |
-
f"Q: {entry[0]}\nA: {entry[1]}"
|
|
|
|
| 282 |
)
|
| 283 |
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 284 |
|
| 285 |
if st.session_state.documents_text.strip():
|
| 286 |
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
content_blocks.append({"image": st.session_state.image_data})
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
content_blocks.append({"video": st.session_state.video_data})
|
| 295 |
|
| 296 |
content_blocks.append({"text": f"Question: {user_input}"})
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
config={"tools": [{"google_search": {}}]}
|
| 304 |
-
)
|
| 305 |
-
|
| 306 |
-
st.session_state.conversation.append((user_input, response.text))
|
| 307 |
-
st.success("\U0001F4A1 Answer:")
|
| 308 |
-
st.write(response.text)
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
})
|
| 314 |
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
| 317 |
|
| 318 |
if st.session_state.conversation:
|
| 319 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|
|
@@ -326,6 +236,4 @@ def main():
|
|
| 326 |
)
|
| 327 |
|
| 328 |
if __name__ == "__main__":
|
| 329 |
-
main()
|
| 330 |
-
|
| 331 |
-
|
|
|
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import re
|
| 14 |
import base64
|
|
|
|
| 15 |
|
| 16 |
# Set Gemini API Key
|
| 17 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
|
|
| 99 |
doc.build(elements)
|
| 100 |
return pdf_path
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# --------- Main App ---------
|
| 103 |
def main():
|
| 104 |
st.set_page_config(page_title="Gemini 2.0 Flash Chat Q&A", layout="wide")
|
| 105 |
st.title("\U0001F4C4 Team 18 Comprehensive Prototype Using Gemini 2.0 Flash")
|
| 106 |
|
|
|
|
| 107 |
if "conversation" not in st.session_state:
|
| 108 |
st.session_state.conversation = []
|
| 109 |
if "documents_text" not in st.session_state:
|
| 110 |
+
st.session_state.documents_text = []
|
| 111 |
if "chat_active" not in st.session_state:
|
| 112 |
st.session_state.chat_active = True
|
| 113 |
if "chat_history" not in st.session_state:
|
|
|
|
| 123 |
accept_multiple_files=True
|
| 124 |
)
|
| 125 |
|
|
|
|
| 126 |
if uploaded_files:
|
| 127 |
all_text_content = ""
|
| 128 |
for uploaded_file in uploaded_files:
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
st.error(f"Failed to extract/process {uploaded_file.name}: {e}")
|
| 159 |
|
| 160 |
+
st.session_state.documents_text = all_text_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
|
|
|
| 162 |
if st.session_state.documents_text:
|
| 163 |
st.markdown("### \U0001F4AC Conversation")
|
| 164 |
for user_q, gemini_a in st.session_state.conversation:
|
|
|
|
| 178 |
with st.spinner("Let me think..."):
|
| 179 |
content_blocks = []
|
| 180 |
|
| 181 |
+
# System prompt inserted first here:
|
| 182 |
content_blocks.append({
|
| 183 |
"text": (
|
| 184 |
"Your role is to analyze documents, images, and videos to help identify and classify driver states, "
|
| 185 |
"including: drowsy, alert, yawning, microsleep, eyes/lips open or closed, and other distractions. "
|
| 186 |
"The user may ask for timestamps of events in video, technical explanations from code/docs, "
|
| 187 |
"or structured summaries. Prioritize your answers for this use case and act as a helpful assistant "
|
| 188 |
+
"within this domain. "
|
| 189 |
+
"Additionally, your classification may rely on the State Farm Distracted Driver Detection dataset, "
|
| 190 |
+
"which defines 10 distraction categories such as texting, phone use, reaching behind, adjusting the radio, "
|
| 191 |
+
"and safe driving. Use this labeling scheme when interpreting relevant images."
|
| 192 |
)
|
| 193 |
})
|
| 194 |
|
| 195 |
if st.session_state.conversation:
|
| 196 |
history_text = "\n\n".join(
|
| 197 |
+
f"Q: {entry[0]}\nA: {entry[1]}"
|
| 198 |
+
for entry in st.session_state.conversation
|
| 199 |
)
|
| 200 |
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 201 |
|
| 202 |
if st.session_state.documents_text.strip():
|
| 203 |
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
|
| 204 |
|
| 205 |
+
if "image_data" in st.session_state:
|
| 206 |
+
content_blocks.append(st.session_state.image_data)
|
|
|
|
| 207 |
|
| 208 |
+
if "video_data" in st.session_state:
|
| 209 |
+
content_blocks.append(st.session_state.video_data)
|
|
|
|
| 210 |
|
| 211 |
content_blocks.append({"text": f"Question: {user_input}"})
|
| 212 |
|
| 213 |
+
response = client.models.generate_content(
|
| 214 |
+
model="gemini-2.0-flash",
|
| 215 |
+
contents=content_blocks,
|
| 216 |
+
config={"tools": [{"google_search": {}}]}
|
| 217 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
st.session_state.conversation.append((user_input, response.text))
|
| 220 |
+
st.success("\U0001F4A1 Answer:")
|
| 221 |
+
st.write(response.text)
|
|
|
|
| 222 |
|
| 223 |
+
st.session_state.chat_history.append({
|
| 224 |
+
"question": user_input,
|
| 225 |
+
"answer": response.text
|
| 226 |
+
})
|
| 227 |
|
| 228 |
if st.session_state.conversation:
|
| 229 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|
|
|
|
| 236 |
)
|
| 237 |
|
| 238 |
if __name__ == "__main__":
|
| 239 |
+
main()
|
|
|
|
|
|