IPTchatbot / chatbot.py
ammoncoder123's picture
Update chatbot.py
abdcaaa verified
raw
history blame
4.27 kB
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
import torch
from huggingface_hub import login
import os
# Force authentication
login(token=os.getenv("HF_TOKEN"))
# ================= CACHE THE MODEL =================
@st.cache_resource
def load_model():
model_id = "ammoncoder123/IPTchatbotModel-1.7B"
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Ensure tokenizer has a chat template, or use a default one
if tokenizer.chat_template is None:
tokenizer.chat_template = "{% for message in messages %}{{'<|' + message['role'] + '|>' + '\n' + message['content'] + '</s>\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% endif %}"
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=quantization_config,
device_map="auto",
torch_dtype=torch.float16,
trust_remote_code=True
)
return pipeline(
"text-generation",
model=model,
tokenizer=tokenizer
)
pipe = load_model()
# ==================== CHAT INTERFACE ====================
st.title("IPT Chatbot Assistance")
st.info("Ask about logbooks, placement in Arusha Technical College, or report writing.")
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User input
if prompt := st.chat_input("Ask about Industrial Practical Training..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
# 1. Fixed system_message structure and quotes
system_message = {
"role": "system",
"content": """You are the "IPT Master Assistant," a specialized AI coordinator for engineering and ICT students in Tanzania undergoing Industrial Practical Training (IPT). Your goal is to provide accurate, encouraging, and practical advice based on official training guidelines.
### CORE KNOWLEDGE & RULES:
1. DAILY LOGBOOK:
- Entries must include: Major tasks performed, time spent (in hours), and any absences with reasons.
- Style: Keep it simple and clear (3-5 sentences).
- Importance: It accounts for 10% of the final grade.
2. WEEKLY REPORTS:
- Focus on ONE major technical task from the week (Maintenance, Repair, or Production).
- Use the "Detailed Description" page. Be concise but technical.
3. PLACEMENT STRATEGY:
- Placement Must Match Department: Always advise students to choose a center that correlates with their field.
- ICT Recommendations: Suggest IT departments in firms like TTCL, Halotel, Vodacom, or e-GA.
4. BEHAVIORAL GUIDELINES:
- Polite & Professional: If unrelated to IPT, politely decline.
- Fact-Checking: 100% honesty is required.
### TONE:
Academic mentor. Refer to Tanzanian institutions (ATC, DIT, MUST, UDSM)."""
}
# 2. Fixed line 102 indentation (ensure 12 spaces)
input_messages = [system_message] + st.session_state.messages[-3:]
# 3. Apply the chat template
formatted_prompt = pipe.tokenizer.apply_chat_template(
input_messages,
tokenize=False,
add_generation_prompt=True
)
# 4. Generate response
outputs = pipe(
formatted_prompt,
max_new_tokens=300,
temperature=0.7,
do_sample=True,
top_p=0.9,
return_full_text=False
)
response = outputs[0]["generated_text"].strip()
st.markdown(response)
# Save assistant response to history
st.session_state.messages.append({"role": "assistant", "content": response})
if st.button("Clear Conversation"):
st.session_state.messages = []
st.rerun()