|
|
import gradio as gr |
|
|
from llama_cpp import Llama |
|
|
from huggingface_hub import hf_hub_download |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MODEL_REPO = "simran40/BBSBEC-GGUF" |
|
|
MODEL_FILE = "BBSBEC.q4_k_m.gguf" |
|
|
|
|
|
print("β³ System Startup: Checking Model...") |
|
|
llm = None |
|
|
|
|
|
try: |
|
|
|
|
|
model_path = hf_hub_download( |
|
|
repo_id=MODEL_REPO, |
|
|
filename=MODEL_FILE, |
|
|
cache_dir="./model_cache" |
|
|
) |
|
|
print(f"β
Model Found: {model_path}") |
|
|
|
|
|
|
|
|
llm = Llama( |
|
|
model_path=model_path, |
|
|
n_ctx=2048, |
|
|
n_threads=2, |
|
|
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).") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = "" |
|
|
|
|
|
|
|
|
if history: |
|
|
for turn in history[-2:]: |
|
|
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" |
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
if llm is None: |
|
|
yield "β οΈ **System Error:** Model not found. Please check MODEL_REPO in the code." |
|
|
return |
|
|
|
|
|
|
|
|
prompt = format_prompt(history, message) |
|
|
|
|
|
try: |
|
|
stream = llm( |
|
|
prompt, |
|
|
max_tokens=256, |
|
|
temperature=0.1, |
|
|
top_p=0.9, |
|
|
repeat_penalty=1.2, |
|
|
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)}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
) |