Spaces:
Runtime error
Runtime error
Update sherlock2.py
Browse files- sherlock2.py +72 -98
sherlock2.py
CHANGED
@@ -257,91 +257,77 @@ def summarize_chat_history(chat_history):
|
|
257 |
|
258 |
|
259 |
def investigate():
|
260 |
-
"""Handles the case investigation process
|
261 |
-
image processing, information analysis using Gemini models, web/Wikipedia search, and case report generation.
|
262 |
-
"""
|
263 |
st.subheader("Case Investigation")
|
264 |
|
265 |
-
# File upload
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
if
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
|
|
|
|
284 |
combined_information = {
|
285 |
"case_text": case_text,
|
286 |
"case_embeddings": case_embeddings,
|
287 |
"image_insights": image_insights,
|
288 |
"keywords": keywords
|
289 |
}
|
290 |
-
|
291 |
-
# Analyze combined information using Gemini 1.5 Pro
|
292 |
prompt = """
|
293 |
You are Sherlock Holmes, the renowned detective. Analyze the following case information and provide insights or
|
294 |
suggestions for further investigation:
|
295 |
""" + str(combined_information)
|
296 |
-
|
297 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, prompt, *case_embeddings])
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
st.
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
# Render user input box
|
335 |
-
user_query = st.text_input("Ask Sherlock:", key="user_input")
|
336 |
-
if user_query:
|
337 |
-
chat_history_summary = summarize_chat_history(st.session_state.chat_history)
|
338 |
-
conversation_history = [sherlock_persona, sherlock_guidelines, chat_history_summary, f"Human: {user_query}"]
|
339 |
-
response = model.generate_content(conversation_history)
|
340 |
-
st.session_state.chat_history.append((user_query, response.text)) # Add chat to history
|
341 |
-
display_chat_history() # Display chat history
|
342 |
-
|
343 |
-
# Call the fixInputToBottom function after rendering the input text box
|
344 |
-
st.markdown("<script>fixInputToBottom();</script>", unsafe_allow_html=True)
|
345 |
|
346 |
|
347 |
def main():
|
@@ -351,28 +337,7 @@ def main():
|
|
351 |
# Custom CSS for Styling
|
352 |
vintage_css = """
|
353 |
<style>
|
354 |
-
|
355 |
-
background-color: #d2b48c; /* Antique White */
|
356 |
-
color: #332200; /* Dark Brown */
|
357 |
-
font-family: 'Times New Roman', serif;
|
358 |
-
}
|
359 |
-
h1, h2, h3 {
|
360 |
-
color: #8b4513; /* Saddle Brown */
|
361 |
-
}
|
362 |
-
.stTextInput > div > div > input {
|
363 |
-
border: 1px solid #8b4513;
|
364 |
-
border-radius: 5px;
|
365 |
-
padding: 10px;
|
366 |
-
font-size: 16px;
|
367 |
-
width: 100%;
|
368 |
-
box-sizing: border-box;
|
369 |
-
}
|
370 |
-
.stButton > button {
|
371 |
-
background-color: #8b4513;
|
372 |
-
color: white;
|
373 |
-
border: none;
|
374 |
-
border-radius: 5px;
|
375 |
-
}
|
376 |
</style>
|
377 |
"""
|
378 |
st.markdown(vintage_css, unsafe_allow_html=True) # Apply custom CSS
|
@@ -391,12 +356,21 @@ def main():
|
|
391 |
st.info(f"**You:** {user_msg}")
|
392 |
st.success(f"**Sherlock:** {bot_msg}")
|
393 |
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
# Call the investigate function to handle the case investigation process
|
398 |
investigate()
|
399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
# Call the fixInputToBottom function after rendering the input text box
|
401 |
st.markdown("<script>fixInputToBottom();</script>", unsafe_allow_html=True)
|
402 |
|
|
|
257 |
|
258 |
|
259 |
def investigate():
|
260 |
+
"""Handles the case investigation process with improved UI and functionality."""
|
|
|
|
|
261 |
st.subheader("Case Investigation")
|
262 |
|
263 |
+
# File upload with clear labels and progress bars
|
264 |
+
documents = st.file_uploader("Upload Case Documents (txt, pdf, docx)", accept_multiple_files=True, type=["txt", "pdf", "docx"], key="docs")
|
265 |
+
images = st.file_uploader("Upload Case Images (jpg, png, jpeg)", accept_multiple_files=True, type=["jpg", "png", "jpeg"], key="imgs")
|
266 |
+
|
267 |
+
if documents and images:
|
268 |
+
# Display file names and processing status
|
269 |
+
st.write("**Uploaded Documents:**")
|
270 |
+
for doc in documents:
|
271 |
+
st.write(f"- {doc.name}")
|
272 |
+
st.write("**Uploaded Images:**")
|
273 |
+
for img in images:
|
274 |
+
st.write(f"- {img.name}")
|
275 |
+
|
276 |
+
# Extract text and process images with progress indication
|
277 |
+
with st.spinner("Extracting text and analyzing images..."):
|
278 |
+
case_text = extract_text_from_files(documents)
|
279 |
+
keywords = extract_keywords_simple("\n\n".join(case_text))
|
280 |
+
case_embeddings = generate_embeddings_from_documents(case_text)
|
281 |
+
image_insights = process_images(images)
|
282 |
+
|
283 |
+
# Combine information and analyze
|
284 |
combined_information = {
|
285 |
"case_text": case_text,
|
286 |
"case_embeddings": case_embeddings,
|
287 |
"image_insights": image_insights,
|
288 |
"keywords": keywords
|
289 |
}
|
|
|
|
|
290 |
prompt = """
|
291 |
You are Sherlock Holmes, the renowned detective. Analyze the following case information and provide insights or
|
292 |
suggestions for further investigation:
|
293 |
""" + str(combined_information)
|
|
|
294 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, prompt, *case_embeddings])
|
295 |
+
|
296 |
+
# Display results in an expandable section
|
297 |
+
with st.expander("Sherlock's Analysis and Suggestions:"):
|
298 |
+
st.write(response.text)
|
299 |
+
|
300 |
+
# Search options with clear buttons and feedback
|
301 |
+
search_options = st.multiselect("Search for additional clues:", ["Wikipedia", "Internet"], default=["Wikipedia"])
|
302 |
+
if st.button("Search"):
|
303 |
+
with st.spinner("Searching for clues..."):
|
304 |
+
if "Wikipedia" in search_options:
|
305 |
+
wikipedia_info = search_and_scrape_wikipedia(keywords)
|
306 |
+
st.subheader("Wikipedia Findings:")
|
307 |
+
for info in wikipedia_info:
|
308 |
+
st.write(f"**Topic:** {info['topic']}")
|
309 |
+
st.write(f"**Summary:** {info['summary']}")
|
310 |
+
st.write(f"**URL:** {info['url']}")
|
311 |
+
if "Internet" in search_options:
|
312 |
+
web_search_results = search_internet("\n\n".join(case_text))
|
313 |
+
st.subheader("Internet Search Results:")
|
314 |
+
for result in web_search_results:
|
315 |
+
st.write(f"**Title:** {result['title']}")
|
316 |
+
st.write(f"**Snippet:** {result['snippet']}")
|
317 |
+
st.write(f"**URL:** {result['url']}")
|
318 |
+
|
319 |
+
# Generate report button
|
320 |
+
if st.button("Generate Case Report"):
|
321 |
+
with st.spinner("Generating report..."):
|
322 |
+
report_prompt = """
|
323 |
+
You are Sherlock Holmes, the renowned detective. Based on the case information, your analysis, findings from
|
324 |
+
Wikipedia and the web, and the extracted keywords, generate a comprehensive case report in your signature style,
|
325 |
+
including deductions, potential suspects, and conclusions.
|
326 |
+
"""
|
327 |
+
final_report = model.generate_content([sherlock_persona, sherlock_guidelines, report_prompt,
|
328 |
+
*case_embeddings, str(wikipedia_info), str(web_search_results)])
|
329 |
+
st.header("Case Report")
|
330 |
+
st.write(final_report.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
|
332 |
|
333 |
def main():
|
|
|
337 |
# Custom CSS for Styling
|
338 |
vintage_css = """
|
339 |
<style>
|
340 |
+
... (CSS code remains the same) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
341 |
</style>
|
342 |
"""
|
343 |
st.markdown(vintage_css, unsafe_allow_html=True) # Apply custom CSS
|
|
|
356 |
st.info(f"**You:** {user_msg}")
|
357 |
st.success(f"**Sherlock:** {bot_msg}")
|
358 |
|
359 |
+
st.sidebar.title("Case Files")
|
360 |
+
st.sidebar.info("Upload your case files and let Sherlock analyze the evidence.")
|
361 |
+
|
|
|
362 |
investigate()
|
363 |
|
364 |
+
# Chat with Sherlock
|
365 |
+
st.sidebar.title("Consult with Sherlock")
|
366 |
+
user_query = st.sidebar.text_input("Ask Sherlock:", key="user_input")
|
367 |
+
if user_query:
|
368 |
+
chat_history_summary = summarize_chat_history(st.session_state.chat_history)
|
369 |
+
conversation_history = [sherlock_persona, sherlock_guidelines, chat_history_summary, f"Human: {user_query}"]
|
370 |
+
response = model.generate_content(conversation_history)
|
371 |
+
st.session_state.chat_history.append((user_query, response.text))
|
372 |
+
display_chat_history()
|
373 |
+
|
374 |
# Call the fixInputToBottom function after rendering the input text box
|
375 |
st.markdown("<script>fixInputToBottom();</script>", unsafe_allow_html=True)
|
376 |
|