Spaces:
Running
Running
File size: 4,273 Bytes
156fb9c fb13fa4 5362559 142dfe7 156fb9c fb13fa4 156fb9c 142dfe7 156fb9c fb13fa4 f928045 142dfe7 fb13fa4 f928045 fb13fa4 f928045 fb13fa4 142dfe7 fb13fa4 156fb9c fb13fa4 72a9cec be278c3 fb13fa4 156fb9c 142dfe7 fb13fa4 7169139 142dfe7 156fb9c b64b83d 523e8fd 156fb9c 7169139 0c033a8 7169139 be278c3 0c033a8 be278c3 7169139 be278c3 7169139 be278c3 7169139 be278c3 7169139 0c033a8 142dfe7 7169139 142dfe7 7169139 142dfe7 1dd0272 7169139 1dd0272 142dfe7 1dd0272 142dfe7 0c033a8 1dd0272 142dfe7 1dd0272 156fb9c 0c033a8 b64b83d |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
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() |