File size: 1,743 Bytes
936b5c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

st.set_page_config(page_title="Kiswahili Grammar Tutor", page_icon="🧠")

# Load model and tokenizer from Hugging Face Hub
@st.cache_resource
def load_model():
    tokenizer = AutoTokenizer.from_pretrained("Bur3hani/kiswmod")
    model = AutoModelForSeq2SeqLM.from_pretrained("Bur3hani/kiswmod")
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model.to(device)
    return tokenizer, model, device

tokenizer, model, device = load_model()

st.title("🧠 Kiswahili Grammar Tutor")
st.markdown("An AI assistant that acts like a Kiswahili teacher or therapist. It corrects sentences and explains grammar in a conversational way (max 3 turns).")

if "history" not in st.session_state:
    st.session_state.history = []

user_input = st.text_input("Mwanafunzi:", "")

if st.button("Submit") and user_input:
    # Build dialogue history
    dialogue = ""
    for student, teacher in st.session_state.history:
        dialogue += f"Mwanafunzi: {student}\nMwalimu: {teacher}\n"
    dialogue += f"Mwanafunzi: {user_input}\nMwalimu:"

    # Generate response
    inputs = tokenizer(dialogue, return_tensors="pt", truncation=True, padding="max_length", max_length=256).to(device)
    output_ids = model.generate(**inputs, max_length=128)
    reply = tokenizer.decode(output_ids[0], skip_special_tokens=True)

    st.session_state.history.append((user_input, reply))
    if len(st.session_state.history) > 3:
        st.session_state.history = st.session_state.history[-3:]

# Display conversation
for student, teacher in st.session_state.history:
    st.markdown(f"**Mwanafunzi:** {student}")
    st.markdown(f"**Mwalimu:** {teacher}")