ammoncoder123 commited on
Commit
b64b83d
·
verified ·
1 Parent(s): 523e8fd

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +34 -16
chatbot.py CHANGED
@@ -4,13 +4,13 @@ import torch
4
  from huggingface_hub import login
5
  import os
6
 
7
- # Force authentication
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"
14
 
15
  quantization_config = BitsAndBytesConfig(
16
  load_in_4bit=True,
@@ -39,10 +39,10 @@ def load_model():
39
  pipe = load_model()
40
 
41
  # ==================== CHAT INTERFACE ====================
42
- st.title("IPTCHATBOT ASSISTANCE")
43
-
44
- st.info(" Answers may vary verify facts.")
45
 
 
46
  if "messages" not in st.session_state:
47
  st.session_state.messages = []
48
 
@@ -50,30 +50,40 @@ for message in st.session_state.messages:
50
  with st.chat_message(message["role"]):
51
  st.markdown(message["content"])
52
 
53
- # ... (model loading code above remains unchanged)
54
-
55
  if prompt := st.chat_input("Ask about Industrial Practical Training..."):
 
56
  st.session_state.messages.append({"role": "user", "content": prompt})
57
  with st.chat_message("user"):
58
  st.markdown(prompt)
59
 
 
60
  with st.chat_message("assistant"):
61
  with st.spinner("Thinking..."):
62
- # Define the system prompt here
63
- system_prompt = system_prompt = """
64
- You are a helpful assistant ONLY for questions about Industrial Practical Training (IPT) in Tanzania.
65
- IPT means Industrial Practical Training (Industrial Attachment) a mandatory work placement for students to gain real-world experience.
66
- Always use this correct meaning.
67
- If the question is not about IPT, logbooks, reports, placement, or related topics, reply exactly:
68
- "Sorry, I can only answer questions about Industrial Practical Training (IPT). Please ask something related."
 
 
 
 
 
 
 
 
69
  """
70
 
71
- # Build messages with system prompt first
72
  chat_messages = [
73
  {"role": "system", "content": system_prompt},
74
  {"role": "user", "content": prompt}
75
  ]
76
 
 
77
  outputs = pipe(
78
  chat_messages,
79
  max_new_tokens=300,
@@ -82,10 +92,18 @@ if prompt := st.chat_input("Ask about Industrial Practical Training..."):
82
  top_p=0.9
83
  )
84
 
 
85
  response = outputs[0]["generated_text"]
86
  if isinstance(response, str) and response.startswith(prompt):
87
  response = response[len(prompt):].strip()
88
 
 
89
  st.markdown(response)
90
 
91
- st.session_state.messages.append({"role": "assistant", "content": response})
 
 
 
 
 
 
 
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/IPTchatbotModel1-1.7B" # Your correct repo
14
 
15
  quantization_config = BitsAndBytesConfig(
16
  load_in_4bit=True,
 
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
 
 
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,
 
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()