Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,20 +9,31 @@ wiki_wiki = wikipediaapi.Wikipedia(
|
|
9 |
)
|
10 |
|
11 |
def get_wikipedia_summary(name):
|
12 |
-
"""Retrieve and
|
13 |
page = wiki_wiki.page(name)
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def generate_dynamic_response(person_name, user_input, context):
|
17 |
"""Dynamically generate a response based on the historical figure's persona and context."""
|
18 |
user_input = user_input.lower()
|
19 |
-
intro = f"I am {person_name}.
|
20 |
|
21 |
sentences = [s.strip() for s in context.split('.') if len(s) > 10]
|
22 |
relevant_sentences = [s for s in sentences if any(word in s.lower() for word in user_input.split())]
|
23 |
|
24 |
if relevant_sentences:
|
25 |
response = random.choice(relevant_sentences)
|
|
|
|
|
|
|
|
|
26 |
else:
|
27 |
response = random.choice([
|
28 |
"That is an intriguing question. What makes you ask?",
|
@@ -44,9 +55,12 @@ person_name = st.text_input("Enter the name of a historical figure:")
|
|
44 |
if person_name and not st.session_state.person_name:
|
45 |
st.session_state.person_name = person_name
|
46 |
st.session_state.summary = get_wikipedia_summary(person_name)
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
-
if st.session_state.person_name:
|
50 |
user_input = st.text_input("Ask a question:")
|
51 |
if user_input:
|
52 |
response = generate_dynamic_response(st.session_state.person_name, user_input, st.session_state.summary)
|
|
|
9 |
)
|
10 |
|
11 |
def get_wikipedia_summary(name):
|
12 |
+
"""Retrieve and clean the Wikipedia summary for a historical figure, avoiding disambiguation pages."""
|
13 |
page = wiki_wiki.page(name)
|
14 |
+
if not page.exists():
|
15 |
+
return "Sorry, I couldn't find information on that historical figure."
|
16 |
+
|
17 |
+
# Remove disambiguation-related content
|
18 |
+
if 'may refer to' in page.summary.lower():
|
19 |
+
return "This name refers to multiple topics. Can you provide more details?"
|
20 |
+
|
21 |
+
return page.summary
|
22 |
|
23 |
def generate_dynamic_response(person_name, user_input, context):
|
24 |
"""Dynamically generate a response based on the historical figure's persona and context."""
|
25 |
user_input = user_input.lower()
|
26 |
+
intro = f"Hello! I am {person_name}. Do you have any questions for me?"
|
27 |
|
28 |
sentences = [s.strip() for s in context.split('.') if len(s) > 10]
|
29 |
relevant_sentences = [s for s in sentences if any(word in s.lower() for word in user_input.split())]
|
30 |
|
31 |
if relevant_sentences:
|
32 |
response = random.choice(relevant_sentences)
|
33 |
+
elif "fishing" in user_input:
|
34 |
+
response = "Ah, fishing! A pastime I did not indulge in, but I was always fascinated by the natural world and its forces."
|
35 |
+
elif "hobbies" in user_input or "free time" in user_input:
|
36 |
+
response = "In my free time, I was consumed by my experiments, always seeking the next great discovery."
|
37 |
else:
|
38 |
response = random.choice([
|
39 |
"That is an intriguing question. What makes you ask?",
|
|
|
55 |
if person_name and not st.session_state.person_name:
|
56 |
st.session_state.person_name = person_name
|
57 |
st.session_state.summary = get_wikipedia_summary(person_name)
|
58 |
+
if "multiple topics" in st.session_state.summary:
|
59 |
+
st.write(st.session_state.summary)
|
60 |
+
else:
|
61 |
+
st.session_state.chat_history.append((person_name, "I am here. Let us talk."))
|
62 |
|
63 |
+
if st.session_state.person_name and "multiple topics" not in st.session_state.summary:
|
64 |
user_input = st.text_input("Ask a question:")
|
65 |
if user_input:
|
66 |
response = generate_dynamic_response(st.session_state.person_name, user_input, st.session_state.summary)
|