Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,34 @@
|
|
1 |
import streamlit as st
|
2 |
import wikipediaapi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def get_wikipedia_summary(name):
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
)
|
9 |
-
page = wiki.page(name)
|
10 |
-
return page.summary if page.exists() else None
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
return f"Indeed, as someone who experienced those times, I can tell you that {user_input}"
|
17 |
-
elif "died" in context.lower():
|
18 |
-
return f"In my time, we would have approached this by {user_input}"
|
19 |
else:
|
20 |
-
return f"
|
21 |
-
|
22 |
-
st.title("Historical Figures Chat")
|
23 |
-
st.write("Chat with historical figures using Wikipedia information!")
|
24 |
-
|
25 |
-
person_name = st.text_input("Enter historical figure name:")
|
26 |
-
|
27 |
-
if "messages" not in st.session_state:
|
28 |
-
st.session_state.messages = []
|
29 |
|
30 |
-
|
31 |
-
st.chat_message(msg["role"]).write(msg["content"])
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
39 |
-
st.chat_message("user").write(user_input)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
response = get_historical_response(context, user_input)
|
46 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
47 |
-
st.chat_message("assistant").write(response)
|
48 |
-
else:
|
49 |
-
st.error(f"Could not find information about {person_name}")
|
|
|
1 |
import streamlit as st
|
2 |
import wikipediaapi
|
3 |
+
import random
|
4 |
+
|
5 |
+
# Initialize Wikipedia API
|
6 |
+
wiki_wiki = wikipediaapi.Wikipedia(
|
7 |
+
user_agent='HistoricalFiguresApp/1.0 ([email protected])',
|
8 |
+
language='en'
|
9 |
+
)
|
10 |
|
11 |
def get_wikipedia_summary(name):
|
12 |
+
"""Retrieve and summarize the Wikipedia page for a given historical figure."""
|
13 |
+
page = wiki_wiki.page(name)
|
14 |
+
return page.summary if page.exists() else "Sorry, I couldn't find information on that historical figure."
|
|
|
|
|
|
|
15 |
|
16 |
+
def generate_response(person_name, user_input, context):
|
17 |
+
"""Generate a dynamic response based on the Wikipedia summary and user input."""
|
18 |
+
if any(word in user_input.lower() for word in ["who", "what", "when", "where", "why", "how"]):
|
19 |
+
return f"{person_name}: {context}"
|
|
|
|
|
|
|
20 |
else:
|
21 |
+
return f"{person_name}: That is an interesting thought. Could you elaborate?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
st.title("Chat with a Historical Figure!")
|
|
|
24 |
|
25 |
+
# User inputs a historical figure
|
26 |
+
person_name = st.text_input("Enter the name of a historical figure:")
|
27 |
+
if person_name:
|
28 |
+
summary = get_wikipedia_summary(person_name)
|
29 |
+
st.write(f"**Historical Context:** {summary}")
|
|
|
|
|
30 |
|
31 |
+
user_input = st.text_input("Ask a question:")
|
32 |
+
if user_input:
|
33 |
+
response = generate_response(person_name, user_input, summary)
|
34 |
+
st.write(f"**{person_name}:** {response}")
|
|
|
|
|
|
|
|
|
|