MY_College / app.py
Musawir19's picture
Update app.py
cc8bb84 verified
import gradio as gr
from transformers import pipeline
# Step 1: Load the Question Answering Pipeline
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Step 2: Define the College Data
college_data = {
"name": "Government Boys Degree College Daulatpur",
"about": """
The college was notified to work from 16th July 2003 on the directions of the then care-taker Minister for Education, Mr. Khan Muhammad Dahri.
At the beginning, the college had no staff of its own, so it was borrowed from Government Degree College Sakrand.
The college started its classes in the afternoon at Government High School Daulatpur as it had no specific building.
With the help of the EDO Education, the possession of the Government Girls High School (Old) Daulatpur was given to the college.
""",
"extended_about": """
Government Boys Degree College Daulatpur is one of the leading educational institutions in the region. It aims to provide high-quality education
and a conducive environment for academic excellence. The college offers various undergraduate programs and is committed to the intellectual
and professional growth of its students. It strives to nurture responsible citizens who contribute positively to society.
""",
"programs": ["ADSE-I", "ADSE-II", "ADAA-I", "ADAA-II", "BS (Zoology) (Proposed for this year)"],
"teachers": {
"Islamiat": ["Prof Mahfooz Khan (Associate Prof)", "M. Essa Samego (Lecturer)"],
"Physics": ["Muhammad Aslam Palli (Associate Prof)", "Asif Ali Rajput (Lecturer)"],
"Botany": ["Abdul Rahman Gaincho (Lecturer)"],
"Chemistry": ["Ali Aijaz Dahri (Lecturer)"],
"Math": ["Fida Hussain (Lecturer)"],
"Librarian": ["Zulfiqar Ali (Senior Librarian)"],
"DPE": ["Naseer Ahmed (DPE)"],
},
"staff": {
"clerks": ["Mr. Younis Unar (Senior Clerk)", "Mr. Muhammad Bughio (Senior Clerk)", "Mr. Dadan Khan (Junior Clerk)"],
"lab_assistants": ["Mr. Rahib Ali Unar (Lab Assistant)", "Mr. Shahbaz Khan (Lab Attendant)"],
"other": ["Mr. Peruaz Ali (Sanitary Worker)"],
},
"facilities": [
"Digital library with 20+ computers",
"Parks: Alquran Park and Botany Park with 50+ plants",
"Building: Two-floor building with 24 rooms",
"Labs: Zoology, Botany, Physics, Chemistry",
"Parking area",
"Solar system",
"RO Plant",
],
"principal": "Prof Irshad Ali Otho",
"rooms": 24, # Replaced 'classes' with 'rooms'
"established": "2003",
"contact": {
"location": "National Highway N6 bypass Daulatpur",
"phone": "+92 300 3003249",
"email": "[email protected]",
"facebook": "https://www.facebook.com/share/19j9Z1iEvz/"
},
}
# Step 3: Custom Question Logic
def answer_question(question):
# Lowercase the question for simpler matching
question = question.lower()
# Rule-based answers for specific types of questions
if "college name" in question or "what is the name" in question:
return f"The name of the college is {college_data['name']}."
elif "about college" in question or "tell me about the college" in question:
return f"About the college:\n{college_data['about']}\n\n{college_data['extended_about']}"
elif "program" in question or "course" in question:
return f"Programs offered:\n- " + "\n- ".join(college_data["programs"])
elif "teacher" in question or "faculty" in question or "lecturer" in question:
for subject, teachers in college_data["teachers"].items():
if subject.lower() in question:
return f"The {subject} lecturer(s): " + ", ".join(teachers)
return (
"Teachers:\n" + "\n".join(
[f"{subject}: " + ", ".join(teachers) for subject, teachers in college_data["teachers"].items()]
)
)
elif "room" in question:
return f"The college has {college_data['rooms']} rooms available."
elif "facility" in question:
return f"Facilities available:\n- " + "\n- ".join(college_data["facilities"])
elif "principal" in question:
return f"Our Principal is {college_data['principal']}."
elif "staff" in question or "clerk" in question:
clerks = "\n".join(college_data["staff"]["clerks"])
lab_assistants = "\n".join(college_data["staff"]["lab_assistants"])
other_staff = "\n".join(college_data["staff"]["other"])
return f"Clerks:\n{clerks}\n\nLab Assistants:\n{lab_assistants}\n\nOther Staff:\n{other_staff}"
elif "location" in question or "where" in question:
return f"The college is located at {college_data['contact']['location']}."
elif "contact" in question or "email" in question or "phone" in question:
contact_info = college_data["contact"]
return (
f"Contact Details:\n"
f"- Location: {contact_info['location']}\n"
f"- Phone: {contact_info['phone']}\n"
f"- Email: {contact_info['email']}\n"
f"- Facebook: {contact_info['facebook']}"
)
elif "establish" in question or "founded" in question or "start" in question:
return f"The college was established in {college_data['established']}."
else:
# Fallback: Use NLP model
context = (
f"About the college: {college_data['about']}\n"
f"Programs offered: {', '.join(college_data['programs'])}\n"
f"Teachers: {', '.join([f'{subject}: ' + ', '.join(teachers) for subject, teachers in college_data['teachers'].items()])}\n"
f"Facilities: {', '.join(college_data['facilities'])}\n"
f"Principal: {college_data['principal']}\n"
f"Contact: {college_data['contact']['location']}, {college_data['contact']['phone']}, {college_data['contact']['email']}"
)
try:
result = qa_pipeline(question=question, context=context)
if result["score"] > 0.2:
return result["answer"]
except Exception:
pass
# If everything fails
return (
"I'm sorry, I couldn't understand your question. You can ask about programs, teachers, facilities, or contact details."
)
# Step 4: Create Gradio Interface
with gr.Blocks() as app:
gr.Image(value="college_logo.png", label="College Logo", elem_id="logo", interactive=False, show_download_button=False)
# Add other components like text, buttons, etc.
gr.Textbox(label="Ask about the college:")
gr.Button("Submit")
# Section for 'About the College'
with gr.Row():
gr.Markdown("### About the College")
about_college = gr.Textbox(value=f"{college_data['about']}\n\n{college_data['extended_about']}",
label="About the College", interactive=False)
# Section for 'Contact Us'
with gr.Row():
gr.Markdown("### Contact Us")
contact_info = gr.Textbox(value=f"Location: {college_data['contact']['location']}\n"
f"Phone: {college_data['contact']['phone']}\n"
f"Email: {college_data['contact']['email']}\n"
f"Facebook: {college_data['contact']['facebook']}",
label="Contact Info", interactive=False)
# Question and Answer section
question = gr.Textbox(label="Ask a question about the college")
answer = gr.Textbox(label="Answer")
question.submit(answer_question, inputs=question, outputs=answer)
# Step 5: Launch the App
app.launch()