ammoncoder123 commited on
Commit
142dfe7
·
verified ·
1 Parent(s): 1d8da75

Update chatbot.py

Browse files
Files changed (1) hide show
  1. 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 with your HF token (secret in Space settings)
8
- login(token=os.getenv("HF_TOKEN"))
 
9
 
10
  # ================= CACHE THE MODEL =================
11
  @st.cache_resource
12
  def load_model():
13
- model_id = "ammoncoder123/IPTchatbotModel-1.7B" # Your correct repo
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("Answers may vary please verify important facts.")
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
- with st.chat_message(message["role"]):
51
- st.markdown(message["content"])
 
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
- # Strong system prompt (customized for your IPT dataset)
64
- system_prompt = """
65
- You are a helpful assistant for engineering and ICT students in Tanzania who are preparing for or doing Industrial Practical Training (IPT), also known as Industrial Attachment.
66
- IPT means Industrial Practical Training a mandatory work placement where students gain real-world experience in companies related to their field of study.
67
- Always answer questions about:
68
- - What IPT is
69
- - How to do IPT (logbook, daily/weekly reports, technical report, presentation)
70
- - Placement suggestions for different engineering fields (ICT, Mechatronics, Electrical, Mechanical, Civil, Biomedical, etc.)
71
- - Choosing IPT centers/companies
72
- - Tips for success in IPT
73
- - Any other directly related IPT topic
74
- If the question is clearly unrelated to IPT (e.g., politics, sports, personal life), politely reply:
75
- "Sorry, I can only help with questions about Industrial Practical Training (IPT). Please ask something related to IPT, logbook, placement, or reports."
76
- For placement suggestions (e.g., for Mechatronics, Electrical, ICT), give practical, realistic company types or industries in Tanzania that match the field.
77
- Be concise, accurate, and helpful.
78
- """
79
-
80
- # Build messages: system prompt first, then user prompt
81
- chat_messages = [
82
- {"role": "system", "content": system_prompt},
83
- {"role": "user", "content": prompt}
84
- ]
85
 
86
  # Generate response
87
  outputs = pipe(
88
- chat_messages,
89
  max_new_tokens=300,
90
  temperature=0.7,
91
  do_sample=True,
92
- top_p=0.9
 
93
  )
94
 
95
- # Extract and clean the generated text
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()