amaltese commited on
Commit
e8d591c
·
verified ·
1 Parent(s): 5fe3846

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -1,33 +1,24 @@
1
  import streamlit as st
2
  import wikipediaapi
3
- import requests
4
 
5
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
6
- headers = {"Authorization": f"Bearer {st.secrets['HUGGINGFACE_TOKEN']}"}
7
-
8
- @st.cache_data
9
  def get_wikipedia_summary(name):
10
  wiki = wikipediaapi.Wikipedia('en')
11
  page = wiki.page(name)
12
  return page.summary if page.exists() else None
13
 
14
- def query_model(payload):
15
- response = requests.post(API_URL, headers=headers, json=payload)
16
- return response.json()
17
-
18
- def chat_with_ai(person_name, user_input):
19
- context = get_wikipedia_summary(person_name)
20
- if not context:
21
- return f"Could not find information about {person_name}"
22
-
23
- prompt = f"You are {person_name}. Context: {context}\n\nUser: {user_input}\n{person_name}:"
24
- try:
25
- response = query_model({"inputs": prompt})
26
- return response[0]["generated_text"].split("User:")[0].strip()
27
- except Exception as e:
28
- return f"Error: {str(e)}"
29
 
30
  st.title("Historical Figures Chat")
 
 
31
  person_name = st.text_input("Enter historical figure name:")
32
 
33
  if "messages" not in st.session_state:
@@ -44,7 +35,12 @@ if user_input := st.chat_input("Message..."):
44
  st.session_state.messages.append({"role": "user", "content": user_input})
45
  st.chat_message("user").write(user_input)
46
 
47
- with st.spinner("Thinking..."):
48
- response = chat_with_ai(person_name, user_input)
49
- st.session_state.messages.append({"role": "assistant", "content": response})
50
- st.chat_message("assistant").write(response)
 
 
 
 
 
 
1
  import streamlit as st
2
  import wikipediaapi
 
3
 
 
 
 
 
4
  def get_wikipedia_summary(name):
5
  wiki = wikipediaapi.Wikipedia('en')
6
  page = wiki.page(name)
7
  return page.summary if page.exists() else None
8
 
9
+ # Added simple character response system
10
+ def get_historical_response(context, user_input):
11
+ # Basic response templates based on context
12
+ if "born" in context.lower():
13
+ return f"Indeed, as someone who experienced those times, I can tell you that {user_input}"
14
+ elif "died" in context.lower():
15
+ return f"In my time, we would have approached this by {user_input}"
16
+ else:
17
+ return f"Based on my experiences, I would say that {user_input}"
 
 
 
 
 
 
18
 
19
  st.title("Historical Figures Chat")
20
+ st.write("Chat with historical figures using Wikipedia information!")
21
+
22
  person_name = st.text_input("Enter historical figure name:")
23
 
24
  if "messages" not in st.session_state:
 
35
  st.session_state.messages.append({"role": "user", "content": user_input})
36
  st.chat_message("user").write(user_input)
37
 
38
+ with st.spinner("Researching..."):
39
+ context = get_wikipedia_summary(person_name)
40
+ if context:
41
+ st.write("Historical Context:", context)
42
+ response = get_historical_response(context, user_input)
43
+ st.session_state.messages.append({"role": "assistant", "content": response})
44
+ st.chat_message("assistant").write(response)
45
+ else:
46
+ st.error(f"Could not find information about {person_name}")