Download backend2.py from VAH6002/CBIT: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/spaces/VAH6002/CBIT/resolve/main/backend2.py
- Command line
-
hf download hf://spaces/VAH6002/CBIT/backend2.py
-
curl -L -o backend2.py https://huggingface.co/spaces/VAH6002/CBIT/resolve/main/backend2.py
2.56 kB
| import os | |
| import json | |
| import google.generativeai as genai | |
| # Load API Key from environment variables | |
| GOOGLE_GEMINI_API_KEY = "AIzaSyCg-nx7szAFMMPfCkBEyP2GHfq74ZJSBd4" | |
| genai.configure(api_key=GOOGLE_GEMINI_API_KEY) | |
| # Load admission knowledge base | |
| with open("cbit_admission_kb.json", "r") as f: | |
| admission_kb = json.load(f) | |
| # Load courses knowledge base | |
| with open("cbit_courses_kb.json", "r") as f: | |
| courses_kb = json.load(f) | |
| def get_course_info(query): | |
| """ | |
| Searches the knowledge base for UG, PG, and Diploma course details. | |
| """ | |
| query = query.lower() | |
| if "ug courses" in query or "btech" in query or "undergraduate" in query: | |
| return courses_kb["ug_courses"] | |
| elif "pg courses" in query or "mba" in query or "postgraduate" in query: | |
| return courses_kb["pg_courses"] | |
| elif "diploma" in query: | |
| return courses_kb["diploma_courses"] | |
| else: | |
| return "I'm sorry, I couldn't find specific course details. Please check the college's official website." | |
| def get_admission_info(query): | |
| """ | |
| Searches the knowledge base for admission-related details. | |
| """ | |
| query = query.lower() | |
| if "category b" in query: | |
| return admission_kb["category_b_admissions"] | |
| elif "apeapcet" in query: | |
| return admission_kb["apeapcet_2024_admissions"] | |
| elif "certificate" in query or "documents" in query: | |
| return admission_kb["certificates_required"] | |
| else: | |
| return "I'm sorry, I couldn't find specific details. Please check the college's official website." | |
| def generate_response(user_query, user_type="student"): | |
| """ | |
| Calls Google Gemini API to generate a hyper-personalized response. | |
| """ | |
| admission_info = get_admission_info(user_query) | |
| course_info = get_course_info(user_query) | |
| prompt = f""" | |
| User Type: {user_type} | |
| Query: {user_query} | |
| Admission Information: | |
| {admission_info} | |
| Course Information: | |
| {course_info} | |
| Provide a clear, friendly, and empathetic response. | |
| """ | |
| # Call Gemini API | |
| model = genai.GenerativeModel("gemini-pro") | |
| response = model.generate_content(prompt) | |
| return response.text if response else "Sorry, I couldn't generate a response." | |
| # Test the API | |
| if __name__ == "__main__": | |
| query = "Tell me about UG courses in Computer Science" | |
| print(generate_response(query, "student")) | |
| query = "What are the admission requirements for MBA?" | |
| print(generate_response(query, "parent")) | |
| query = "List diploma courses available" | |
| print(generate_response(query, "general")) | |