Musawir19 commited on
Commit
1cec144
·
verified ·
1 Parent(s): 6c2aadb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +269 -49
app.py CHANGED
@@ -1,64 +1,284 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
4
  """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
 
 
 
 
 
 
 
 
6
  """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
41
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
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
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ # Step 2: Import libraries
2
  import gradio as gr
3
+ from transformers import pipeline
4
 
5
+ # Step 3: Define College Data
6
+ college_about = """
7
+ 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.
8
+ At the beginning, the college had no staff of its own, so it was borrowed from Government Degree College Sakrand.
9
+ The college started its classes in the afternoon at Government High School Daulatpur as it had no specific building.
10
+ With the help of the EDO Education, the possession of the Government Girls High School (Old) Daulatpur was given to the college.
11
  """
12
+
13
+ faculty_data = """
14
+ 1. Prof Mahfooz Khan (Associate Prof, Islamiat)
15
+ 2. Muhammad Aslam Palli (Associate Prof, Physics)
16
+ 3. Abdul Rahman Gaincho (Botany)
17
+ 4. M. Essa Samego (Lecturer, Islamiat)
18
+ 5. Ali Aijaz Dahri (Lecturer, Chemistry)
19
+ 6. Asif Ali Rajput (Lecturer, Physics)
20
+ 7. Fida Hussain (Lecturer, Math)
21
+ 8. Zulfiqar Ali (Senior Librarian)
22
  """
 
23
 
24
+ facilities = """
25
+ Facilities available:
26
+ 1. Digital library with 20+ computers
27
+ 2. Parks: Alquran Park and Botany Park with 50+ plants
28
+ 3. Labs: Zoology, Botany, Physics, Chemistry
29
+ 4. Parking area
30
+ 5. Solar system and RO Plant
31
+ """
32
 
33
+ # Step 4: Define College Dataset (from your data dictionary)
34
+ # Define the dataset
35
+ data = {
36
+ "Questions": [
37
+ "tell me name of college?",
38
+ "tell me establish of college?",
39
+ "who is principal of college?",
40
+ "what a programs in college?",
41
+ "what a programs in college?",
42
+ "what a programs in college?",
43
+ "what a programs in college?",
44
+ "what a programs in college?",
45
+ "where is located?",
46
+ "islamiat teacher?",
47
+ "physics teacher?",
48
+ "botany teacher?",
49
+ "islamiat teacher?",
50
+ "chemistry teacher?",
51
+ "physics teacher?",
52
+ "math teacher?",
53
+ "who is librarian",
54
+ "who is DPE",
55
+ "lab assistant?",
56
+ "clerk?",
57
+ "clerk?",
58
+ "clerk?",
59
+ "lab assistant?",
60
+ "lab assistant?",
61
+ "lab assistant?",
62
+ "who is sanitary worker?",
63
+ "facilities?",
64
+ "facilities?",
65
+ "facilities?",
66
+ "facilities?",
67
+ "facilities?",
68
+ "facilities?",
69
+ "facilities?",
70
+ "facilities?",
71
+ "facilities?",
72
+ "facilities?",
73
+ "facilities?",
74
+ ],
75
+ "Category": [
76
+ "College Name",
77
+ "Established",
78
+ "Principal",
79
+ "Programs",
80
+ "Programs",
81
+ "Programs",
82
+ "Programs",
83
+ "Programs",
84
+ "Location",
85
+ "Teaching Staff",
86
+ "Teaching Staff",
87
+ "Teaching Staff",
88
+ "Teaching Staff",
89
+ "Teaching Staff",
90
+ "Teaching Staff",
91
+ "Teaching Staff",
92
+ "Teaching Staff",
93
+ "Teaching Staff",
94
+ "Non-Teaching Staff",
95
+ "Non-Teaching Staff",
96
+ "Non-Teaching Staff",
97
+ "Non-Teaching Staff",
98
+ "Non-Teaching Staff",
99
+ "Non-Teaching Staff",
100
+ "Non-Teaching Staff",
101
+ "Non-Teaching Staff",
102
+ "Facilities",
103
+ "Facilities",
104
+ "Facilities",
105
+ "Facilities",
106
+ "Facilities",
107
+ "Facilities",
108
+ "Facilities",
109
+ "Facilities",
110
+ "Facilities",
111
+ "Facilities",
112
+ "Facilities",
113
+ ],
114
+ "Name": [
115
+ "Government Boys Degree College Daulatpur",
116
+ "2003",
117
+ "Prof Irshad Ali Otho",
118
+ "ADSE-I",
119
+ "ADSE-II",
120
+ "ADAA-",
121
+ "ADAA",
122
+ "(Zoology),Proposed this year,",
123
+ "Bypass Daulatpur",
124
+ "Prof Mahfooz Khan",
125
+ "Muhammad Aslam Palli",
126
+ "Abdul Rahman Gaincho",
127
+ "M. Essa Samego",
128
+ "Ali Aijaz Dahri",
129
+ "Asif Ali Rajput",
130
+ "Fida Hussain",
131
+ "Zulfiqar Ali",
132
+ "Naseer Ahmed",
133
+ "Mr Abear Lund",
134
+ "Mr Younis Unar",
135
+ "Mr Muhammad Bughio",
136
+ "Mr Dadan Khan,",
137
+ "Mr Rahib Ali Unar",
138
+ "Mr M Saleem Unar",
139
+ "Mr Shahbaz Khan",
140
+ "Mr Peruaz Ali",
141
+ "Digital Library",
142
+ "Parks",
143
+ "Building",
144
+ "Zoology Lab",
145
+ "Botany Lab",
146
+ "Physics Lab",
147
+ "Chemistry Lab",
148
+ "Parking Area",
149
+ "Solar System",
150
+ "RO Plant",
151
+ ],
152
+ "Designation": [
153
+ "NA",
154
+ "NA",
155
+ "NA",
156
+ "NA",
157
+ "NA",
158
+ "NA",
159
+ "NA",
160
+ "NA",
161
+ "NA",
162
+ "Associate Professor",
163
+ "Associate Professor",
164
+ "Lecturer",
165
+ "Lecturer",
166
+ "Lecturer",
167
+ "Lecturer",
168
+ "Lecturer",
169
+ "Senior Librarian",
170
+ "DPE",
171
+ "Assistant",
172
+ "Senior Clerk",
173
+ "Senior Clerk",
174
+ "Junior Clerk",
175
+ "Lab Assistant",
176
+ "Lab Attendant",
177
+ "Lab Attendant",
178
+ "Sanitary Worker",
179
+ "NA",
180
+ "NA",
181
+ "NA",
182
+ "NA",
183
+ "NA",
184
+ "NA",
185
+ "NA",
186
+ "NA",
187
+ "NA",
188
+ "NA",
189
+ ],
190
+ "Subject/Details": [
191
+ "NA",
192
+ "NA",
193
+ "NA",
194
+ "NA",
195
+ "NA",
196
+ "NA",
197
+ "NA",
198
+ "NA",
199
+ "NA",
200
+ "Islamiat",
201
+ "Physics",
202
+ "Botany",
203
+ "Islamiat",
204
+ "Chemistry",
205
+ "Physics",
206
+ "Math",
207
+ "NA",
208
+ "NA",
209
+ "NA",
210
+ "NA",
211
+ "NA",
212
+ "NA",
213
+ "NA",
214
+ "NA",
215
+ "NA",
216
+ "NA",
217
+ "20+ Computers",
218
+ "Alquran Park and Botany Park with 50+ plats",
219
+ "Two-floor building with 24 rooms",
220
+ "Available",
221
+ "Available",
222
+ "Available",
223
+ "Available",
224
+ "Available",
225
+ "Available",
226
+ "Available",
227
+ ],
228
+ }
229
 
