Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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("
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}")
|