Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,50 @@
|
|
1 |
import streamlit as st
|
2 |
-
import json
|
3 |
-
import requests
|
4 |
-
from transformers import pipeline
|
5 |
import wikipediaapi
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
user_agent='HistoricalFiguresApp/1.0 ([email protected])', # Replace with your contact info
|
10 |
-
language='en'
|
11 |
-
)
|
12 |
-
|
13 |
-
# Load local AI model (HuggingFaceH4/zephyr-7b-beta)
|
14 |
-
chat_model = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta")
|
15 |
-
|
16 |
|
|
|
17 |
def get_wikipedia_summary(name):
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def chat_with_ai(person_name, user_input):
|
22 |
context = get_wikipedia_summary(person_name)
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
st.title("Educational Chatbot")
|
28 |
-
mode = st.sidebar.selectbox("Select Chat Mode", ["Chat with Historical Figures", "Study with a Tutor"])
|
29 |
-
|
30 |
-
if mode == "Chat with Historical Figures":
|
31 |
-
person_name = st.text_input("Enter the name of a historical figure:")
|
32 |
-
if person_name:
|
33 |
-
st.write(f"You are now chatting with **{person_name}**!")
|
34 |
-
|
35 |
-
elif mode == "Study with a Tutor":
|
36 |
-
topic = st.sidebar.selectbox("Choose a Study Topic", ["Mathematics", "History", "Physics"])
|
37 |
-
st.write(f"You are now studying **{topic}**!")
|
38 |
-
|
39 |
-
# Chat input
|
40 |
if "messages" not in st.session_state:
|
41 |
st.session_state.messages = []
|
42 |
|
43 |
for msg in st.session_state.messages:
|
44 |
st.chat_message(msg["role"]).write(msg["content"])
|
45 |
|
46 |
-
user_input
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
50 |
st.chat_message("user").write(user_input)
|
51 |
-
|
52 |
-
st.
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.sidebar.subheader("Deployment Instructions")
|
57 |
-
st.sidebar.markdown("1. Ensure `transformers` and `wikipedia-api` libraries are installed.")
|
58 |
-
st.sidebar.markdown("2. Rename `chatbot.py` to `app.py` for Hugging Face Spaces.")
|
59 |
-
st.sidebar.markdown("3. Run `streamlit run app.py` locally.")
|
60 |
-
st.sidebar.markdown("4. Deploy on [Hugging Face Spaces](https://huggingface.co/spaces) or Replit.")
|
|
|
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:
|
34 |
st.session_state.messages = []
|
35 |
|
36 |
for msg in st.session_state.messages:
|
37 |
st.chat_message(msg["role"]).write(msg["content"])
|
38 |
|
39 |
+
if user_input := st.chat_input("Message..."):
|
40 |
+
if not person_name:
|
41 |
+
st.error("Please enter a historical figure's name")
|
42 |
+
st.stop()
|
43 |
+
|
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)
|
|
|
|
|
|
|
|
|
|