230
+ # Print the data (optional)
231
+ #print(data)
 
 
 
232
 
 
233
 
234
+ # Step 5: Define a Question-Answering Model (Hugging Face)
235
+ qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
236
 
237
+ # Helper function for answering dataset questions
238
+ def get_predefined_answer(question):
239
+ for q, ans in zip(data["Questions"], data["Answers"]):
240
+ if question.lower() in q.lower():
241
+ return ans
242
+ return "Sorry, I don't have an answer for that. Please ask a different question."
 
 
243
 
244
+ # Define answer query function with dataset context
245
+ def answer_query(question):
246
+ predefined_answer = get_predefined_answer(question)
247
+ if predefined_answer != "Sorry, I don't have an answer for that. Please ask a different question.":
248
+ return predefined_answer
249
+ else:
250
+ # Fallback to model-based answer if question is not predefined
251
+ context = f"{college_about}\n\n{faculty_data}\n\n{facilities}"
252
+ response = qa_model(question=question, context=context)
253
+ return response['answer']
254
 
255
+ # Step 6: Create Gradio Interface
256
+ with gr.Blocks(theme=gr.themes.Monochrome()) as app:
257
+ gr.Markdown("## Government Boys Degree College Daulatpur")
258
+ gr.Markdown("### Welcome to our College Information RAG App")
259
+ gr.Image("college_logo.png", label="College Logo") # Upload your logo as college_logo.png
260
 
261
+ with gr.Tabs():
262
+ with gr.Tab("About College"):
263
+ gr.Markdown(college_about)
264
+ with gr.Tab("Faculty and Staff"):
265
+ gr.Markdown(faculty_data)
266
+ with gr.Tab("Facilities"):
267
+ gr.Markdown(facilities)
268
+ with gr.Tab("Ask a Question"):
269
+ question = gr.Textbox(label="Ask about the college")
270
+ answer = gr.Textbox(label="Answer")
271
+ question.submit(answer_query, inputs=question, outputs=answer)
272
+ with gr.Tab("Predefined Data"):
273
+ predefined_data = "\n".join([f"{q} - Category: {cat}, Name: {name}, Designation: {desig}, Details: {details}"
274
+ for q, cat, name, desig, details in zip(data["Questions"], data["Category"], data["Name"], data["Designation"], data["Subject/Details"])])
275
+ gr.Markdown(predefined_data)
276
+ with gr.Tab("Contact Us"):
277
+ gr.Markdown("**Contact Details:**\n- Location: National Highway N6 bypass Daulatpur\n- Phone: +92 300 3003249\n- Email: [email protected]")
278
+ gr.Markdown("[Visit our Facebook Page](https://www.facebook.com/share/19j9Z1iEvz/)")
279
+
280
+ gr.Markdown("### Made by Musawir Manzoor")
281
 
282
+ # Step 7: Launch the App
283
+ app.launch()
284