File size: 2,360 Bytes
ec76126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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.")