Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import wikipediaapi
|
| 6 |
+
|
| 7 |
+
# Load historical figures and tutor topics from Wikipedia dynamically
|
| 8 |
+
wiki_wiki = wikipediaapi.Wikipedia("en")
|
| 9 |
+
|
| 10 |
+
# Load local AI model (Mistral-7B or Llama-2)
|
| 11 |
+
chat_model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 12 |
+
|
| 13 |
+
def get_wikipedia_summary(name):
|
| 14 |
+
page = wiki_wiki.page(name)
|
| 15 |
+
return page.summary if page.exists() else "Sorry, I couldn't find information on that historical figure."
|
| 16 |
+
|
| 17 |
+
def chat_with_ai(person_name, user_input):
|
| 18 |
+
context = get_wikipedia_summary(person_name)
|
| 19 |
+
prompt = f"You are {person_name}. Based on this historical information: {context}\n\nUser: {user_input}\n{person_name}:"
|
| 20 |
+
response = chat_model(prompt, max_length=200, truncation=True)
|
| 21 |
+
return response[0]["generated_text"].split("User:")[0].strip()
|
| 22 |
+
|
| 23 |
+
st.title("Educational Chatbot")
|
| 24 |
+
mode = st.sidebar.selectbox("Select Chat Mode", ["Chat with Historical Figures", "Study with a Tutor"])
|
| 25 |
+
|
| 26 |
+
if mode == "Chat with Historical Figures":
|
| 27 |
+
person_name = st.text_input("Enter the name of a historical figure:")
|
| 28 |
+
if person_name:
|
| 29 |
+
st.write(f"You are now chatting with **{person_name}**!")
|
| 30 |
+
|
| 31 |
+
elif mode == "Study with a Tutor":
|
| 32 |
+
topic = st.sidebar.selectbox("Choose a Study Topic", ["Mathematics", "History", "Physics"])
|
| 33 |
+
st.write(f"You are now studying **{topic}**!")
|
| 34 |
+
|
| 35 |
+
# Chat input
|
| 36 |
+
if "messages" not in st.session_state:
|
| 37 |
+
st.session_state.messages = []
|
| 38 |
+
|
| 39 |
+
for msg in st.session_state.messages:
|
| 40 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 41 |
+
|
| 42 |
+
user_input = st.chat_input("Type your message here...")
|
| 43 |
+
|
| 44 |
+
if user_input and mode == "Chat with Historical Figures" and person_name:
|
| 45 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 46 |
+
st.chat_message("user").write(user_input)
|
| 47 |
+
response = chat_with_ai(person_name, user_input)
|
| 48 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 49 |
+
st.chat_message("assistant").write(response)
|
| 50 |
+
|
| 51 |
+
# Instructions for Deployment
|
| 52 |
+
st.sidebar.subheader("Deployment Instructions")
|
| 53 |
+
st.sidebar.markdown("1. Ensure `transformers` and `wikipedia-api` libraries are installed.")
|
| 54 |
+
st.sidebar.markdown("2. Rename `chatbot.py` to `app.py` for Hugging Face Spaces.")
|
| 55 |
+
st.sidebar.markdown("3. Run `streamlit run app.py` locally.")
|
| 56 |
+
st.sidebar.markdown("4. Deploy on [Hugging Face Spaces](https://huggingface.co/spaces) or Replit.")
|