Upload 2 files
Browse files- app_hf_streamlit.py +46 -0
- requirements.txt +3 -0
app_hf_streamlit.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Kiswahili Grammar Tutor", page_icon="🧠")
|
| 7 |
+
|
| 8 |
+
# Load model and tokenizer from Hugging Face Hub
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained("Bur3hani/kiswmod")
|
| 12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Bur3hani/kiswmod")
|
| 13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
model.to(device)
|
| 15 |
+
return tokenizer, model, device
|
| 16 |
+
|
| 17 |
+
tokenizer, model, device = load_model()
|
| 18 |
+
|
| 19 |
+
st.title("🧠 Kiswahili Grammar Tutor")
|
| 20 |
+
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).")
|
| 21 |
+
|
| 22 |
+
if "history" not in st.session_state:
|
| 23 |
+
st.session_state.history = []
|
| 24 |
+
|
| 25 |
+
user_input = st.text_input("Mwanafunzi:", "")
|
| 26 |
+
|
| 27 |
+
if st.button("Submit") and user_input:
|
| 28 |
+
# Build dialogue history
|
| 29 |
+
dialogue = ""
|
| 30 |
+
for student, teacher in st.session_state.history:
|
| 31 |
+
dialogue += f"Mwanafunzi: {student}\nMwalimu: {teacher}\n"
|
| 32 |
+
dialogue += f"Mwanafunzi: {user_input}\nMwalimu:"
|
| 33 |
+
|
| 34 |
+
# Generate response
|
| 35 |
+
inputs = tokenizer(dialogue, return_tensors="pt", truncation=True, padding="max_length", max_length=256).to(device)
|
| 36 |
+
output_ids = model.generate(**inputs, max_length=128)
|
| 37 |
+
reply = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 38 |
+
|
| 39 |
+
st.session_state.history.append((user_input, reply))
|
| 40 |
+
if len(st.session_state.history) > 3:
|
| 41 |
+
st.session_state.history = st.session_state.history[-3:]
|
| 42 |
+
|
| 43 |
+
# Display conversation
|
| 44 |
+
for student, teacher in st.session_state.history:
|
| 45 |
+
st.markdown(f"**Mwanafunzi:** {student}")
|
| 46 |
+
st.markdown(f"**Mwalimu:** {teacher}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch
|