Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 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,6 +100,76 @@ def export_conversation_to_pdf(conversation_history):
|
|
| 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")
|
|
@@ -113,117 +184,76 @@ def main():
|
|
| 113 |
if "chat_history" not in st.session_state:
|
| 114 |
st.session_state.chat_history = []
|
| 115 |
|
| 116 |
-
|
| 117 |
-
"
|
| 118 |
-
|
| 119 |
-
"txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex",
|
| 120 |
-
"jpg", "jpeg", "png",
|
| 121 |
-
"mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"
|
| 122 |
-
],
|
| 123 |
-
accept_multiple_files=True
|
| 124 |
)
|
| 125 |
|
| 126 |
-
if
|
| 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 |
-
# 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 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
if st.session_state.conversation:
|
| 229 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|
|
|
|
| 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 |
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 |
def main():
|
| 175 |
st.set_page_config(page_title="Gemini 2.0 Flash Chat Q&A", layout="wide")
|
|
|
|
| 184 |
if "chat_history" not in st.session_state:
|
| 185 |
st.session_state.chat_history = []
|
| 186 |
|
| 187 |
+
action = st.selectbox(
|
| 188 |
+
"Choose Action",
|
| 189 |
+
["Ask a Question", "Capture Photo", "Capture Video"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
+
if action == "Capture Photo":
|
| 193 |
+
capture_photo()
|
| 194 |
+
|
| 195 |
+
elif action == "Capture Video":
|
| 196 |
+
capture_video()
|
| 197 |
+
|
| 198 |
+
elif action == "Ask a Question":
|
| 199 |
+
if st.session_state.documents_text:
|
| 200 |
+
st.markdown("### \U0001F4AC Conversation")
|
| 201 |
+
for user_q, gemini_a in st.session_state.conversation:
|
| 202 |
+
st.markdown(f"**You:** {user_q}")
|
| 203 |
+
st.markdown(f"**Gemini:**\n\n{gemini_a}")
|
| 204 |
+
|
| 205 |
+
if st.session_state.chat_active:
|
| 206 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
| 207 |
+
user_input = st.text_input("Ask a question about the documents (type 'exit' to stop):")
|
| 208 |
+
submit = st.form_submit_button("Send")
|
| 209 |
+
|
| 210 |
+
if submit and user_input:
|
| 211 |
+
if user_input.strip().lower() == "exit":
|
| 212 |
+
st.session_state.chat_active = False
|
| 213 |
+
st.success("Chat ended. Reload to start again.")
|
| 214 |
+
else:
|
| 215 |
+
with st.spinner("Let me think..."):
|
| 216 |
+
content_blocks = []
|
| 217 |
+
|
| 218 |
+
# System prompt for context
|
| 219 |
+
content_blocks.append({
|
| 220 |
+
"text": (
|
| 221 |
+
"Your role is to analyze documents, images, and videos to help identify and classify driver states..."
|
| 222 |
+
)
|
| 223 |
+
})
|
| 224 |
+
|
| 225 |
+
if st.session_state.conversation:
|
| 226 |
+
history_text = "\n\n".join(
|
| 227 |
+
f"Q: {entry[0]}\nA: {entry[1]}"
|
| 228 |
+
for entry in st.session_state.conversation
|
| 229 |
+
)
|
| 230 |
+
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 231 |
+
|
| 232 |
+
if st.session_state.documents_text.strip():
|
| 233 |
+
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
|
| 234 |
+
|
| 235 |
+
if "image_data" in st.session_state:
|
| 236 |
+
content_blocks.append(st.session_state.image_data)
|
| 237 |
+
|
| 238 |
+
if "video_data" in st.session_state:
|
| 239 |
+
content_blocks.append(st.session_state.video_data)
|
| 240 |
+
|
| 241 |
+
content_blocks.append({"text": f"Question: {user_input}"})
|
| 242 |
+
|
| 243 |
+
response = client.models.generate_content(
|
| 244 |
+
model="gemini-2.0-flash",
|
| 245 |
+
contents=content_blocks,
|
| 246 |
+
config={"tools": [{"google_search": {}}]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
+
st.session_state.conversation.append((user_input, response.text))
|
| 250 |
+
st.success("\U0001F4A1 Answer:")
|
| 251 |
+
st.write(response.text)
|
| 252 |
|
| 253 |
+
st.session_state.chat_history.append({
|
| 254 |
+
"question": user_input,
|
| 255 |
+
"answer": response.text
|
| 256 |
+
})
|
| 257 |
|
| 258 |
if st.session_state.conversation:
|
| 259 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|