amaltese commited on
Commit
09be789
·
verified ·
1 Parent(s): 318c58a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -13,26 +13,24 @@ def get_wikipedia_summary(name):
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_persona_response(person_name, user_input, context):
17
- """Generate a conversational response based on user input and historical context."""
18
  user_input = user_input.lower()
 
19
 
20
- hobby_keywords = ["hobby", "free time", "fun", "leisure", "relax"]
21
- politics_keywords = ["government", "law", "president", "policy", "rights"]
22
- war_keywords = ["battle", "war", "fight", "military", "enemy"]
23
 
24
- if any(word in user_input for word in hobby_keywords):
25
- return f"Ah, leisure! In my time, I enjoyed reading, studying science, and tending to my gardens. What do you do for leisure?"
26
- elif any(word in user_input for word in politics_keywords):
27
- return f"Politics is a fascinating matter. I strongly believed in individual liberty and self-governance. What are your thoughts on democracy?"
28
- elif any(word in user_input for word in war_keywords):
29
- return f"Conflict is often necessary to secure freedom. I played my role in the Revolution to help shape a new nation. What do you know of it?"
30
  else:
31
- relevant_sentences = [s.strip() for s in context.split('.') if any(word in s.lower() for word in user_input.split())]
32
- if relevant_sentences:
33
- return random.choice(relevant_sentences) + '.'
34
- else:
35
- return f"That is an interesting question. Could you elaborate on what you mean?"
 
 
36
 
37
  st.title("Chat with a Historical Figure!")
38
 
@@ -46,12 +44,12 @@ person_name = st.text_input("Enter the name of a historical figure:")
46
  if person_name and not st.session_state.person_name:
47
  st.session_state.person_name = person_name
48
  st.session_state.summary = get_wikipedia_summary(person_name)
49
- st.session_state.chat_history.append((person_name, "I am here. Ask what you wish."))
50
 
51
  if st.session_state.person_name:
52
  user_input = st.text_input("Ask a question:")
53
  if user_input:
54
- response = generate_persona_response(st.session_state.person_name, user_input, st.session_state.summary)
55
  st.session_state.chat_history.append(("You", user_input))
56
  st.session_state.chat_history.append((st.session_state.person_name, response))
57
 
 
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_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}. Let us discuss."
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?",
29
+ "A curious thought. Tell me more of your own views.",
30
+ "History is shaped by questions such as these. What do you believe?"
31
+ ])
32
+
33
+ return f"{intro} {response}"
34
 
35
  st.title("Chat with a Historical Figure!")
36
 
 
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
+ st.session_state.chat_history.append((person_name, "I am here. Let us talk."))
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)
53
  st.session_state.chat_history.append(("You", user_input))
54
  st.session_state.chat_history.append((st.session_state.person_name, response))
55