nes470 commited on
Commit
c3a942f
1 Parent(s): 55c90bf

Upload folder using huggingface_hub

Browse files
.DS_Store ADDED
Binary file (8.2 kB). View file
 
.gitattributes CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ resources/small.guesstrain.json filter=lfs diff=lfs merge=lfs -text
37
+ resources/wiki_page_text.json filter=lfs diff=lfs merge=lfs -text
38
+ resources/wiki_text_16.json filter=lfs diff=lfs merge=lfs -text
39
+ resources/wiki_text_2.json filter=lfs diff=lfs merge=lfs -text
QBModelConfig.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ import torch
3
+
4
+ class QBModelConfig(PretrainedConfig):
5
+ model_type = 'QA-umd-quizbowl'
6
+
7
+ def __init__(self, **kwargs):
8
+ self.torch_dtype = torch.float16
9
+ super().__init__( **kwargs)
10
+
11
+
QBModelWrapper.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from transformers import PreTrainedModel
3
+ from transformers import PretrainedConfig
4
+ from QBModelConfig import QBModelConfig
5
+ from qbmodel import QuizBowlModel
6
+
7
+ class QBModelWrapper(PreTrainedModel):
8
+ config_class= QBModelConfig
9
+
10
+
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+
14
+ self.model = QuizBowlModel()
15
+ self.tfmodel = self.model.guesser
16
+
17
+
18
+
19
+ def forward(self, question):
20
+ return self.model.guess_and_buzz(question)
QBModelWrapperCopy.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from transformers import PreTrainedModel
3
+ from transformers import PretrainedConfig
4
+ from QBModelConfig import QBModelConfig
5
+ from qbmodel import QuizBowlModel
6
+
7
+ class QBModelWrapper(PreTrainedModel):
8
+ config_class= QBModelConfig
9
+
10
+
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+
14
+ self.model = QuizBowlModel()
15
+ self.tfmodel = self.model.guesser
16
+
17
+
18
+ def forward(self, question, context):
19
+ output = self.model.guess_and_buzz([question])
20
+ return output[0]
QBpipeline.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Pipeline
2
+ from transformers.utils import ModelOutput
3
+
4
+ from transformers import PreTrainedModel, Pipeline
5
+ from typing import Any, Dict, List
6
+
7
+ class QApipeline(Pipeline):
8
+ def __init__(
9
+ self,
10
+ model: PreTrainedModel,
11
+ **kwargs
12
+ ):
13
+ super().__init__(
14
+ model=model,
15
+ **kwargs
16
+ )
17
+
18
+ print("in __init__")
19
+
20
+ def __call__( self, question: str, context: str, **kwargs) -> Dict[str, Any]:
21
+ inputs = {
22
+ "question": question,
23
+ "context": context
24
+ }
25
+
26
+ outputs = self.model(**inputs)
27
+
28
+ answer = self._process_output(outputs)
29
+
30
+ print("in __call___")
31
+
32
+ return {"answer": answer}
33
+
34
+ def _process_output(self, outputs: Any) -> str:
35
+
36
+ print("in process outputs")
37
+
38
+ format = {'guess': outputs[0], 'confidence': int(outputs[1])}
39
+ return format
40
+
41
+
42
+ def _sanitize_parameters(self, **kwargs):
43
+ print("in sanatize params")
44
+
45
+ return {}, {}, {}
46
+
47
+ def preprocess(self, inputs):
48
+ print("in preprocess")
49
+
50
+ return inputs
51
+
52
+ def postprocess(self, outputs):
53
+ print("in postprocess")
54
+ format = {'guess': outputs[0], 'confidence': float(outputs[1])}
55
+ return format
56
+
57
+ def _forward(self, input_tensors, **forward_parameters: Dict) -> ModelOutput:
58
+ print("in _forward")
59
+ return super()._forward(input_tensors, **forward_parameters)
60
+
README.md ADDED
@@ -0,0 +1,330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The evaluation of this project is to answer trivia questions. You do
2
+ not need to do well at this task, but you should submit a system that
3
+ completes the task or create adversarial questions in that setting. This will help the whole class share data and
4
+ resources.
5
+
6
+ If you focus on something other than predicting answers, *that's fine*!
7
+
8
+ About the Data
9
+ ==============
10
+
11
+ Quiz bowl is an academic competition between schools in
12
+ English-speaking countries; hundreds of teams compete in dozens of
13
+ tournaments each year. Quiz bowl is different from Jeopardy, a recent
14
+ application area. While Jeopardy also uses signaling devices, these
15
+ are only usable after a question is completed (interrupting Jeopardy's
16
+ questions would make for bad television). Thus, Jeopardy is rapacious
17
+ classification followed by a race---among those who know the
18
+ answer---to punch a button first.
19
+
20
+ Here's an example of a quiz bowl question:
21
+
22
+ Expanding on a 1908 paper by Smoluchowski, he derived a formula for
23
+ the intensity of scattered light in media fluctuating densities that
24
+ reduces to Rayleigh's law for ideal gases in The Theory of the
25
+ Opalescence of Homogenous Fluids and Liquid Mixtures near the Critical
26
+ State. That research supported his theories of matter first developed
27
+ when he calculated the diffusion constant in terms of fundamental
28
+ parameters of the particles of a gas undergoing Brownian Motion. In
29
+ that same year, 1905, he also published On a Heuristic Point of View
30
+ Concerning the Production and Transformation of Light. That
31
+ explication of the photoelectric effect won him 1921 Nobel in Physics.
32
+ For ten points, name this German physicist best known for his theory
33
+ of Relativity.
34
+
35
+ *ANSWER*: Albert _Einstein_
36
+
37
+ Two teams listen to the same question. Teams interrupt the question at
38
+ any point by "buzzing in"; if the answer is correct, the team gets
39
+ points and the next question is read. Otherwise, the team loses
40
+ points and the other team can answer.
41
+
42
+ You are welcome to use any *automatic* method to choose an answer. It
43
+ need not be similar nor build on our provided systems. In addition to
44
+ the data we provide, you are welcome to use any external data *except*
45
+ our test quiz bowl questions (i.e., don't hack our server!). You are
46
+ welcome (an encouraged) to use any publicly available software, but
47
+ you may want to check on Piazza for suggestions as many tools are
48
+ better (or easier to use) than others.
49
+
50
+ If you don't like the interruptability of questions, you can also just answer entire questions. However, you must also output a confidence.
51
+
52
+ Competition
53
+ ==================
54
+ We will use Dynabech website (https://dynabench.org/tasks/qa). If you remember the past workshop about Dynabench submission, this is the way to do it. The specific task name is "Grounded QA". Here, with the help of the video tutorial, you submit your QA model and assess how your QA model did compared to others. The assessment will take place by testing your QA model on several QA test datasets and the results of yours and your competitors will be visible on the leaderboard. Your goal is to rank the highest in terms of expected wins: you buzz in with probability proportional to your confidence, and if you're more right than the competition, you win.
55
+
56
+ Writing Questions
57
+ ==================
58
+
59
+ Alternatively, you can also *write* 50 adversarial questions that
60
+ challenge modern NLP systems. These questions must be diverse in the
61
+ subjects asked about, the skills computers need to answer the
62
+ questions, and the entities in those questions. Remember that your questions should be *factual* and
63
+ *specific* enough for humans to answer, because your task is to stump
64
+ the computers relative to humans!
65
+
66
+ In addition to the raw questions, you will also need to create citations describing:
67
+ * Why the question is difficult for computers: include citations from the NLP/AI/ML literature
68
+ * Why the information in the question is correct: include citations from the sources you drew on the write the question
69
+ * Why the question is interesting: include scholarly / popular culture artifacts to prove that people care about this
70
+ * Why the question is pyramidal: discuss why your first clues are harder than your later clues
71
+
72
+ **Category**
73
+
74
+ We want questions from many domains such as Art, Literature, Geography, History,
75
+ Science, TV and Film, Music, Lifestyle, and Sport. The questions
76
+ should be written using all topics above (5 questions for each
77
+ category and 5 more for the remaining categories). Indicate in your
78
+ writeup which category you chose to write on for each question.
79
+
80
+
81
+ Art:
82
+
83
+ * Questions about works: Mona Lisa, Raft of the Medussa
84
+
85
+ * Questions about forms: color, contour, texture
86
+
87
+ * Questions about artists: Picasso, Monet, Leonardo da Vinci
88
+
89
+ * Questions about context: Renaissance, post-modernism, expressionism, surrealism
90
+
91
+
92
+ Literature:
93
+
94
+ * Questions about works: novels (1984), plays (The Lion and the Jewel), poems (Rubaiyat), criticism (Poetics)
95
+
96
+ * Questions about major characters or events in literature: The Death of Anna Karenina, Noboru Wataya, the Marriage of Hippolyta and Theseus
97
+
98
+ * Questions about literary movements (Sturm und Drang)
99
+
100
+ * Questions about translations
101
+
102
+ * Cross-cutting questions (appearances of Overcoats in novels)
103
+
104
+ * Common link questions (the literary output of a country/region)
105
+
106
+
107
+ Geography:
108
+
109
+ * Questions about location: names of capital, state, river
110
+
111
+ * Questions about the place: temperature, wind flow, humidity
112
+
113
+
114
+ History:
115
+
116
+ * When: When did the First World war start?
117
+
118
+ * Who: Who is called Napoleon of Iran?
119
+
120
+ * Where: Where was the first Summer Olympics held?
121
+
122
+ * Which: Which is the oldest civilization in the world?
123
+
124
+
125
+ Science:
126
+
127
+ * Questions about terminology: The concept of gravity was discovered by which famous physicist?
128
+
129
+ * Questions about the experiment
130
+
131
+ * Questions about theory: The social action theory believes that individuals are influenced by this theory.
132
+
133
+
134
+ TV and Film:
135
+
136
+ * Quotes: What are the dying words of Charles Foster Kane in Citizen Kane?
137
+
138
+ * Title: What 1927 musical was the first "talkie"?
139
+
140
+ * Plot: In The Matrix, does Neo take the blue pill or the red pill?
141
+
142
+
143
+ Music:
144
+
145
+ * Singer: What singer has had a Billboard No. 1 hit in each of the last four decades?
146
+
147
+ * Band: Before Bleachers and fun., Jack Antonoff fronted what band?
148
+
149
+ * Title: What was Madonna's first top 10 hit?
150
+
151
+ * History: Which classical composer was deaf?
152
+
153
+
154
+ Lifestyle:
155
+
156
+ * Clothes: What clothing company, founded by a tennis player, has an alligator logo?
157
+
158
+ * Decoration: What was the first perfume sold by Coco Chanel?
159
+
160
+
161
+ Sport:
162
+
163
+ * Known facts: What sport is best known as the ‘king of sports’?
164
+
165
+ * Nationality: What’s the national sport of Canada?
166
+
167
+ * Sport player: The classic 1980 movie called Raging Bull is about which real-life boxer?
168
+
169
+ * Country: What country has competed the most times in the Summer Olympics yet hasn’t won any kind of medal?
170
+
171
+
172
+ **Diversity**
173
+
174
+ Other than category diversity, if you find an ingenious way of writing questions about underrepresented countries, you will get bonus points (indicate which questions you included the diversity component in your writeup). You may decide which are underrepresented countries with your own reasonable reason (etc., less population may indicate underrepresented), but make sure to articulate this in your writeup.
175
+
176
+ * Run state of the art QA systems on the questions to show they struggle, give individual results for each question and a summary over all questions
177
+
178
+ For an example of what the writeup for a single question should look like, see the adversarial HW:
179
+ https://github.com/Pinafore/nlp-hw/blob/master/adversarial/question.tex
180
+
181
+ Proposal
182
+ ==================
183
+
184
+ The project proposal is a one page PDF document that describes:
185
+
186
+ * Who is on your team (team sizes can be between three and six
187
+ students, but six is really too big to be effective; my suggestion
188
+ is that most groups should be between four or five).
189
+
190
+ * What techniques you will explore
191
+
192
+ * Your timeline for completing the project (be realistic; you should
193
+ have your first submission in a week or two)
194
+
195
+ Submit the proposal on Gradescope, but make sure to include all group
196
+ members. If all group members are not included, you will lose points. Late days cannot be used on this
197
+ assignment.
198
+
199
+ Milestone 1
200
+ ======================
201
+
202
+ You'll have to update how things are going: what's
203
+ working, what isn't, and how does it change your timeline? How does it change your division of labor?
204
+
205
+ *Question Writing*: You'll need to have answers selected for all of
206
+ your questions and first drafts of at least 15 questions. This must
207
+ be submitted as a JSON file so that we run computer QA systems on it.
208
+
209
+ *Project*: You'll need to have made a submission to the leaderboard with something that satisfies the API.
210
+
211
+ Submit a PDF updating on your progress to Gradescope. If all team
212
+ members are not on the submission, you will lose points.
213
+
214
+ Milestone 2
215
+ ===================
216
+
217
+ As before, provide an updated timeline / division of labor, provide your intermediary results.
218
+
219
+ *Question Writing*: You'll need to have reflected the feedback from the first questions and completed a first draft of at least 30 questions. You'll also need machine results to your questions and an overall evaluation of your human/computer accuracy.
220
+
221
+ *Project*: You'll need to have a made a submission to the leaderboard with a working system (e.g., not just obey the API, but actually get reasonable answers).
222
+
223
+ Submit a PDF updating on your progress.
224
+
225
+ Final Presentation
226
+ ======================
227
+
228
+ The final presentation will be virtual (uploading a video). In
229
+ the final presentation you will:
230
+
231
+ * Explain what you did
232
+
233
+ * Who did what. For example, for the question writing project a team of five people might write: A wrote the first draft of questions. B and C verified they were initially answerable by a human. B ran computer systems to verify they were challenging to a computer. C edited the questions and increased the computer difficulty. D and E verified that the edited questions were still answerable by a human. D and E checked all of the questions for factual accuracy and created citations and the writeup.
234
+
235
+ * What challenges you had
236
+
237
+ * Review how well you did (based on the competition or your own metrics). If you do not use the course infrastructure to evaluate your project's work, you should talk about what alternative evaluations you used, why they're appropriate/fair, and how well you did on them.
238
+
239
+ * Provide an error analysis. An error analysis must contain examples from the
240
+ development set that you get wrong. You should show those sentences
241
+ and explain why (in terms of features or the model) they have the
242
+ wrong answer. You should have been doing this all along as you
243
+ derive new features, but this is your final inspection of
244
+ your errors. The feature or model problems you discover should not
245
+ be trivial features you could add easily. Instead, these should be
246
+ features or models that are difficult to correct. An error analysis
247
+ is not the same thing as simply presenting the error matrix, as it
248
+ does not inspect any individual examples. If you're writing questions, talk about examples of questions that didn't work out as intended.
249
+
250
+ * The linguistic motivation for your features / how your wrote the questions. This is a
251
+ computational linguistics class, so you should give precedence to
252
+ features / techniques that we use in this class (e.g., syntax,
253
+ morphology, part of speech, word sense, etc.). Given two features
254
+ that work equally well and one that is linguistically motivated,
255
+ we'll prefer the linguistically motivated one.
256
+
257
+ * Presumably you did many different things; how did they each
258
+ individually contribute to your final result?
259
+
260
+ Each group has 10 minutes to deliver their presentation. Please record the video, and upload it to Google Drive, and include the link in your writeup submission.
261
+
262
+ Final Question Submission
263
+ ======================
264
+
265
+ Because we need to get the questions ready for the systems, upload your raw questions on May 10. This doesn't include the citations or other parts of the writeup.
266
+
267
+ System Submission
268
+ ======================
269
+
270
+ You must submit a version of your system by May 12. It may not be perfect, but this what the question writing teams will use to test their results.
271
+
272
+ Your system should be sent directly to the professor and TAs in zip files, including the correct dependencies and a working inference code. Your inference code should run successfully in the root folder (extracted from zip folder) directory with the command:
273
+
274
+ ```
275
+ > python3 inference.py --data=evaluation_set.json
276
+
277
+ ```
278
+
279
+ The input will be in the form of a .json file () in the same format as the file the adversarial question writing team submits. The output format should also be in string.
280
+
281
+ If you have any notes or comments that we should be aware of while running your code, please include them in the folder as a .txt file. Also, dependency information should be included as a .txt file. 
282
+
283
+ Please prepend your email title with [2024-CMSC 470 System Submission].
284
+
285
+ Project Writeup and JSON file
286
+ ======================
287
+
288
+ By May 17, submit your project writeup explaining what
289
+ you did and what results you achieved. This document should
290
+ make it clear:
291
+
292
+ * Why this is a good idea
293
+ * What you did
294
+ * Who did what
295
+ * Whether your technique worked or not
296
+
297
+ For systems, please do not go over 2500 words unless you have a really good reason.
298
+ Images are a much better use of space than words, usually (there's no
299
+ limit on including images, but use judgement and be selective).
300
+
301
+ For question writing, you have one page (single spaced, two column) per question plus a two page summary of results. Talk about how you organized the question writing, how you evaluated the questions, and a summary of the results. Along with your writeup, turn in a json including the raw text of the question and answer and category. The json file is included in this directory. Make sure your json file is in the correct format and is callable via below code. Your submission will not be graded if it does not follow the format of the example json file.
302
+
303
+ ```
304
+ with open('path to your json file', 'r') as f:
305
+ data = json.load(f)
306
+ ```
307
+
308
+
309
+
310
+ Grade
311
+ ======================
312
+
313
+ The grade will be out of 25 points, broken into five areas:
314
+
315
+ * _Presentation_: For your oral presentation, do you highlight what
316
+ you did and make people care? Did you use time well during the
317
+ presentation?
318
+
319
+ * _Writeup_: Does the writeup explain what you did in a way that is
320
+ clear and effective?
321
+
322
+ The final three areas are different between the system and the questions.
323
+
324
+ | | System | Questions |
325
+ |----------|:-------------:|------:|
326
+ | _Technical Soundness_ | Did you use the right tools for the job, and did you use them correctly? Were they relevant to this class? | Were your questions correct and accurately cited. |
327
+ | _Effort_ | Did you do what you say you would, and was it the right ammount of effort. | Are the questions well-written, interesting, and thoroughly edited? |
328
+ | _Performance_ | How did your techniques perform in terms of accuracy, recall, etc.? | Is the human accuracy substantially higher than the computer accuracy? |
329
+
330
+ All members of the group will receive the same grade. It's impossible for the course staff to adjudicate Rashomon-style accounts of who did what, and the goal of a group project is for all team members to work together to create a cohesive project that works well together. While it makes sense to divide the work into distinct areas of responsibility, at grading time we have now way to know who really did what, so it's the groups responsibility to create a piece of output that reflects well on the whole group.
__init__.py ADDED
File without changes
__pycache__/QBModelConfig.cpython-311.pyc ADDED
Binary file (927 Bytes). View file
 
__pycache__/QBModelWrapper.cpython-311.pyc ADDED
Binary file (1.41 kB). View file
 
__pycache__/QBModelWrapperCopy.cpython-311.pyc ADDED
Binary file (1.46 kB). View file
 
__pycache__/QBpipeline.cpython-311.pyc ADDED
Binary file (3.23 kB). View file
 
__pycache__/qbmodel.cpython-311.pyc ADDED
Binary file (2.21 kB). View file
 
__pycache__/tfidf.cpython-311.pyc ADDED
Binary file (6.34 kB). View file
 
huggingface.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from QBModelConfig import QBModelConfig
2
+ from QBModelWrapper import QBModelWrapper
3
+ from transformers import AutoConfig, AutoModel, AutoModelForQuestionAnswering
4
+ import torch
5
+ import numpy as np
6
+ from transformers import QuestionAnsweringPipeline
7
+ from transformers import PretrainedConfig
8
+ from transformers.pipelines import PIPELINE_REGISTRY
9
+ from transformers import AutoModelForQuestionAnswering, TFAutoModelForQuestionAnswering
10
+ from transformers import pipeline
11
+
12
+
13
+
14
+ # class DemoQAPipeline(QuestionAnsweringPipeline):
15
+ # def postprocess(self, model_outputs):
16
+ # answers = super().postprocess(model_outputs)
17
+ # return {'guess': answers['answer'], 'confidence': answers['score']}
18
+
19
+
20
+ AutoConfig.register("QA-umd-quizbowl", QBModelConfig)
21
+ AutoModel.register(QBModelConfig, QBModelWrapper)
22
+ AutoModelForQuestionAnswering.register(QBModelConfig, QBModelWrapper)
23
+
24
+ QBModelConfig.register_for_auto_class()
25
+ QBModelWrapper.register_for_auto_class("AutoModel")
26
+ QBModelWrapper.register_for_auto_class("AutoModelForQuestionAnswering")
27
+
28
+ # PIPELINE_REGISTRY.register_pipeline(
29
+ # "demo-qa",
30
+ # pipeline_class=DemoQAPipeline,
31
+ # pt_model=AutoModelForQuestionAnswering,
32
+ # tf_model=TFAutoModelForQuestionAnswering,
33
+ # )
34
+
35
+
36
+
37
+ qbmodel_config = QBModelConfig()
38
+ qbmodel = QBModelWrapper(qbmodel_config)
39
+
40
+ # # qbmodel_config.save_pretrained("hf-model-config")
41
+ #qbmodel.save_pretrained(save_directory='hf-model-save', safe_serialization= False, push_to_hub=True)
42
+ qbmodel.push_to_hub("quiz-bowl-model-qa-new-attempt-v2")
43
+
44
+ # model = AutoModelForQuestionAnswering.from_pretrained("nes470/hf-model-save", trust_remote_code = True)
45
+ # qa_pipe = pipeline("question-answering", model=model)
46
+
models/processed_tfidf_wiki_16_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca2ab29a43da3e9ee614f5e8a549afed69414da0c8da1628be82f7185f7ed322
3
+ size 36566715
models/processed_tfidf_wiki_page_text_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2a99e5c934c1d5f8714e44fdf50f349a6b71cf21eb6412994eb017385365d76e
3
+ size 59604793
output_data_csv ADDED
The diff for this file is too large to render. See raw diff
 
qbmodel.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Tuple
2
+ import nltk
3
+ import sklearn
4
+ from tfidf import TfidfWikiGuesser
5
+ import numpy as np
6
+ import pandas as pd
7
+
8
+
9
+ class QuizBowlModel:
10
+
11
+ def __init__(self):
12
+ """
13
+ Load your model(s) and whatever else you need in this function.
14
+
15
+ Do NOT load your model or resources in the guess_and_buzz() function,
16
+ as it will increase latency severely.
17
+ """
18
+ #best accuracy when using wiki_page_text.json
19
+ self.guesser = TfidfWikiGuesser(wikidump=None) #can specify different wikidump if needed
20
+ print("model loaded")
21
+
22
+
23
+
24
+ def guess_and_buzz(self, question_text: List[str]) -> List[Tuple[str, bool]]:
25
+ """
26
+ This function accepts a list of question strings, and returns a list of tuples containing
27
+ strings representing the guess and corresponding booleans representing
28
+ whether or not to buzz.
29
+
30
+ So, guess_and_buzz(["This is a question"]) should return [("answer", False)]
31
+
32
+ If you are using a deep learning model, try to use batched prediction instead of
33
+ iterating using a for loop.
34
+ """
35
+
36
+ answers = []
37
+ top_guesses = 3 #guesser will return this amount guesses for each question (in sorted confidence)
38
+
39
+ for question in question_text:
40
+ guesses = self.guesser.make_guess(question, num_guesses=top_guesses)
41
+ #print(guesses)
42
+
43
+ #do the buzzing
44
+
45
+ #make a tuple and add to answers list
46
+ tup = (guesses[0], True)
47
+ answers.append(tup)
48
+
49
+ return answers
50
+
51
+
52
+
questions.json ADDED
@@ -0,0 +1 @@
 
 
1
+ [{"text": "The reciprocal of what quantity was estimated to be .9 higher than the three-digit integer value popularly associated with it in 1916 experiments conducted by a Brandenburg-based researcher? That researcher also hosted Ho Zah-wei ( 何泽慧 ) in his home; she would later go on to lead the neutron flux team of the Chinese nuclear program.", "answer": "Fine-structure_constant", "category": "Science"}]
resources/sample_questions ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ q: "An object orbiting this planet contains sections named Liberty, Equality, and Fraternity. A small group of clouds on this planet was nicknamed \"the Scooter\" for its high speed. Volcanoes that eject ice were first observed on an object that orbits this planet. The first high resolution images of this object were taken by Voyager 2 and revealed a storm system known as the \"Great Dark Spot\". Johann Galle first observed this planet from a telescope using predictions made by Urbain Le Verrier [\"ur-BAIN le vay-ree-AY\"] about its effects on the orbit of Uranus. For 10 points, name this dark blue gas giant, the outermost planet in the Solar System."
2
+ a: Neptune
resources/small.buzztrain.json ADDED
The diff for this file is too large to render. See raw diff
 
resources/small.guesstrain.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e9ceeaaf6da96c0bad2245ce813eee2cf75077ed17753ea0ae78397bc78e0abd
3
+ size 32922039
resources/wiki_page_text.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e151757562aac2ef371ccb40408f531e3cbd88d009b2cde2f5d607ae846a158
3
+ size 105900092
resources/wiki_text_16.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d34632eb523e6486a1815734ac6c6a5f6e4cdf8789b743f7f1f179c507e62002
3
+ size 64487269
resources/wiki_text_2.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0f060e1826986193dcc58055b60f0006b48981e551e1aa758f6553efd641a051
3
+ size 381319675
resources/wiki_text_4.json.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f3def247d35e0971185c59be302e9b0878627c17b6a65437595ca0aedd8a3c61
3
+ size 81351408
resources/wiki_text_8.json.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c717508a9d4cd347841c9f0293d54f3661f203b53a3fa6c765b253748e95000a
3
+ size 46784837
test-huggingface ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from QBModelWrapperCopy import QBModelWrapper
2
+ from QBModelConfig import QBModelConfig
3
+ from QBpipeline import QApipeline
4
+ from transformers.pipelines import PIPELINE_REGISTRY
5
+ from transformers import AutoModelForQuestionAnswering, TFAutoModelForQuestionAnswering
6
+ from transformers import pipeline
7
+ from transformers import AutoConfig, AutoModel, AutoModelForQuestionAnswering, TFAutoModel
8
+
9
+ config = QBModelConfig()
10
+ qb_model = QBModelWrapper(config)
11
+
12
+ # qa_pipe = QApipeline(model=qb_model)
13
+
14
+
15
+ AutoConfig.register("QA-umd-quizbowl", QBModelConfig)
16
+ AutoModel.register(QBModelConfig, QBModelWrapper)
17
+ AutoModelForQuestionAnswering.register(QBModelConfig, QBModelWrapper)
18
+ # TFAutoModel.register(QBModelConfig, QBModelWrapper)
19
+ # TFAutoModelForQuestionAnswering(QBModelConfig, QBModelWrapper)
20
+
21
+ QBModelConfig.register_for_auto_class()
22
+ QBModelWrapper.register_for_auto_class("AutoModel")
23
+ QBModelWrapper.register_for_auto_class("AutoModelForQuestionAnswering")
24
+
25
+
26
+ # result = qa_pipe(question="This star in the solar system has 8 planets", context="Context for the question")
27
+ # print(result["answer"])
28
+
29
+ PIPELINE_REGISTRY.register_pipeline(
30
+ "qa-pipeline-qb",
31
+ pipeline_class=QApipeline,
32
+ pt_model=AutoModelForQuestionAnswering,
33
+ tf_model=TFAutoModelForQuestionAnswering,
34
+ # pt_model=AutoModel,
35
+ # tf_model=TFAutoModel
36
+ )
37
+
38
+ qa_pipe = pipeline("qa-pipeline-qb", model=qb_model)
39
+ #qa_pipe.push_to_hub("new-attempt-pipeline-2", safe_serialization=False)
40
+
41
+ result = qa_pipe(question="This star in the solar system has 8 planets", context="Context for the question")
42
+ print(result["answer"])
43
+
44
+ #if still doesnt work then try making custom pipeline that inherits from QuestionAnsweringPipeline
testqb.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from qbmodel import QuizBowlModel
2
+ import json
3
+ import pandas as pd
4
+
5
+
6
+
7
+ def compare_answers(str1: str, str2: str):
8
+ stripped_1 = ''.join(c.lower() for c in str1 if not c.isspace())
9
+ stripped_2 = ''.join(c.lower() for c in str2 if not c.isspace())
10
+
11
+
12
+ if stripped_1 == stripped_2:
13
+ return True
14
+
15
+ if stripped_1.find(stripped_2) != -1:
16
+ return True
17
+
18
+ if stripped_2.find(stripped_1) != -1:
19
+ return True
20
+
21
+ stripped_1 = stripped_1.replace("_", "")
22
+ stripped_1 = stripped_1.replace("-", "")
23
+ stripped_2 = stripped_2.replace("_", "")
24
+ stripped_2 = stripped_2.replace("-", "")
25
+
26
+ strip = [',','[',']','{','}', '.', '!', '(', ')', ';', ':', '\'',"\""]
27
+ stripped_1 = stripped_1.translate({ord(c): '' for c in strip})
28
+ stripped_2 = stripped_2.translate({ord(c): '' for c in strip})
29
+
30
+ if stripped_1 == stripped_2:
31
+ return True
32
+
33
+ if stripped_1.find(stripped_2) != -1:
34
+ return True
35
+
36
+ if stripped_2.find(stripped_1) != -1:
37
+ return True
38
+
39
+ return False
40
+
41
+
42
+
43
+ qb = QuizBowlModel()
44
+
45
+ question = '''An object orbiting this planet contains sections named Liberty, Equality, and Fraternity.
46
+ A small group of clouds on this planet was nicknamed \"the Scooter\" for its high speed.
47
+ Volcanoes that eject ice were first observed on an object that orbits this planet.
48
+ The first high resolution images of this object were taken by Voyager 2 and revealed a storm system known as the \"Great Dark Spot\".
49
+ Johann Galle first observed this planet from a telescope using predictions made by Urbain Le Verrier [\"ur-BAIN le vay-ree-AY\"] about its effects on the orbit of Uranus.
50
+ For 10 points, name this dark blue gas giant, the outermost planet in the Solar System.
51
+ '''
52
+
53
+
54
+ #can split question into chunks to see how guesser works on harder difficulty.
55
+ splits = question.split(".")
56
+
57
+ data_source = 'resources/small.buzztrain.json'
58
+ with open(data_source) as f:
59
+ doc = json.load(f)
60
+ questions_json = doc['questions']
61
+ questions = []
62
+ answers = []
63
+ limit = 2000 #only test this many or less questions
64
+ for question_json in questions_json:
65
+ question = question_json['text']
66
+ answer = question_json['answer']
67
+ questions.append(question)
68
+ answers.append(answer)
69
+
70
+ limit -= 1
71
+ if limit < 0:
72
+ break
73
+
74
+
75
+
76
+
77
+ #questions = # splits #[question]
78
+ result = qb.guess_and_buzz(questions)
79
+ correct = 0
80
+ almost_correct = 0
81
+
82
+ guesses = []
83
+ buzz = []
84
+ correct_answer = []
85
+ for idx, (result_answer, result_bool) in enumerate(result):
86
+ if answers[idx] == result_answer:
87
+ correct += 1
88
+ guesses.append(result_answer)
89
+ correct_answer.append(result_answer)
90
+ buzz.append(True)
91
+ #print("correct | question: " + questions[idx] + " | answer: " + result_answer)
92
+ elif answers[idx].find(result_answer) != -1:
93
+ print("almost | correct answer: " + answers[idx] + " | result: " + result_answer)
94
+ almost_correct += 1
95
+ guesses.append(result_answer)
96
+ correct_answer.append(answers[idx])
97
+ buzz.append(True)
98
+ elif compare_answers(result_answer, answers[idx]):
99
+ almost_correct += 1
100
+ guesses.append(result_answer)
101
+ correct_answer.append(answers[idx])
102
+ buzz.append(True)
103
+ else:
104
+ guesses.append(result_answer)
105
+ correct_answer.append(answers[idx])
106
+ buzz.append(False)
107
+
108
+ data_df = pd.DataFrame({
109
+ 'question': questions,
110
+ 'correct_answer': correct_answer,
111
+ 'guess': guesses,
112
+ 'buzz': buzz
113
+ })
114
+
115
+ #json.dump(data_df)
116
+ data_df.to_csv('output_data_csv')
117
+
118
+
119
+
120
+ correct_acc = correct/(len(answers))
121
+ almost_acc = (correct+almost_correct)/(len(answers))
122
+ print("correct: " + str(correct_acc))
123
+ print("almost_correct: " + str(almost_acc))
124
+
125
+
tfidf-vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d925508ab964a0c2ad724d0bdf4fe848e59ec807df0d2ce7deb51acc327a0877
3
+ size 34889841
tfidf.ipynb ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from sklearn.feature_extraction.text import TfidfVectorizer\n",
10
+ "from sklearn.metrics.pairwise import cosine_similarity\n",
11
+ "import zipfile\n",
12
+ "import json"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 6,
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "def create_corpus(json_file):\n",
22
+ " corpus = []\n",
23
+ " page_titles = []\n",
24
+ " \n",
25
+ " for json_obj in json_file:\n",
26
+ " corpus.append(json_obj['text'])\n",
27
+ " page_titles.append(json_obj['page'])\n",
28
+ "\n",
29
+ "\n",
30
+ " return (corpus, page_titles)\n",
31
+ "\n",
32
+ "with zipfile.ZipFile('resources/wiki_text_4.json.zip', 'r') as z:\n",
33
+ " with z.open('wiki_text_4.json') as f:\n",
34
+ " doc = json.load(f)\n",
35
+ "#wiki dump is an json array of json objects with page and text fields \n",
36
+ "# with open('resources/wiki_text_16.json') as f:\n",
37
+ "# doc = json.load(f)\n",
38
+ "\n",
39
+ "corpus, titles = create_corpus(doc)\n",
40
+ "\n",
41
+ "vectorizer = TfidfVectorizer()\n",
42
+ "tfidf = vectorizer.fit_transform(corpus)"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 7,
48
+ "metadata": {},
49
+ "outputs": [],
50
+ "source": [
51
+ "question = \"An object orbiting this planet contains sections named Liberty, Equality, and Fraternity. A small group of clouds on this planet was nicknamed \\\"the Scooter\\\" for its high speed. Volcanoes that eject ice were first observed on an object that orbits this planet. The first high resolution images of this object were taken by Voyager 2 and revealed a storm system known as the \\\"Great Dark Spot\\\". Johann Galle first observed this planet from a telescope using predictions made by Urbain Le Verrier [\\\"ur-BAIN le vay-ree-AY\\\"] about its effects on the orbit of Uranus. For 10 points, name this dark blue gas giant, the outermost planet in the Solar System.\"\n",
52
+ "tfidf_question = vectorizer.transform([question])"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": 8,
58
+ "metadata": {},
59
+ "outputs": [
60
+ {
61
+ "name": "stdout",
62
+ "output_type": "stream",
63
+ "text": [
64
+ "Methods_of_detecting_exoplanets\n"
65
+ ]
66
+ }
67
+ ],
68
+ "source": [
69
+ "sim = cosine_similarity(tfidf, tfidf_question) \n",
70
+ "\n",
71
+ "#get index of best matching document and use it to get sim document \n",
72
+ "sim_index = sim.argmax()\n",
73
+ "sim_doc = corpus[sim_index]\n",
74
+ "\n",
75
+ "print(titles[sim_index])\n",
76
+ "# titles"
77
+ ]
78
+ }
79
+ ],
80
+ "metadata": {
81
+ "kernelspec": {
82
+ "display_name": "Python 3",
83
+ "language": "python",
84
+ "name": "python3"
85
+ },
86
+ "language_info": {
87
+ "codemirror_mode": {
88
+ "name": "ipython",
89
+ "version": 3
90
+ },
91
+ "file_extension": ".py",
92
+ "mimetype": "text/x-python",
93
+ "name": "python",
94
+ "nbconvert_exporter": "python",
95
+ "pygments_lexer": "ipython3",
96
+ "version": "3.10.12"
97
+ }
98
+ },
99
+ "nbformat": 4,
100
+ "nbformat_minor": 2
101
+ }
tfidf.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.feature_extraction.text import TfidfVectorizer
2
+ from sklearn.metrics.pairwise import cosine_similarity
3
+ import numpy as np
4
+ import json
5
+ import zipfile
6
+ import pickle
7
+ import os
8
+ from nltk.corpus import stopwords
9
+ from nltk.tokenize import word_tokenize
10
+ from nltk.stem import WordNetLemmatizer
11
+ from models import processed_tfidf_wiki_page_text_model
12
+
13
+
14
+ class TfidfWikiGuesser:
15
+ def __init__(self, wikidump = 'resources/wiki_text_16.json') -> None:
16
+ self.tfidf = None
17
+ self.corpus = None
18
+ self.titles = None
19
+ self.vectorizer = None
20
+ self.lemmatizer = WordNetLemmatizer()
21
+ mod_file = processed_tfidf_wiki_page_text_model
22
+ model_file = "processed_tfidf_wiki_page_text_model.pkl" # <--- has best acc so far (using wiki_page_text.json from gdrive folder)
23
+ #model_file = "processed_tfidf_wiki_16_model.pkl"
24
+ # full_model_path = model_file
25
+ full_model_path = os.path.join("./models", model_file)
26
+
27
+ if os.path.exists(full_model_path):
28
+ print("Loading model from pickle...")
29
+ self.load_from_pkl(full_model_path)
30
+ else:
31
+ if wikidump:
32
+ print("No pre-trained model found, loading data from dump...")
33
+ self.load_model(wikidump)
34
+ self.save_model(full_model_path)
35
+ # self.load_model(wikidump)
36
+
37
+ def load_model(self, wikidump):
38
+ # wiki dump is an json array of json objects with page and text fields
39
+ with open(wikidump) as f:
40
+ doc = json.load(f)
41
+ # with zipfile.ZipFile('resources/wiki_text_8.json.zip', 'r') as z:
42
+ # with z.open('wiki_text_8.json') as f:
43
+ # doc = json.load(f)
44
+
45
+
46
+ self.corpus, self.titles = self.create_corpus(doc)
47
+
48
+ self.vectorizer = TfidfVectorizer(stop_words='english')
49
+ self.tfidf = self.vectorizer.fit_transform(self.corpus)
50
+
51
+ def preprocess_text(self,text):
52
+ if type(text) == float:
53
+ return str(text)
54
+ tokens = word_tokenize(text.lower())
55
+ filtered_tokens = [token for token in tokens if token not in stopwords.words('english')]
56
+ lemmatized_tokens = [self.lemmatizer.lemmatize(token) for token in filtered_tokens]
57
+ processed_text = ' '.join(lemmatized_tokens)
58
+ return processed_text
59
+
60
+ def create_corpus(self, json_file):
61
+ corpus = []
62
+ page_titles = []
63
+
64
+ for json_obj in json_file:
65
+ # corpus.append(json_obj['text'])
66
+ #corpus.append(self.preprocess_text(json_obj['text']))
67
+ corpus.append(json_obj['text'])
68
+ page_titles.append(json_obj['page'])
69
+
70
+ return (corpus, page_titles)
71
+
72
+ def make_guess(self, question, num_guesses = 1):
73
+ tfidf_question = self.vectorizer.transform([question])
74
+
75
+ sim = cosine_similarity(self.tfidf, tfidf_question)
76
+
77
+ #get indices of best matching documents and use it to get (num_guesses) top documents
78
+ sim_indices = np.argsort(sim.flatten())[::-1]
79
+ best_indices = sim_indices[:num_guesses]
80
+
81
+ # best_docs = []
82
+ best_guesses = []
83
+ for i in best_indices:
84
+ # best_docs.append(self.corpus[i])
85
+ best_guesses.append(self.titles[i])
86
+
87
+ return best_guesses
88
+
89
+ def save_model(self, file_name):
90
+ with open(file_name, 'wb') as f:
91
+ pickle.dump({
92
+ 'vectorizer': self.vectorizer,
93
+ 'tfidf_matrix': self.tfidf,
94
+ 'titles': self.titles,
95
+ # 'corpus': self.corpus
96
+ }, f)
97
+
98
+ def load_from_pkl(self, file_name):
99
+ with open(file_name, 'rb') as f:
100
+ data = pickle.load(f)
101
+ self.vectorizer = data['vectorizer']
102
+ self.tfidf = data['tfidf_matrix']
103
+ self.titles = data['titles']
104
+ # self.corpus = data['corpus']