Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
import requests | |
from transformers import pipeline | |
import wikipediaapi | |
# Load historical figures and tutor topics from Wikipedia dynamically | |
wiki_wiki = wikipediaapi.Wikipedia("en") | |
# Load local AI model (Mistral-7B or Llama-2) | |
chat_model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1") | |
def get_wikipedia_summary(name): | |
page = wiki_wiki.page(name) | |
return page.summary if page.exists() else "Sorry, I couldn't find information on that historical figure." | |
def chat_with_ai(person_name, user_input): | |
context = get_wikipedia_summary(person_name) | |
prompt = f"You are {person_name}. Based on this historical information: {context}\n\nUser: {user_input}\n{person_name}:" | |
response = chat_model(prompt, max_length=200, truncation=True) | |
return response[0]["generated_text"].split("User:")[0].strip() | |
st.title("Educational Chatbot") | |
mode = st.sidebar.selectbox("Select Chat Mode", ["Chat with Historical Figures", "Study with a Tutor"]) | |
if mode == "Chat with Historical Figures": | |
person_name = st.text_input("Enter the name of a historical figure:") | |
if person_name: | |
st.write(f"You are now chatting with **{person_name}**!") | |
elif mode == "Study with a Tutor": | |
topic = st.sidebar.selectbox("Choose a Study Topic", ["Mathematics", "History", "Physics"]) | |
st.write(f"You are now studying **{topic}**!") | |
# Chat input | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
for msg in st.session_state.messages: | |
st.chat_message(msg["role"]).write(msg["content"]) | |
user_input = st.chat_input("Type your message here...") | |
if user_input and mode == "Chat with Historical Figures" and person_name: | |
st.session_state.messages.append({"role": "user", "content": user_input}) | |
st.chat_message("user").write(user_input) | |
response = chat_with_ai(person_name, user_input) | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
st.chat_message("assistant").write(response) | |
# Instructions for Deployment | |
st.sidebar.subheader("Deployment Instructions") | |
st.sidebar.markdown("1. Ensure `transformers` and `wikipedia-api` libraries are installed.") | |
st.sidebar.markdown("2. Run `streamlit run chatbot.py` locally.") | |
st.sidebar.markdown("3. Deploy on [Hugging Face Spaces](https://huggingface.co/spaces) or Replit.") | |