amaltese commited on
Commit
4140b01
·
verified ·
1 Parent(s): 3f076ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -40
app.py CHANGED
@@ -1,49 +1,34 @@
1
  import streamlit as st
2
  import wikipediaapi
 
 
 
 
 
 
 
3
 
4
  def get_wikipedia_summary(name):
5
- wiki = wikipediaapi.Wikipedia(
6
- user_agent='HistoricalFiguresBot/1.0 (educational-app)',
7
- language='en'
8
- )
9
- page = wiki.page(name)
10
- return page.summary if page.exists() else None
11
 
12
- # Added simple character response system
13
- def get_historical_response(context, user_input):
14
- # Basic response templates based on context
15
- if "born" in context.lower():
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"Based on my experiences, I would say that {user_input}"
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
- for msg in st.session_state.messages:
31
- st.chat_message(msg["role"]).write(msg["content"])
32
 
33
- if user_input := st.chat_input("Message..."):
34
- if not person_name:
35
- st.error("Please enter a historical figure's name")
36
- st.stop()
37
-
38
- st.session_state.messages.append({"role": "user", "content": user_input})
39
- st.chat_message("user").write(user_input)
40
 
41
- with st.spinner("Researching..."):
42
- context = get_wikipedia_summary(person_name)
43
- if context:
44
- st.write("Historical Context:", context)
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}")