Spaces:
Running
Running
Update chatbot.py
Browse files- chatbot.py +37 -46
chatbot.py
CHANGED
|
@@ -4,13 +4,14 @@ import torch
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Force authentication
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# ================= CACHE THE MODEL =================
|
| 11 |
@st.cache_resource
|
| 12 |
def load_model():
|
| 13 |
-
model_id = "ammoncoder123/IPTchatbotModel-1.7B"
|
| 14 |
|
| 15 |
quantization_config = BitsAndBytesConfig(
|
| 16 |
load_in_4bit=True,
|
|
@@ -18,6 +19,10 @@ def load_model():
|
|
| 18 |
)
|
| 19 |
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
model_id,
|
| 23 |
quantization_config=quantization_config,
|
|
@@ -29,81 +34,67 @@ def load_model():
|
|
| 29 |
return pipeline(
|
| 30 |
"text-generation",
|
| 31 |
model=model,
|
| 32 |
-
tokenizer=tokenizer
|
| 33 |
-
max_new_tokens=300,
|
| 34 |
-
temperature=0.7,
|
| 35 |
-
do_sample=True,
|
| 36 |
-
top_p=0.9
|
| 37 |
)
|
| 38 |
|
| 39 |
pipe = load_model()
|
| 40 |
|
| 41 |
# ==================== CHAT INTERFACE ====================
|
| 42 |
st.title("IPT Chatbot Assistance")
|
| 43 |
-
st.info("
|
| 44 |
|
| 45 |
-
# Display chat history
|
| 46 |
if "messages" not in st.session_state:
|
| 47 |
st.session_state.messages = []
|
| 48 |
|
|
|
|
| 49 |
for message in st.session_state.messages:
|
| 50 |
-
|
| 51 |
-
st.
|
|
|
|
| 52 |
|
| 53 |
# User input
|
| 54 |
if prompt := st.chat_input("Ask about Industrial Practical Training..."):
|
| 55 |
-
# Add user message to history and display
|
| 56 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 57 |
with st.chat_message("user"):
|
| 58 |
st.markdown(prompt)
|
| 59 |
|
| 60 |
-
# Generate assistant response
|
| 61 |
with st.chat_message("assistant"):
|
| 62 |
with st.spinner("Thinking..."):
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
{"role": "system", "content": system_prompt},
|
| 83 |
-
{"role": "user", "content": prompt}
|
| 84 |
-
]
|
| 85 |
|
| 86 |
# Generate response
|
| 87 |
outputs = pipe(
|
| 88 |
-
|
| 89 |
max_new_tokens=300,
|
| 90 |
temperature=0.7,
|
| 91 |
do_sample=True,
|
| 92 |
-
top_p=0.9
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
response = outputs[0]["generated_text"]
|
| 97 |
-
if isinstance(response, str) and response.startswith(prompt):
|
| 98 |
-
response = response[len(prompt):].strip()
|
| 99 |
-
|
| 100 |
-
# Show the response
|
| 101 |
st.markdown(response)
|
| 102 |
|
| 103 |
-
# Save assistant response to history
|
| 104 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 105 |
|
| 106 |
-
# Optional: Clear conversation button
|
| 107 |
if st.button("Clear Conversation"):
|
| 108 |
st.session_state.messages = []
|
| 109 |
st.rerun()
|
|
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Force authentication
|
| 8 |
+
if "HF_TOKEN" in os.environ:
|
| 9 |
+
login(token=os.getenv("HF_TOKEN"))
|
| 10 |
|
| 11 |
# ================= CACHE THE MODEL =================
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model():
|
| 14 |
+
model_id = "ammoncoder123/IPTchatbotModel-1.7B"
|
| 15 |
|
| 16 |
quantization_config = BitsAndBytesConfig(
|
| 17 |
load_in_4bit=True,
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 22 |
+
# Ensure tokenizer has a chat template, or use a default one
|
| 23 |
+
if tokenizer.chat_template is None:
|
| 24 |
+
tokenizer.chat_template = "{% for message in messages %}{{'<|' + message['role'] + '|>' + '\n' + message['content'] + '</s>\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% endif %}"
|
| 25 |
+
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
model_id,
|
| 28 |
quantization_config=quantization_config,
|
|
|
|
| 34 |
return pipeline(
|
| 35 |
"text-generation",
|
| 36 |
model=model,
|
| 37 |
+
tokenizer=tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
pipe = load_model()
|
| 41 |
|
| 42 |
# ==================== CHAT INTERFACE ====================
|
| 43 |
st.title("IPT Chatbot Assistance")
|
| 44 |
+
st.info("Ask about logbooks, placement in Tanzania, or report writing.")
|
| 45 |
|
|
|
|
| 46 |
if "messages" not in st.session_state:
|
| 47 |
st.session_state.messages = []
|
| 48 |
|
| 49 |
+
# Display chat history
|
| 50 |
for message in st.session_state.messages:
|
| 51 |
+
if message["role"] != "system": # Don't show system prompt to user
|
| 52 |
+
with st.chat_message(message["role"]):
|
| 53 |
+
st.markdown(message["content"])
|
| 54 |
|
| 55 |
# User input
|
| 56 |
if prompt := st.chat_input("Ask about Industrial Practical Training..."):
|
|
|
|
| 57 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 58 |
with st.chat_message("user"):
|
| 59 |
st.markdown(prompt)
|
| 60 |
|
|
|
|
| 61 |
with st.chat_message("assistant"):
|
| 62 |
with st.spinner("Thinking..."):
|
| 63 |
+
# The context for the AI
|
| 64 |
+
system_message = {
|
| 65 |
+
"role": "system",
|
| 66 |
+
"content": "You are a helpful assistant for engineering and ICT students in Tanzania. "
|
| 67 |
+
"Answer questions about IPT, logbooks, and placements (e.g., TANESCO, TTCL, Halotel, TARURA). "
|
| 68 |
+
"If the question is unrelated to IPT, politely decline. Be concise."
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Prepare the template-ready messages
|
| 72 |
+
# We only send the system prompt and the current prompt to keep it simple,
|
| 73 |
+
# or you can send st.session_state.messages for full history.
|
| 74 |
+
input_messages = [system_message] + st.session_state.messages[-3:] # Last 3 messages for context
|
| 75 |
+
|
| 76 |
+
# Apply the chat template
|
| 77 |
+
formatted_prompt = pipe.tokenizer.apply_chat_template(
|
| 78 |
+
input_messages,
|
| 79 |
+
tokenize=False,
|
| 80 |
+
add_generation_prompt=True
|
| 81 |
+
)
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# Generate response
|
| 84 |
outputs = pipe(
|
| 85 |
+
formatted_prompt,
|
| 86 |
max_new_tokens=300,
|
| 87 |
temperature=0.7,
|
| 88 |
do_sample=True,
|
| 89 |
+
top_p=0.9,
|
| 90 |
+
return_full_text=False # THIS IS THE KEY FIX
|
| 91 |
)
|
| 92 |
|
| 93 |
+
response = outputs[0]["generated_text"].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
st.markdown(response)
|
| 95 |
|
|
|
|
| 96 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 97 |
|
|
|
|
| 98 |
if st.button("Clear Conversation"):
|
| 99 |
st.session_state.messages = []
|
| 100 |
st.rerun()
|