File size: 5,081 Bytes
303bdf3
 
 
 
 
 
 
 
 
 
3a1f34c
303bdf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import os

# ==========================================
# 1. CONFIGURATION
# ==========================================
# REPLACE with your actual Hugging Face Repo ID and Filename
MODEL_REPO = "simran40/BBSBEC-GGUF"  
MODEL_FILE = "BBSBEC.q4_k_m.gguf"       

print("⏳ System Startup: Checking Model...")
llm = None

try:
    # Check if model exists locally or download it
    model_path = hf_hub_download(
        repo_id=MODEL_REPO,
        filename=MODEL_FILE,
        cache_dir="./model_cache"
    )
    print(f"βœ… Model Found: {model_path}")
    
    # Initialize Llama.cpp Engine
    llm = Llama(
        model_path=model_path,
        n_ctx=2048,        # Context window size
        n_threads=2,       # CPU threads
        n_batch=512,       
        verbose=False      
    )
    print("βœ… Inference Engine Ready")

except Exception as e:
    print(f"❌ Load Error: {e}")
    print("⚠️ App starting in Safe Mode (Chat disabled).")


# ==========================================
# 2. PROMPT ENGINEERING (MATCHING TRAINING PIPELINE)
# ==========================================

# CRITICAL: This MUST match the System Prompt used in Cell 8 & 12 of your training notebook.
SYSTEM_IDENTITY = """You are the official AI Assistant for BABA BANDA SINGH BAHADUR ENGINEERING COLLEGE, FATEHGARH SAHIB.
Your role is to answer questions about B.Tech, M.Tech, BCA, MBA, exams, hostels, placements, and campus facilities.
You are helpful, polite, and strictly factual.
You are NOT a human. You do not have feelings."""

def format_prompt(history, message):
    """
    Constructs the prompt exactly as the model was fine-tuned.
    Format: Alpaca-Style
    """
    prompt_context = ""
    
    # Include recent chat history to allow follow-up questions
    if history:
        for turn in history[-2:]: # Keep last 2 turns
            if isinstance(turn, (list, tuple)) and len(turn) >= 2:
                user_msg = turn[0]
                bot_msg = turn[1]
                prompt_context += f"User: {str(user_msg)}\nAssistant: {str(bot_msg)}\n"
    
    # The Exact Prompt Structure needed for your Fine-Tuned Llama-3 Model
    full_prompt = (
        f"### Instruction:\n"
        f"{SYSTEM_IDENTITY}\n\n"
        
        f"### Previous Context:\n"
        f"{prompt_context}\n"
        
        f"### Current User Question:\n"
        f"{message}\n\n"
        
        f"### Response:\n"
    )
    return full_prompt

def chat_with_bot(message, history):
    # --- Safety Check ---
    if llm is None:
        yield "⚠️ **System Error:** Model not found. Please check MODEL_REPO in the code."
        return

    # --- Generate Response ---
    prompt = format_prompt(history, message)
    
    try:
        stream = llm(
            prompt,
            max_tokens=256,
            temperature=0.1,    # Low temp for factual accuracy (as tested in Cell 12)
            top_p=0.9,
            repeat_penalty=1.2, # Higher penalty to prevent loops
            stop=["###", "User:", "Assistant:", "<|end_of_text|>"],
            stream=True
        )
        
        response = ""
        for chunk in stream:
            text = chunk["choices"][0]["text"]
            response += text
            yield response
            
    except Exception as e:
        yield f"Error: {str(e)}"


# ==========================================
# 3. USER INTERFACE (BBSBEC BRANDING)
# ==========================================

custom_css = ".gradio-container {max-width: 800px; margin: auto;}"

with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="BBSBEC AI Assistant") as demo:
    gr.Markdown(
        """
        # 🏫 BBSBEC Fatehgarh Sahib Assistant
        
        I am the official AI for **Baba Banda Singh Bahadur Engineering College**. 
        Managed by **SGPC**. Affiliated with **IKGPTU**.
        
        **Ask me about:**
        * πŸŽ“ B.Tech, M.Tech, BCA, MBA Admissions
        * πŸ’° Fees & Scholarships
        * 🏨 Hostels (Baba Ajit Singh, Mata Gujri, etc.)
        * πŸ“ Exams (MSTs, Results) & Placements
        """
    )
    
    chatbot = gr.ChatInterface(
        fn=chat_with_bot,
        chatbot=gr.Chatbot(height=450, show_label=False),
        textbox=gr.Textbox(
            placeholder="E.g., What is the fee for B.Tech CSE?",
            container=False,
            scale=7
        ),
        examples=[
            "What is the eligibility for B.Tech CSE?",
            "Tell me about the hostel facilities.",
            "Do you offer BCA?",
            "How far is the college from the railway station?",
            "Is there a ragging free campus?"
        ],
        cache_examples=False,
    )
    
    gr.Markdown(
        """
        <div style="text-align: center; font-size: 0.8em; color: gray;">
        BBSBEC AI Assistant β€’ Powered by Llama-3.2-1B (Fine-Tuned)
        </div>
        """
    )

if __name__ == "__main__":
    demo.queue(max_size=5).launch(
        server_name="0.0.0.0",
        server_port=7860
    )