Download app.py from Anisha007/nlppesbot: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/Anisha007/nlppesbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Anisha007/nlppesbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Anisha007/nlppesbot/resolve/main/app.py
5.14 kB
| import torch | |
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForQuestionAnswering | |
| # β Device setup | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| # β Section-wise context data | |
| contexts = { | |
| "1. B.Tech Regular": """For Bachelor of Technology (B.Tech), admissions can be through: | |
| - KCET entrance exam and counselling, | |
| - JEE Main or KCET scores via PES University counselling, | |
| - Higher Fee Category (Management Quota) by contacting the admissions office. | |
| Eligibility requires: | |
| - Minimum 60% aggregate in Physics, Chemistry, and Mathematics (PCM) in 10+2 or equivalent, | |
| - A valid JEE Main or KCET score. | |
| JEE Main Quota Fee Structure (per annum): | |
| - Computer Science and Engineering (CSE): INR 5,00,000 | |
| - CSE (Artificial Intelligence & Machine Learning β AIML): INR 5,00,000 | |
| - Electronics and Communication Engineering (ECE): INR 4,50,000 | |
| - Mechanical Engineering (ME): INR 4,00,000 | |
| - Electrical and Electronics Engineering (EEE): INR 4,00,000 | |
| - Biotechnology (BT): INR 4,00,000 | |
| For Higher Fee Category / Management / NRI / Foreign / Sponsored / OCI / PIO seats: | |
| - Please contact the Office of Admissions or write to admissions@pes.edu | |
| Other Charges: | |
| - One-time non-refundable administrative fee: INR 10,000""", | |
| "2. B.Tech Lateral Entry": """For B.Tech Diploma Lateral Entry, admission is possible through DCET entrance | |
| and counselling or PES University Entrance Test. Eligibility requires 10+3 Diploma with minimum 60% | |
| from a DTE-affiliated institution.""", | |
| "3. BCA": """For Bachelor of Computer Applications (BCA), admission is through registration, PES Entrance | |
| Test (online interview) with minimum 50% aggregate in 10+2 and Mathematics as a compulsory subject. | |
| Registration fee is typically INR 1,200. Counseling, verification, and fee payment complete the admission process.""", | |
| "4. M.Tech": """Master of Technology (M.Tech) Admissions | |
| Admission Pathways | |
| Gaining admission to the M.Tech program at PES University involves a few steps: | |
| Direct Application: Candidates need to register for the relevant program on the PES University admissions portal and complete the process to secure admission: | |
| PES Entrance Test: This is an online interview process candidates must attend. You'll need to have your educational transcripts, any supporting work or documents, and certifications ready to strengthen your application. Results are typically shared within three days of the interview. If selected, you'll receive further admission details. | |
| PGCET Quota: This admission method is available for eligible candidates. | |
| Valid GATE Score: If you have a valid GATE score, you should register on the PES University admissions portal and proceed with the application accordingly. | |
| Eligibility Criteria | |
| To be eligible for M.Tech admission, you must have at least 50% in an Engineering degree in a relevant stream from a UGC recognized University.""" | |
| } | |
| # β Load available public models only | |
| models = { | |
| "RoBERTa (deepset)": { | |
| "tokenizer": AutoTokenizer.from_pretrained("deepset/roberta-base-squad2"), | |
| "model": AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2").to(device) | |
| }, | |
| "DistilBERT QA (fast)": { | |
| "tokenizer": AutoTokenizer.from_pretrained("distilbert-base-cased-distilled-squad"), | |
| "model": AutoModelForQuestionAnswering.from_pretrained("distilbert-base-cased-distilled-squad").to(device) | |
| }, | |
| "Electra QA": { | |
| "tokenizer": AutoTokenizer.from_pretrained("deepset/electra-base-squad2"), | |
| "model": AutoModelForQuestionAnswering.from_pretrained("deepset/electra-base-squad2").to(device) | |
| } | |
| } | |
| # β QA Logic | |
| def answer_question(model_choice, section_choice, question): | |
| context = contexts.get(section_choice) | |
| if not context or not question.strip(): | |
| return "β Please select a valid section and enter your question." | |
| tokenizer = models[model_choice]["tokenizer"] | |
| model = models[model_choice]["model"] | |
| inputs = tokenizer(question, context, return_tensors="pt", max_length=512, truncation=True, padding=True) | |
| inputs = {k: v.to(device) for k, v in inputs.items()} | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| start = torch.argmax(outputs.start_logits) | |
| end = torch.argmax(outputs.end_logits) + 1 | |
| tokens = inputs["input_ids"][0][start:end] | |
| answer = tokenizer.decode(tokens, skip_special_tokens=True).strip() | |
| if not answer or answer.lower() in ["[cls]", "[sep]"]: | |
| return "β οΈ No clear answer found. Try rephrasing your question." | |
| return f"β {model_choice} says:\n\n{answer}" | |
| # β Gradio Interface | |
| iface = gr.Interface( | |
| fn=answer_question, | |
| inputs=[ | |
| gr.Radio(list(models.keys()), label="Choose QA Model"), | |
| gr.Dropdown(list(contexts.keys()), label="Select Section"), | |
| gr.Textbox(lines=2, label="Your Question", placeholder="e.g., What is the eligibility for B.Tech?") | |
| ], | |
| outputs="text", | |
| title="π PES Admission Q&A Bot", | |
| description="Ask admission-related questions (B.Tech, M.Tech, BCA, etc.). Choose your QA model. Works fully offline." | |
| ) | |
| iface.launch() | |