Spaces:
Running
Running
| 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 | |
| ) |