Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import google.generativeai as genai | |
| import os | |
| # 1. Configure Gemini API | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") | |
| if GEMINI_API_KEY: | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| # 2. Detailed University Information | |
| NIELIT_INFO = """ | |
| You are the official AI Assistant for NIELIT (National Institute of Electronics & Information Technology) Deemed to be University. | |
| UNIVERSITY STATUS: | |
| - Autonomous scientific society under MeitY, Govt of India. | |
| - Conferred "Deemed to be University" status under Section 3 of the UGC Act, 1956. | |
| FEE STRUCTURE (Session 2025-26/2026): | |
| - Admission Fee: ₹5,000 (One-time) | |
| - Security Deposit: ₹5,000 (Refundable) | |
| - Academic Fee: ₹5,000 (Per odd semester) | |
| - Tuition Fee (Per Semester): | |
| * B.Tech: ₹50,000 | |
| * M.Tech: ₹60,000 | |
| * MCA: ₹32,000 | |
| * BCA: ₹30,000 | |
| * Diploma: ₹27,000 | |
| * PhD: ₹40,000 | |
| - Note: Tuition & Exam fees are waived for SC/ST candidates as per Govt. rules. | |
| IMPORTANT DATES (January 2026 Cycle): | |
| - Theory Exams (O/A/B/C Level): Jan 10 – Jan 18, 2026. | |
| - Practical Exams: Starting Feb 7, 2026. | |
| - DLC (CCC/BCC) Jan 2026 Exam: Postponed to Feb 23–27, 2026. | |
| GUIDELINES: | |
| 1. Use a professional, student-friendly tone. | |
| 2. If asked about fees or dates not listed here, provide the specific 2026 data mentioned above. | |
| 3. Always encourage students to verify at https://student.nielit.gov.in for their specific admit cards. | |
| """ | |
| # --- ROBUST MODEL SELECTOR (Prevents 404 Errors) --- | |
| def get_working_model(): | |
| """Finds a model that actually exists for your key""" | |
| try: | |
| # Get all available models | |
| available = [m.name for m in genai.list_models() if 'generateContent' in m.supported_generation_methods] | |
| # Priority list (Fastest -> Best -> Fallback) | |
| priorities = [ | |
| "models/gemini-1.5-flash", | |
| "models/gemini-1.5-pro", | |
| "models/gemini-1.0-pro", | |
| "models/gemini-pro" | |
| ] | |
| # Pick the first priority that exists in your available list | |
| for model in priorities: | |
| if model in available: | |
| return model | |
| # If no priority match, just take the first available one | |
| return available[0] if available else "models/gemini-pro" | |
| except Exception: | |
| # Emergency fallback | |
| return "gemini-pro" | |
| def get_chatbot_response(message, history): | |
| # 1. Handle Empty/Greeting locally (Zero Latency) | |
| if not message or not message.strip(): | |
| return "" | |
| greetings = ["hi", "hello", "hey", "hlo", "namaste", "hola"] | |
| if message.lower().strip() in greetings: | |
| return "Hello! 👋 I am the NIELIT University AI Assistant. How can I help you with admissions, fees, or exam details today?" | |
| if not GEMINI_API_KEY: | |
| return "⚠️ System Error: GEMINI_API_KEY is missing." | |
| try: | |
| # 2. Get a working model dynamically | |
| model_name = get_working_model() | |
| # 3. Initialize | |
| model = genai.GenerativeModel( | |
| model_name=model_name, | |
| system_instruction=NIELIT_INFO | |
| ) | |
| # 4. Format History | |
| chat_history = [] | |
| for human, assistant in history: | |
| chat_history.append({"role": "user", "parts": [human]}) | |
| chat_history.append({"role": "model", "parts": [assistant]}) | |
| # 5. Generate | |
| chat = model.start_chat(history=chat_history) | |
| response = chat.send_message(message) | |
| return response.text | |
| except Exception as e: | |
| return f"❌ Connection Error: {str(e)}" | |
| # 3. Custom CSS | |
| custom_css = """ | |
| #header { text-align: center; background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%); padding: 20px; border-radius: 10px; margin-bottom: 20px; color: white; } | |
| .message-wrap { border-radius: 10px !important; } | |
| footer { text-align: center; margin-top: 20px; color: #666; font-size: 0.9em; } | |
| """ | |
| # 4. UI Layout | |
| with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: | |
| with gr.Row(elem_id="header"): | |
| gr.HTML("<h1>🎓 NIELIT University AI Support</h1><p>Admissions • Fees • Exams • 2026 Session</p>") | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| # --- AVATAR CONFIGURATION HERE --- | |
| # (None, "bot.png") means: User gets default icon, Bot gets "bot.png" | |
| chatbot = gr.Chatbot( | |
| height=500, | |
| show_label=False, | |
| avatar_images=(None, "https://cdn-uploads.huggingface.co/production/uploads/6474405f90330355db146c76/dOtPhPMj5nk3SEceM_3Iq.png") | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox(placeholder="Ask about B.Tech fees, CCC exams, or admissions...", scale=4, container=False) | |
| submit = gr.Button("Send", variant="primary", scale=1) | |
| clear = gr.Button("🗑️ Clear Conversation") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### 💡 FAQ") | |
| q1 = gr.Button("What are the B.Tech fees?", size="sm") | |
| q2 = gr.Button("When are the Jan 2026 exams?", size="sm") | |
| q3 = gr.Button("Is there a fee waiver for SC/ST?", size="sm") | |
| gr.Markdown("---") | |
| gr.Markdown("### 🔗 Links\n- [Student Portal](https://student.nielit.gov.in)\n- [Official Site](https://nielit.gov.in)") | |
| # 5. Logic | |
| def respond(message, chat_history): | |
| if chat_history is None: chat_history = [] | |
| bot_message = get_chatbot_response(message, chat_history) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| # *** API ENDPOINT EXPOSED HERE *** | |
| submit.click(respond, [msg, chatbot], [msg, chatbot], api_name="chat_with_bot") | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| # Quick Links | |
| q1.click(lambda: "What is the fee structure for B.Tech programs?", outputs=msg).then(respond, [msg, chatbot], [msg, chatbot]) | |
| q2.click(lambda: "What are the exam dates for January 2026?", outputs=msg).then(respond, [msg, chatbot], [msg, chatbot]) | |
| q3.click(lambda: "Do SC/ST students get a fee waiver?", outputs=msg).then(respond, [msg, chatbot], [msg, chatbot]) | |
| gr.HTML("<footer>Powered by Google Gemini | Official NIELIT Deemed University Assistant</footer>") | |
| if __name__ == "__main__": | |
| demo.launch() |