Spaces:
Runtime error
Runtime error
Update sherlock2.py
Browse files- sherlock2.py +87 -142
sherlock2.py
CHANGED
@@ -213,173 +213,118 @@ def search_internet(case_text):
|
|
213 |
|
214 |
return internet_search_results
|
215 |
|
216 |
-
def investigate(
|
217 |
-
"""
|
218 |
-
|
219 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
# Extract keywords and important information from the text
|
225 |
-
keywords = extract_keywords_simple("\n\n".join(case_text))
|
226 |
-
|
227 |
-
# Generate embeddings for the extracted text
|
228 |
-
case_embeddings = generate_embeddings_from_documents(case_text)
|
229 |
-
|
230 |
-
# Process images using Gemini Pro Vision
|
231 |
-
image_insights = process_images(uploaded_images)
|
232 |
-
|
233 |
-
# Combine text, image, and keyword information
|
234 |
-
combined_information = {
|
235 |
-
"case_text": case_text,
|
236 |
-
"case_embeddings": case_embeddings,
|
237 |
-
"image_insights": image_insights,
|
238 |
-
"keywords": keywords
|
239 |
-
}
|
240 |
-
|
241 |
-
# Analyze combined information using Gemini 1.5 Pro
|
242 |
-
prompt = """
|
243 |
-
You are Sherlock Holmes, the renowned detective. Analyze the following case information and provide insights or
|
244 |
-
suggestions for further investigation:
|
245 |
-
""" + str(combined_information)
|
246 |
-
|
247 |
-
response = model.generate_content([sherlock_persona, sherlock_guidelines, prompt, *case_embeddings])
|
248 |
-
sherlock_analysis = response.text # Store Sherlock 's analysis
|
249 |
-
|
250 |
-
# Search Wikipedia and the web for related information
|
251 |
-
wikipedia_info = search_and_scrape_wikipedia(keywords)
|
252 |
-
web_search_results = search_internet("\n\n".join(case_text)) # Search the web
|
253 |
-
|
254 |
-
# Generate a case report in Sherlock Holmes's style
|
255 |
-
report_prompt = """
|
256 |
-
You are Sherlock Holmes, the renowned detective. Based on the case information, your analysis, findings from
|
257 |
-
Wikipedia and the web, and the extracted keywords, generate a comprehensive case report in your signature style,
|
258 |
-
including deductions, potential suspects, and conclusions.
|
259 |
-
"""
|
260 |
-
|
261 |
-
final_report = model.generate_content([sherlock_persona, sherlock_guidelines, report_prompt,
|
262 |
-
*case_embeddings, str(wikipedia_info), str(web_search_results)])
|
263 |
-
|
264 |
-
return {
|
265 |
-
"sherlock_analysis": sherlock_analysis,
|
266 |
-
"wikipedia_info": wikipedia_info,
|
267 |
-
"web_search_results": web_search_results,
|
268 |
-
"final_report": final_report.text
|
269 |
-
}
|
270 |
-
|
271 |
-
def chat_with_sherlock(user_query, chat_history=None):
|
272 |
-
"""Handles the chat interaction with Sherlock Holmes (Gemini 1.0 Pro)."""
|
273 |
-
if chat_history is None:
|
274 |
-
chat_history = []
|
275 |
-
|
276 |
-
# 1. Create a list of 'Part' objects from the chat history
|
277 |
-
chat_parts = [
|
278 |
-
glm.Part(text=f"{message['role']}: {message['content']}")
|
279 |
-
for message in chat_history
|
280 |
-
]
|
281 |
-
|
282 |
-
# 2. Add the user query as a new Part
|
283 |
-
chat_parts.append(glm.Part(text=f"user: {user_query}"))
|
284 |
-
|
285 |
-
# 3. Generate response using the list of Parts
|
286 |
-
response = model.generate_content(
|
287 |
-
[sherlock_persona, sherlock_guidelines , glm.Content(parts=chat_parts)]
|
288 |
-
)
|
289 |
-
|
290 |
-
# 4. Add Sherlock's response to chat history
|
291 |
-
chat_history.append({"role": "assistant", "content": response.text})
|
292 |
-
|
293 |
-
return response.text, chat_history
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
def display_chat_history():
|
298 |
-
"""Displays the chat history between the user and Sherlock."""
|
299 |
-
for user_msg, bot_msg in st.session_state.chat_history:
|
300 |
-
st.info(f"**You:** {user_msg}")
|
301 |
-
st.success(f"**Sherlock:** {bot_msg}")
|
302 |
-
|
303 |
-
|
304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
def main():
|
306 |
# --- Vintage Sherlock Holmes Theme ---
|
307 |
st.set_page_config(page_title="AI Detective Sherlock Holmes", page_icon=":mag_right:")
|
308 |
|
309 |
-
# Custom CSS for Styling
|
310 |
-
vintage_css = """
|
311 |
<style>
|
312 |
body {
|
313 |
background-color: #d2b48c; /* Antique White */
|
314 |
color: #332200; /* Dark Brown */
|
315 |
-
font-
|
316 |
}
|
317 |
h1, h2, h3 {
|
318 |
color: #8b4513; /* Saddle Brown */
|
319 |
}
|
320 |
.stTextInput > div > div > input {
|
321 |
-
border: 1px solid #
|
322 |
-
border-radius: 5px;
|
323 |
}
|
324 |
-
.stButton > button {
|
325 |
background-color: #8b4513;
|
326 |
color: white;
|
327 |
border: none;
|
328 |
-
border-radius:
|
329 |
-
}
|
330 |
-
/* Fixed input at the bottom */
|
331 |
-
footer {
|
332 |
-
position: fixed;
|
333 |
-
bottom: 0;
|
334 |
-
width: 100%;
|
335 |
-
background-color: #eee;
|
336 |
-
padding: 10px;
|
337 |
}
|
338 |
-
</
|
339 |
"""
|
340 |
-
st.markdown(vintage_css, unsafe_allow_html=True)
|
341 |
|
342 |
# Title and Header
|
343 |
st.title("AI Detective Sherlock Holmes")
|
344 |
st.header("_'Elementary, my dear Watson!'_")
|
345 |
|
346 |
-
#
|
347 |
-
st.sidebar.title("
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
if
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
# Input box in the footer
|
363 |
-
with st.container(): # Create a container for better visual separation
|
364 |
-
user_query = st.text_input("Ask Sherlock:", key="sherlock_input")
|
365 |
-
|
366 |
-
# Case investigation button
|
367 |
-
if uploaded_documents and uploaded_images and st.button("Analyze Case"):
|
368 |
-
results = investigate(uploaded_documents, uploaded_images)
|
369 |
-
st.header("Case Report")
|
370 |
-
st.write(results["final_report"])
|
371 |
-
|
372 |
-
# Chat with Sherlock
|
373 |
-
if user_query:
|
374 |
-
response, st.session_state.chat_history = chat_with_sherlock(user_query, st.session_state.chat_history)
|
375 |
-
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
376 |
-
# Redisplay chat history (to update the UI)
|
377 |
-
for message in st .session_state.chat_history:
|
378 |
-
if message["role"] == "user":
|
379 |
-
st.info(f"**You:** {message['content']}")
|
380 |
-
else:
|
381 |
-
st.success(f"**Sherlock:** {message['content']}")
|
382 |
-
|
383 |
-
|
384 |
-
if __name__ == "__main__":
|
385 |
main()
|
|
|
213 |
|
214 |
return internet_search_results
|
215 |
|
216 |
+
def investigate():
|
217 |
+
"""Handles the case investigation process, including file upload, text extraction, embedding generation,
|
218 |
+
image processing, information analysis using Gemini models, web/Wikipedia search, and case report generation.
|
219 |
"""
|
220 |
+
st.header("Case Investigation")
|
221 |
+
|
222 |
+
# File upload for documents and images
|
223 |
+
uploaded_documents = st.file_uploader("Upload Case Documents", accept_multiple_files=True, type=["txt", "pdf", "docx"])
|
224 |
+
uploaded_images = st.file_uploader("Upload Case Images", accept_multiple_files=True, type=["jpg", "png", "jpeg"])
|
225 |
+
|
226 |
+
if uploaded_documents and uploaded_images and st.button("Analyze Case"):
|
227 |
+
# Extract text from uploaded documents
|
228 |
+
case_text = extract_text_from_files(uploaded_documents)
|
229 |
+
|
230 |
+
# Extract keywords and important information from the text
|
231 |
+
keywords = extract_keywords_simple("\n\n".join(case_text))
|
232 |
+
|
233 |
+
# Generate embeddings for the extracted text
|
234 |
+
case_embeddings = generate_embeddings_from_documents(case_text)
|
235 |
+
|
236 |
+
# Process images using Gemini Pro Vision
|
237 |
+
image_insights = process_images(uploaded_images)
|
238 |
+
|
239 |
+
# Combine text, image, and keyword information
|
240 |
+
combined_information = {
|
241 |
+
"case_text": case_text,
|
242 |
+
"case_embeddings": case_embeddings,
|
243 |
+
"image_insights": image_insights,
|
244 |
+
"keywords": keywords
|
245 |
+
}
|
246 |
+
|
247 |
+
# Analyze combined information using Gemini 1.5 Pro
|
248 |
+
prompt = """
|
249 |
+
You are Sherlock Holmes, the renowned detective. Analyze the following case information and provide insights or
|
250 |
+
suggestions for further investigation:
|
251 |
+
""" + str(combined_information)
|
252 |
+
|
253 |
+
response = model.generate_content([sherlock_persona, sherlock_guidelines, prompt, *case_embeddings])
|
254 |
+
st.write(response.text)
|
255 |
+
|
256 |
+
# Search Wikipedia and the web for related information
|
257 |
+
wikipedia_info = search_and_scrape_wikipedia(keywords)
|
258 |
+
web_search_results = search_internet("\n\n".join(case_text)) # Search the web
|
259 |
+
|
260 |
+
# Generate a case report in Sherlock Holmes's style
|
261 |
+
report_prompt = """
|
262 |
+
You are Sherlock Holmes, the renowned detective. Based on the case information, your analysis, findings from
|
263 |
+
Wikipedia and the web, and the extracted keywords, generate a comprehensive case report in your signature style,
|
264 |
+
including deductions, potential suspects, and conclusions.
|
265 |
+
"""
|
266 |
+
|
267 |
+
final_report = model.generate_content([sherlock_persona, sherlock_guidelines, report_prompt,
|
268 |
+
*case_embeddings, str(wikipedia_info), str(web_search_results)])
|
269 |
+
st.header("Case Report")
|
270 |
+
st.write(final_report.text)
|
271 |
|
272 |
+
else:
|
273 |
+
st.info("Please upload both case documents and images to proceed with the investigation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
+
# Chat with Sherlock Holmes (Gemini 1.5 Pro)
|
276 |
+
st.write("Alternatively, you may engage in a conversation with Sherlock Holmes.")
|
277 |
+
user_query = st.text_input("Ask Sherlock:")
|
278 |
+
if user_query:
|
279 |
+
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
280 |
+
st.write(response.text)
|
281 |
def main():
|
282 |
# --- Vintage Sherlock Holmes Theme ---
|
283 |
st.set_page_config(page_title="AI Detective Sherlock Holmes", page_icon=":mag_right:")
|
284 |
|
285 |
+
# Custom CSS for Styling
|
286 |
+
vintage_css = """
|
287 |
<style>
|
288 |
body {
|
289 |
background-color: #d2b48c; /* Antique White */
|
290 |
color: #332200; /* Dark Brown */
|
291 |
+
font-family: 'Times New Roman', serif;
|
292 |
}
|
293 |
h1, h2, h3 {
|
294 |
color: #8b4513; /* Saddle Brown */
|
295 |
}
|
296 |
.stTextInput > div > div > input {
|
297 |
+
border: 1px solid #8b4513;
|
298 |
+
border-radius: 5px;
|
299 |
}
|
300 |
+
.stButton > button {
|
301 |
background-color: #8b4513;
|
302 |
color: white;
|
303 |
border: none;
|
304 |
+
border-radius: 5px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
}
|
306 |
+
</style>
|
307 |
"""
|
308 |
+
st.markdown(vintage_css, unsafe_allow_html=True) # Apply custom CSS
|
309 |
|
310 |
# Title and Header
|
311 |
st.title("AI Detective Sherlock Holmes")
|
312 |
st.header("_'Elementary, my dear Watson!'_")
|
313 |
|
314 |
+
# Add a sidebar for navigation
|
315 |
+
st.sidebar.title("Navigation")
|
316 |
+
options = ["Investigate Case", "Chat with Sherlock"]
|
317 |
+
choice = st.sidebar.radio("Choose an option:", options)
|
318 |
+
|
319 |
+
if choice == "Investigate Case":
|
320 |
+
investigate()
|
321 |
+
else:
|
322 |
+
# Chat with Sherlock Holmes (Gemini 1.5 Pro)
|
323 |
+
st.write("No case files uploaded. Feel free to chat with Sherlock Holmes.")
|
324 |
+
user_query = st.text_input("Ask Sherlock:")
|
325 |
+
if user_query:
|
326 |
+
response = model.generate_content([sherlock_persona, sherlock_guidelines, user_query])
|
327 |
+
st.write(response.text)
|
328 |
+
|
329 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
main()
|