File size: 4,241 Bytes
156fb9c
fb13fa4
 
5362559
 
 
b64b83d
5362559
156fb9c
fb13fa4
156fb9c
 
b64b83d
156fb9c
fb13fa4
 
 
 
 
f928045
fb13fa4
 
 
f928045
fb13fa4
f928045
fb13fa4
 
 
 
 
 
 
 
 
 
 
 
156fb9c
 
fb13fa4
b64b83d
 
fb13fa4
b64b83d
156fb9c
 
 
fb13fa4
 
 
156fb9c
b64b83d
523e8fd
b64b83d
156fb9c
 
 
 
b64b83d
156fb9c
 
b64b83d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dd0272
 
b64b83d
1dd0272
 
 
 
 
b64b83d
1dd0272
 
 
 
 
 
 
 
b64b83d
fb13fa4
f928045
fb13fa4
156fb9c
b64b83d
1dd0272
156fb9c
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
import torch
from huggingface_hub import login
import os

# Force authentication with your HF token (secret in Space settings)
login(token=os.getenv("HF_TOKEN"))

# ================= CACHE THE MODEL =================
@st.cache_resource
def load_model():
    model_id = "ammoncoder123/IPTchatbotModel1-1.7B"  # Your correct repo

    quantization_config = BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_compute_dtype=torch.float16
    )

    tokenizer = AutoTokenizer.from_pretrained(model_id)
    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,
        max_new_tokens=300,
        temperature=0.7,
        do_sample=True,
        top_p=0.9
    )

pipe = load_model()

# ==================== CHAT INTERFACE ====================
st.title("IPT Chatbot Assistance")
st.info("Answers may vary — please verify important facts.")

# Display chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# User input
if prompt := st.chat_input("Ask about Industrial Practical Training..."):
    # Add user message to history and display
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Generate assistant response
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            # Strong system prompt (customized for your IPT dataset)
            system_prompt = """
            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.
            IPT means Industrial Practical Training — a mandatory work placement where students gain real-world experience in companies related to their field of study.
            Always answer questions about:
            - What IPT is
            - How to do IPT (logbook, daily/weekly reports, technical report, presentation)
            - Placement suggestions for different engineering fields (ICT, Mechatronics, Electrical, Mechanical, Civil, Biomedical, etc.)
            - Choosing IPT centers/companies
            - Tips for success in IPT
            - Any other directly related IPT topic
            If the question is clearly unrelated to IPT (e.g., politics, sports, personal life), politely reply:
            "Sorry, I can only help with questions about Industrial Practical Training (IPT). Please ask something related to IPT, logbook, placement, or reports."
            For placement suggestions (e.g., for Mechatronics, Electrical, ICT), give practical, realistic company types or industries in Tanzania that match the field.
            Be concise, accurate, and helpful.
            """

            # Build messages: system prompt first, then user prompt
            chat_messages = [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt}
            ]

            # Generate response
            outputs = pipe(
                chat_messages,
                max_new_tokens=300,
                temperature=0.7,
                do_sample=True,
                top_p=0.9
            )

            # Extract and clean the generated text
            response = outputs[0]["generated_text"]
            if isinstance(response, str) and response.startswith(prompt):
                response = response[len(prompt):].strip()

            # Show the response
            st.markdown(response)

    # Save assistant response to history
    st.session_state.messages.append({"role": "assistant", "content": response})

# Optional: Clear conversation button
if st.button("Clear Conversation"):
    st.session_state.messages = []
    st.rerun()