Spaces:
Runtime error
Runtime error
Update sherlock2.py
Browse files- sherlock2.py +23 -61
sherlock2.py
CHANGED
@@ -232,45 +232,14 @@ def investigate():
|
|
232 |
"""Handles the case investigation process, including file upload, text extraction, embedding generation,
|
233 |
image processing, information analysis using Gemini models, web/Wikipedia search, and case report generation.
|
234 |
"""
|
235 |
-
st.
|
236 |
|
237 |
# File upload for documents and images
|
238 |
uploaded_documents = st.file_uploader("Upload Case Documents", accept_multiple_files=True, type=["txt", "pdf", "docx"])
|
239 |
uploaded_images = st.file_uploader("Upload Case Images", accept_multiple_files=True, type=["jpg", "png", "jpeg"])
|
240 |
|
241 |
if uploaded_documents and uploaded_images and st.button("Analyze Case"):
|
242 |
-
#
|
243 |
-
case_text = extract_text_from_files(uploaded_documents)
|
244 |
-
|
245 |
-
# Extract keywords and important information from the text
|
246 |
-
keywords = extract_keywords_simple("\n\n".join(case_text))
|
247 |
-
|
248 |
-
# Generate embeddings for the extracted text
|
249 |
-
case_embeddings = generate_embeddings_from_documents(case_text)
|
250 |
-
|
251 |
-
# Process images using Gemini Pro Vision
|
252 |
-
image_insights = process_images(uploaded_images)
|
253 |
-
|
254 |
-
# Combine text, image, and keyword information
|
255 |
-
combined_information = {
|
256 |
-
"case_text": case_text,
|
257 |
-
"case_embeddings": case_embeddings,
|
258 |
-
"image_insights": image_insights,
|
259 |
-
"keywords": keywords
|
260 |
-
}
|
261 |
-
|
262 |
-
# Analyze combined information using Gemini 1.5 Pro
|
263 |
-
prompt = """
|
264 |
-
You are Sherlock Holmes, the renowned detective. Analyze the following case information and provide insights or
|
265 |
-
suggestions for further investigation:
|
266 |
-
""" + str(combined_information)
|
267 |
-
|
268 |
-
response = model.generate_content([sherlock_persona, sherlock_guidelines, prompt, *case_embeddings])
|
269 |
-
st.write(response.text)
|
270 |
-
|
271 |
-
# Search Wikipedia and the web for related information
|
272 |
-
wikipedia_info = search_and_scrape_wikipedia(keywords)
|
273 |
-
web_search_results = search_internet("\n\n".join(case_text)) # Search the web
|
274 |
|
275 |
# Generate a case report in Sherlock Holmes's style
|
276 |
report_prompt = """
|
@@ -284,7 +253,9 @@ def investigate():
|
|
284 |
st.header("Case Report")
|
285 |
st.write(final_report.text)
|
286 |
|
287 |
-
|
|
|
|
|
288 |
if user_query:
|
289 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
290 |
st.write(response.text)
|
@@ -297,9 +268,9 @@ def investigate():
|
|
297 |
else:
|
298 |
st.info("Please upload both case documents and images to proceed with the investigation.")
|
299 |
|
300 |
-
#
|
301 |
-
|
302 |
-
user_query = st.text_input("Ask Sherlock:")
|
303 |
if user_query:
|
304 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
305 |
st.write(response.text)
|
@@ -342,30 +313,20 @@ def main():
|
|
342 |
"""
|
343 |
st.markdown(vintage_css, unsafe_allow_html=True) # Apply custom CSS
|
344 |
|
345 |
-
# JavaScript function to fix the input text box to the bottom of the screen
|
346 |
-
st.markdown(
|
347 |
-
"""
|
348 |
-
<script>
|
349 |
-
function fixInputToTop() {
|
350 |
-
const inputContainer = document.querySelector('.stTextInput');
|
351 |
-
if (inputContainer) {
|
352 |
-
inputContainer.style.position = 'sticky';
|
353 |
-
inputContainer.style.top = '20px';
|
354 |
-
inputContainer.style.left = '20px';
|
355 |
-
inputContainer.style.right = '20px';
|
356 |
-
inputContainer.style.zIndex = '999';
|
357 |
-
}
|
358 |
-
}
|
359 |
-
window.addEventListener('load', fixInputToTop);
|
360 |
-
</script>
|
361 |
-
""",
|
362 |
-
unsafe_allow_html=True
|
363 |
-
)
|
364 |
-
|
365 |
# Title and Header
|
366 |
st.title("AI Detective Sherlock Holmes")
|
367 |
st.header("_'Elementary, my dear Watson!'_")
|
368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
369 |
# Display chat history
|
370 |
display_chat_history()
|
371 |
|
@@ -378,16 +339,17 @@ def main():
|
|
378 |
investigate()
|
379 |
else:
|
380 |
# Chat with Sherlock Holmes (Gemini 1.5 Pro)
|
381 |
-
st.
|
382 |
-
user_query = st.text_input("Ask Sherlock:")
|
383 |
if user_query:
|
384 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
385 |
st.write(response.text)
|
386 |
st.session_state.chat_history.append((user_query, response.text)) # Add chat to history
|
387 |
display_chat_history() # Display chat history
|
388 |
|
389 |
-
|
390 |
-
|
|
|
391 |
|
392 |
if __name__ == "__main__":
|
393 |
main()
|
|
|
232 |
"""Handles the case investigation process, including file upload, text extraction, embedding generation,
|
233 |
image processing, information analysis using Gemini models, web/Wikipedia search, and case report generation.
|
234 |
"""
|
235 |
+
st.subheader("Case Investigation")
|
236 |
|
237 |
# File upload for documents and images
|
238 |
uploaded_documents = st.file_uploader("Upload Case Documents", accept_multiple_files=True, type=["txt", "pdf", "docx"])
|
239 |
uploaded_images = st.file_uploader("Upload Case Images", accept_multiple_files=True, type=["jpg", "png", "jpeg"])
|
240 |
|
241 |
if uploaded_documents and uploaded_images and st.button("Analyze Case"):
|
242 |
+
# ... (keep the existing code for case investigation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
# Generate a case report in Sherlock Holmes's style
|
245 |
report_prompt = """
|
|
|
253 |
st.header("Case Report")
|
254 |
st.write(final_report.text)
|
255 |
|
256 |
+
# Display chat history and allow user input
|
257 |
+
display_chat_history()
|
258 |
+
user_query = st.text_input("Ask Sherlock:", key="user_input")
|
259 |
if user_query:
|
260 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
261 |
st.write(response.text)
|
|
|
268 |
else:
|
269 |
st.info("Please upload both case documents and images to proceed with the investigation.")
|
270 |
|
271 |
+
# Display chat history and allow user input
|
272 |
+
display_chat_history()
|
273 |
+
user_query = st.text_input("Ask Sherlock:", key="user_input")
|
274 |
if user_query:
|
275 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
276 |
st.write(response.text)
|
|
|
313 |
"""
|
314 |
st.markdown(vintage_css, unsafe_allow_html=True) # Apply custom CSS
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
# Title and Header
|
317 |
st.title("AI Detective Sherlock Holmes")
|
318 |
st.header("_'Elementary, my dear Watson!'_")
|
319 |
|
320 |
+
# Initialize chat history
|
321 |
+
if 'chat_history' not in st.session_state:
|
322 |
+
st.session_state.chat_history = []
|
323 |
+
|
324 |
+
# Function to display chat history with highlighted user input and chatbot response
|
325 |
+
def display_chat_history():
|
326 |
+
for user_msg, bot_msg in st.session_state.chat_history:
|
327 |
+
st.info(f"**You:** {user_msg}")
|
328 |
+
st.success(f"**Sherlock:** {bot_msg}")
|
329 |
+
|
330 |
# Display chat history
|
331 |
display_chat_history()
|
332 |
|
|
|
339 |
investigate()
|
340 |
else:
|
341 |
# Chat with Sherlock Holmes (Gemini 1.5 Pro)
|
342 |
+
st.subheader("Chat with Sherlock Holmes")
|
343 |
+
user_query = st.text_input("Ask Sherlock:", key="user_input")
|
344 |
if user_query:
|
345 |
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
346 |
st.write(response.text)
|
347 |
st.session_state.chat_history.append((user_query, response.text)) # Add chat to history
|
348 |
display_chat_history() # Display chat history
|
349 |
|
350 |
+
# Call the fixInputToBottom function after rendering the input text box
|
351 |
+
st.markdown("<script>fixInputToBottom();</script>", unsafe_allow_html=True)
|
352 |
+
|
353 |
|
354 |
if __name__ == "__main__":
|
355 |
main()
|