Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,224 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Step 1: Load the Question Answering Pipeline
|
5 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
|
|
|
|
6 |
|
7 |
+
# Step 2: Define the College Data
|
8 |
+
college_data = {
|
9 |
+
"name": "Government Boys Degree College Daulatpur",
|
10 |
+
"about": """
|
11 |
+
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.
|
12 |
+
At the beginning, the college had no staff of its own, so it was borrowed from Government Degree College Sakrand.
|
13 |
+
The college started its classes in the afternoon at Government High School Daulatpur as it had no specific building.
|
14 |
+
With the help of the EDO Education, the possession of the Government Girls High School (Old) Daulatpur was given to the college.
|
15 |
+
""",
|
16 |
+
"extended_about": """
|
17 |
+
Government Boys Degree College Daulatpur is one of the leading educational institutions in the region. It aims to provide high-quality education
|
18 |
+
and a conducive environment for academic excellence. The college offers various undergraduate programs and is committed to the intellectual
|
19 |
+
and professional growth of its students. It strives to nurture responsible citizens who contribute positively to society.
|
20 |
+
""",
|
21 |
+
"programs": ["ADSE-I", "ADSE-II", "ADAA-I", "ADAA-II", "BS (Zoology) (Proposed for this year)"],
|
22 |
+
"teachers": {
|
23 |
+
"Islamiat": ["Prof Mahfooz Khan (Associate Prof)", "M. Essa Samego (Lecturer)"],
|
24 |
+
"Physics": ["Muhammad Aslam Palli (Associate Prof)", "Asif Ali Rajput (Lecturer)"],
|
25 |
+
"Botany": ["Abdul Rahman Gaincho (Lecturer)"],
|
26 |
+
"Chemistry": ["Ali Aijaz Dahri (Lecturer)"],
|
27 |
+
"Math": ["Fida Hussain (Lecturer)"],
|
28 |
+
"Librarian": ["Zulfiqar Ali (Senior Librarian)"],
|
29 |
+
"DPE": ["Naseer Ahmed (DPE)"],
|
30 |
+
},
|
31 |
+
"staff": {
|
32 |
+
"clerks": ["Mr. Younis Unar (Senior Clerk)", "Mr. Muhammad Bughio (Senior Clerk)", "Mr. Dadan Khan (Junior Clerk)"],
|
33 |
+
"lab_assistants": ["Mr. Rahib Ali Unar (Lab Assistant)", "Mr. Shahbaz Khan (Lab Attendant)"],
|
34 |
+
"other": ["Mr. Peruaz Ali (Sanitary Worker)"],
|
35 |
+
},
|
36 |
+
"facilities": [
|
37 |
+
"Digital library with 20+ computers",
|
38 |
+
"Parks: Alquran Park and Botany Park with 50+ plants",
|
39 |
+
"Building: Two-floor building with 24 rooms",
|
40 |
+
"Labs: Zoology, Botany, Physics, Chemistry",
|
41 |
+
"Parking area",
|
42 |
+
"Solar system",
|
43 |
+
"RO Plant",
|
44 |
+
],
|
45 |
+
"principal": "Prof Irshad Ali Otho",
|
46 |
+
"rooms": 24,
|
47 |
+
"established": "2003",
|
48 |
+
"contact": {
|
49 |
+
"location": "National Highway N6 bypass Daulatpur",
|
50 |
+
"phone": "+92 300 3003249",
|
51 |
+
"email": "[email protected]",
|
52 |
+
"facebook": "https://www.facebook.com/share/19j9Z1iEvz/"
|
53 |
+
},
|
54 |
+
}
|
55 |
|
56 |
+
# Step 3: Custom Question Logic
|
57 |
+
def answer_question(question):
|
58 |
+
question = question.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
if "college name" in question or "what is the name" in question:
|
61 |
+
return f"The name of the college is {college_data['name']}."
|
62 |
+
|
63 |
+
elif "about college" in question or "tell me about the college" in question:
|
64 |
+
return f"About the college:\n{college_data['about']}\n\n{college_data['extended_about']}"
|
65 |
+
|
66 |
+
elif "program" in question or "course" in question:
|
67 |
+
return f"Programs offered:\n- " + "\n- ".join(college_data["programs"])
|
68 |
+
|
69 |
+
elif "teacher" in question or "faculty" in question or "lecturer" in question:
|
70 |
+
for subject, teachers in college_data["teachers"].items():
|
71 |
+
if subject.lower() in question:
|
72 |
+
return f"The {subject} lecturer(s): " + ", ".join(teachers)
|
73 |
+
return (
|
74 |
+
"Teachers:\n" + "\n".join(
|
75 |
+
[f"{subject}: " + ", ".join(teachers) for subject, teachers in college_data["teachers"].items()]
|
76 |
+
)
|
77 |
+
)
|
78 |
+
|
79 |
+
elif "room" in question:
|
80 |
+
return f"The college has {college_data['rooms']} rooms available."
|
81 |
+
|
82 |
+
elif "facility" in question:
|
83 |
+
return f"Facilities available:\n- " + "\n- ".join(college_data["facilities"])
|
84 |
+
|
85 |
+
elif "principal" in question:
|
86 |
+
return f"Our Principal is {college_data['principal']}."
|
87 |
+
|
88 |
+
elif "staff" in question or "clerk" in question:
|
89 |
+
clerks = "\n".join(college_data["staff"]["clerks"])
|
90 |
+
lab_assistants = "\n".join(college_data["staff"]["lab_assistants"])
|
91 |
+
other_staff = "\n".join(college_data["staff"]["other"])
|
92 |
+
return f"Clerks:\n{clerks}\n\nLab Assistants:\n{lab_assistants}\n\nOther Staff:\n{other_staff}"
|
93 |
+
|
94 |
+
elif "location" in question or "where" in question:
|
95 |
+
return f"The college is located at {college_data['contact']['location']}."
|
96 |
+
|
97 |
+
elif "contact" in question or "email" in question or "phone" in question:
|
98 |
+
contact_info = college_data["contact"]
|
99 |
+
return (
|
100 |
+
f"Contact Details:\n"
|
101 |
+
f"- Location: {contact_info['location']}\n"
|
102 |
+
f"- Phone: {contact_info['phone']}\n"
|
103 |
+
f"- Email: {contact_info['email']}\n"
|
104 |
+
f"- Facebook: {contact_info['facebook']}"
|
105 |
+
)
|
106 |
+
|
107 |
+
elif "establish" in question or "founded" in question or "start" in question:
|
108 |
+
return f"The college was established in {college_data['established']}."
|
109 |
+
|
110 |
+
else:
|
111 |
+
context = (
|
112 |
+
f"About the college: {college_data['about']}\n"
|
113 |
+
f"Programs offered: {', '.join(college_data['programs'])}\n"
|
114 |
+
f"Teachers: {', '.join([f'{subject}: ' + ', '.join(teachers) for subject, teachers in college_data['teachers'].items()])}\n"
|
115 |
+
f"Facilities: {', '.join(college_data['facilities'])}\n"
|
116 |
+
f"Principal: {college_data['principal']}\n"
|
117 |
+
f"Contact: {college_data['contact']['location']}, {college_data['contact']['phone']}, {college_data['contact']['email']}"
|
118 |
+
)
|
119 |
+
try:
|
120 |
+
result = qa_pipeline(question=question, context=context)
|
121 |
+
if result["score"] > 0.2:
|
122 |
+
return result["answer"]
|
123 |
+
except Exception:
|
124 |
+
pass
|
125 |
|
126 |
+
return (
|
127 |
+
"I'm sorry, I couldn't understand your question. You can ask about programs, teachers, facilities, or contact details."
|
128 |
+
)
|
129 |
|
130 |
+
# Step 4: Create Gradio Interface
|
131 |
+
with gr.Blocks() as app:
|
132 |
+
gr.HTML("""
|
133 |
+
<style>
|
134 |
+
body {
|
135 |
+
background-color: white;
|
136 |
+
color: black;
|
137 |
+
font-family: Arial, sans-serif;
|
138 |
+
}
|
139 |
+
.gradio-container {
|
140 |
+
background-color: white;
|
141 |
+
border-radius: 10px;
|
142 |
+
padding: 20px;
|
143 |
+
width: 80%;
|
144 |
+
margin: 0 auto;
|
145 |
+
}
|
146 |
+
.gr-textbox input {
|
147 |
+
background-color: #f0f0f0;
|
148 |
+
color: black;
|
149 |
+
}
|
150 |
+
.gr-button {
|
151 |
+
background-color: #007BFF;
|
152 |
+
color: white;
|
153 |
+
border-radius: 5px;
|
154 |
+
padding: 10px;
|
155 |
+
font-size: 16px;
|
156 |
+
}
|
157 |
+
.gr-button:hover {
|
158 |
+
background-color: #0056b3;
|
159 |
+
}
|
160 |
+
.gr-markdown {
|
161 |
+
font-size: 18px;
|
162 |
+
}
|
163 |
+
.gr-textbox {
|
164 |
+
font-size: 22px;
|
165 |
+
height: 150px;
|
166 |
+
}
|
167 |
+
.logo {
|
168 |
+
text-align: center;
|
169 |
+
margin-bottom: 20px;
|
170 |
+
}
|
171 |
+
.partition {
|
172 |
+
border-top: 2px solid #FFD700;
|
173 |
+
margin: 20px 0;
|
174 |
+
padding-top: 10px;
|
175 |
+
}
|
176 |
+
.question-answer-box {
|
177 |
+
transition: background-color 0.3s ease;
|
178 |
+
font-size: 22px;
|
179 |
+
padding: 20px;
|
180 |
+
border-radius: 8px;
|
181 |
+
height: 200px;
|
182 |
+
}
|
183 |
+
.question-answer-box:hover {
|
184 |
+
background-color: #e0f7fa;
|
185 |
+
}
|
186 |
+
.center-text {
|
187 |
+
text-align: center;
|
188 |
+
color: #007BFF;
|
189 |
+
}
|
190 |
+
</style>
|
191 |
+
""")
|
192 |
|
193 |
+
# Adding the logo (change the logo path as per your requirement)
|
194 |
+
gr.HTML("""
|
195 |
+
<div class="logo">
|
196 |
+
<img src="https://via.placeholder.com/150" alt="College Logo" />
|
197 |
+
</div>
|
198 |
+
""")
|
|
|
|
|
199 |
|
200 |
+
gr.Markdown("<h1 class='center-text' style='font-size: 36px;'>Government Boys Degree College Daulatpur</h1>")
|
|
|
201 |
|
202 |
+
# About Section
|
203 |
+
with gr.Row():
|
204 |
+
gr.Markdown("<h3 class='center-text'>About Us</h3>")
|
205 |
+
gr.Markdown(college_data["about"])
|
206 |
|
207 |
+
gr.HTML("<div class='partition'></div>")
|
208 |
+
|
209 |
+
# Contact Information
|
210 |
+
with gr.Row():
|
211 |
+
gr.Markdown("<h3 class='center-text'>Contact Information</h3>")
|
212 |
+
gr.Markdown(f"Location: {college_data['contact']['location']}\nPhone: {college_data['contact']['phone']}\nEmail: {college_data['contact']['email']}")
|
213 |
+
|
214 |
+
gr.HTML("<div class='partition'></div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
+
# Question Answering Section
|
217 |
+
with gr.Row():
|
218 |
+
gr.Markdown("<h3 class='center-text'>Ask Me About the College</h3>")
|
219 |
+
user_input = gr.Textbox(label="Ask your Question:", placeholder="e.g. What programs are offered?", interactive=True)
|
220 |
+
response_output = gr.Textbox(label="Answer", interactive=False, elem_classes="question-answer-box")
|
221 |
+
user_input.submit(answer_question, inputs=user_input, outputs=response_output)
|
222 |
|
223 |
+
# Launch the app
|
224 |
+
app.launch(share=True)
|