Sonja-Subt commited on
Commit
c55e75f
·
verified ·
1 Parent(s): ad013fc

Upload folder using huggingface_hub

Browse files
.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
.idea/NLP_final2.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="C:\Users\Sofiia\miniconda3" jdkType="Python SDK" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/NLP_final2.iml" filepath="$PROJECT_DIR$/.idea/NLP_final2.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Harry Potter
3
- emoji: 👀
4
- colorFrom: indigo
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.8.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Harry_Potter
3
+ app_file: main_final.py
 
 
4
  sdk: gradio
5
  sdk_version: 5.8.0
 
 
6
  ---
 
 
chunker_final.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def chunk_document_to_dict(doc: str, doc_name: str, desired_chunk_size: int = 400, max_chunk_size: int = 500):
2
+ chunks = {}
3
+ chunk = ''
4
+ chunk_number = 1
5
+
6
+ for line in doc.splitlines():
7
+ chunk += line + '\n'
8
+ if len(chunk) >= desired_chunk_size:
9
+ chunk_id = f"{doc_name}_{chunk_number}"
10
+ chunks[chunk_id] = chunk[:max_chunk_size]
11
+ chunk = ''
12
+ chunk_number += 1
13
+
14
+ if chunk: # Залишок запихаємо в останній чанк
15
+ chunk_id = f"{doc_name}_{chunk_number}"
16
+ chunks[chunk_id] = chunk
17
+
18
+ return chunks
19
+
20
+
21
+ def chunk_documents_to_dict(docs: dict, desired_chunk_size: int = 400, max_chunk_size: int = 500):
22
+ all_chunks = {}
23
+ for doc_name, doc_text in docs.items():
24
+ chunks = chunk_document_to_dict(doc_text, doc_name, desired_chunk_size, max_chunk_size)
25
+ all_chunks.update(chunks)
26
+ return all_chunks
data/book.txt ADDED
The diff for this file is too large to render. See raw diff
 
data/info.txt ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Minister Hermione Jean[28] Granger (b. 19 September 1979)[1] was an English Muggle-born[3] witch born to Mr and Mrs Granger. At the age of eleven, she learned about her magical nature and was accepted into Hogwarts School of Witchcraft and Wizardry. Hermione began attending Hogwarts in 1991 and was Sorted into Gryffindor House. She possessed a brilliant academic mind and proved to be a gifted student in almost every subject that she studied, to the point where she was nearly made a Ravenclaw by the Sorting Hat.
2
+
3
+ Hermione first met her future best friends Harry Potter and Ron Weasley aboard the Hogwarts Express, and initially they found her unfriendly and somewhat of an "insufferable know-it-all", an impression reinforced by her constant correct answers and eagerness to please the professors. However, she stepped in to take the blame from the boys after they had saved her from a troll on Hallowe'en in 1991, surprising them in a grateful way, which led to them quickly becoming friends. She later played a crucial role in protecting the Philosopher's Stone from Voldemort.
4
+
5
+ In her second year, Hermione had a key role in the discovery of the Chamber of Secrets, before falling victim to the Basilisk unleashed upon Hogwarts following the opening of the Chamber. However, she recovered from the petrification under the care of Madam Pomfrey with Professor Sprout's Mandrake Restorative Draught. The next year, Hermione was granted permission to use a Time-Turner from the Ministry of Magic to facilitate her volition to study far more subjects than were possible without time travel, though she and Harry later used it to rescue Sirius Black from the Dementor's Kiss and Buckbeak the hippogriff from execution.
6
+
7
+ During her fourth year, Hermione became an advocate for the better treatment of house-elves, forming the association S.P.E.W., and helped in Harry's preparation for the Triwizard Tournament. In her fifth year, Hermione was the driving force behind the creation of Dumbledore's Army and fought alongside fellow D.A. members in the Battle of the Department of Mysteries. In her sixth year, Hermione continued her role as Prefect and fought in the Battle of the Astronomy Tower.
8
+
9
+ Hermione played a significant role in many other battles of the Second Wizarding War, as well. In 1997, she fought in the Battle of the Seven Potters, alongside the Order of the Phoenix. Hermione and Ron decided not to return to Hogwarts for their final year of studies, instead choosing to accompany Harry on his quest to find and destroy Lord Voldemort's Horcruxes, a quest in which several of her actions were essential. She destroyed Hufflepuff's Cup, a Horcrux, with Ron using a Basilisk fang found in the Chamber of Secrets. Hermione then fought in the Battle of Hogwarts, helping in the defeat of the Death Eater who tortured her: Bellatrix Lestrange.
10
+ Ronald Bilius[27] "Ron" Weasley (b. 1 March 1980) was an English pure-blood[2] wizard, the sixth and youngest son of Arthur and Molly Weasley (née Prewett). He was also the younger brother of Bill, Charlie, Percy, Fred, George, and the elder brother of Ginny. Ron and his siblings lived at The Burrow, on the outskirts of Ottery St Catchpole, Devon.
11
+
12
+ Ron began attending Hogwarts School of Witchcraft and Wizardry in 1991 and was Sorted into Gryffindor House. He soon became close friends with fellow student Harry Potter and later Hermione Granger. Together, they made the Golden Trio and faced many challenges during their adolescence, including keeping the Philosopher's Stone from Professor Quirinus Quirrell, rescuing Ginny from the Basilisk in the Chamber of Secrets, saving Harry's godfather Sirius Black from the Dementors of Azkaban, and later supporting Harry during the Triwizard Tournament.
13
+
14
+ He also helped form Dumbledore's Army and fought in numerous battles of the Second Wizarding War. These battles included the Battle of the Department of Mysteries (1996), Battle of the Astronomy Tower (1997), and Battle of the Seven Potters (1997). Ron also became a Gryffindor prefect and a Keeper on the Gryffindor Quidditch team during his fifth year at Hogwarts.
15
+
16
+ He left Hogwarts before completing his final year in order to accompany Harry and Hermione on a hunt to destroy all of Lord Voldemort's Horcruxes and fought in the Battle of Hogwarts in 1998, during which he lost his brother Fred, a loss which devastated him and his family, especially George, Fred's twin.
17
+
18
+ After the war, Ron became an Auror and helped reform and revolutionise the British Ministry of Magic.[28] After two years as an Auror, he worked at Weasleys' Wizard Wheezes with George to help keep the business thriving after Fred’s death. He and Hermione eventually married and had two children, Rose and Hugo Granger-Weasley. Ron was also named the godfather of James Sirius Potter, his sister Ginny Weasley's and Harry Potter's eldest child.
19
+ Ginevra[9] Molly[22] "Ginny" Potter[23] (née Weasley) (b. 11 August 1981),[1] occasionally known as Gin by Harry Potter,[4] was an English pure-blood[3] witch, the only daughter of Arthur and Molly Weasley (née Prewett), and the younger sister of Bill, Charlie, Percy, the late Fred, George and Ron. She was the first female to be born into the Weasley line for several generations. She and her older brothers grew up in The Burrow on the outskirts of Ottery St Catchpole in Devon.
20
+
21
+ Ginny attended Hogwarts School of Witchcraft and Wizardry from 1992 to 1999, a year beneath the famous Harry Potter, and was sorted into Gryffindor House. During her first year, she developed a long-standing crush on Harry Potter and came under the influence of the memory of Tom Riddle's sixteen-year-old self, who was preserved in a diary, which forced Ginny to re-open the Chamber of Secrets, endangering the lives of many students, including her own.
22
+
23
+ Following that ordeal, Ginny grew into a confident young woman, becoming an important member of Dumbledore's Army, an organisation taught and led by Harry Potter in her fourth year. She would later fight in the Battle of the Department of Mysteries (1996), the Battle of the Astronomy Tower (1997), and the Battle of Hogwarts (1998). She was also an accomplished Quidditch player, playing Chaser and Seeker at different times for the Gryffindor Quidditch team.
24
+
25
+ After the Second Wizarding War, Ginny became a professional Quidditch player for the Holyhead Harpies. Upon retiring from the Harpies, she became the senior Quidditch correspondent for the Daily Prophet. Ginny eventually married Harry Potter and the couple had three children; James Sirius (named after Harry's father and godfather), Albus Severus (named after Albus Dumbledore and Severus Snape), and Lily Luna Potter (named after Harry's mother and their friend Luna Lovegood). By 2020, Ginny had become the sports editor for the Daily Prophet.
26
+ Professor Neville Longbottom (b. 30 July 1980)[1][2] was a British pure-blood[3] wizard, the only child and son of Frank and Alice Longbottom. Neville's parents were well-respected Aurors and members of the original Order of the Phoenix until they were tortured into insanity by Bellatrix Lestrange and three other Death Eaters with the Cruciatus Curse when he was about sixteen months old. They were placed in the Janus Thickey Ward at St Mungo's Hospital for Magical Maladies and Injuries, leaving Neville to be raised by his paternal grandmother, Augusta Longbottom.
27
+
28
+ At the time of his birth, Neville was marked by a prophecy as being one of two children with the potential to defeat Lord Voldemort, the most powerful and feared Dark wizard in the world. However, Voldemort's first attempt to circumvent the prophecy by killing Neville's later friend and classmate Harry Potter, ultimately established Harry as Voldemort's nemesis.
29
+
30
+ Neville began school at Hogwarts School of Witchcraft and Wizardry in 1991 and was Sorted into Gryffindor House, along with Harry Potter, Hermione Granger, and Ronald Weasley. Throughout his school years, he was mostly a shy, clumsy, introverted boy who was constantly being told by his grandmother that he was not good enough or living up to his parents' accomplishments. However, in his later years, he showed that he possessed great courage and perseverance: he became an important member of Dumbledore's Army, an organisation taught and led by Harry Potter with the goal of helping other students learn spells to attack and defend themselves.
31
+
32
+ Neville would later fight in the Battle of the Department of Mysteries (1996), the Battle of the Astronomy Tower (1997), and the Battle of Hogwarts (1998). During the Battle of Hogwarts, having refused Voldemort's invitation to join the Death Eaters, he destroyed the seventh and final Horcrux when he beheaded the snake Nagini. He also co-led the D.A. during his final year in opposition to the Death Eater professors.
33
+
34
+ After the end of the Second Wizarding War, Neville briefly served as an Auror,[10] before becoming the Herbology professor and the Head of Gryffindor House[5] at Hogwarts and marrying Hannah Abbott from Herbology. He was also the godfather of Harry and Ginny's second son, Albus Potter.
35
+ Tom Marvolo Riddle (31 December 1926[20] – 2 May 1998), later known as Lord Voldemort or, alternatively as the Dark Lord, You-Know-Who, or He-Who-Must-Not-Be-Named was an English half-blood[3] wizard considered to have been the most powerful and dangerous Dark wizard of all time.[21][22] He was amongst the greatest wizards to have ever lived, rivalled only by Albus Dumbledore.[23][24][25] Descended from Salazar Slytherin on his mother's side through the House of Gaunt, Tom Riddle was conceived whilst his father, Tom Riddle Senior, was under the effects of a love potion supplied by his mother, Merope Gaunt. When the enchantment was lifted, Tom's father abandoned his pregnant wife and returned to Little Hangleton. His mother gave birth to him, before dying an hour later. Her only wishes were that he'd look like his father, and that he was named Tom after his father, his middle name Marvolo after her father, and that Riddle would be his last name. And with that, Tom Marvolo Riddle was born and raised in the Muggle-run Wool's Orphanage.
36
+
37
+ Tom began attending Hogwarts School of Witchcraft and Wizardry in 1938 and was immediately sorted into Slytherin house. During this time, Tom discovered his heritage and adopted pure-blood supremacist ideals. Tom opened the Chamber of Secrets and used its monster, the Basilisk, to attack Muggle-born students, resulting in the death of Myrtle Warren. He began his quest to attain immortality between the years of 1943 and 1998, creating his first Horcrux at the age of sixteen. While attending Hogwarts, Tom Riddle was made a prefect in 1942 until his graduation in 1945.
38
+
39
+ Tom spent several months as a purchasing agent for the Dark artefacts shop Borgin and Burkes before disappearing. Splitting his own spirit into a total of eight fragments, Tom created an unprecedented seven Horcruxes, with one made later in his life, unintentionally and without his knowledge — Harry Potter. Tom re-emerged several decades later having abandoned his 'Muggle' name and becoming the self-proclaimed Lord Voldemort, an anagram of his birth name. In the First and Second Wizarding Wars, he commanded a veritable army of wizards and Dark creatures; committed countless murders and atrocities; and eventually succeeded in taking over the British Ministry of Magic.
40
+
41
+ Voldemort was ripped from his body in 1981 after attempting to kill Harry Potter. Although unable to die, he was unable to regain a permanent and physical body until 1995, thus spending the intervening fourteen years as 'a shell, less than the meanest ghost'. He was finally killed by his own backfired Killing Curse after Harry Potter and Albus Dumbledore succeeded in destroying all of his Horcruxes.
42
+
43
+ Unknown to most, however, Voldemort's legacy and bloodline would live on through his daughter Delphini, whom he conceived with his loyal follower Bellatrix Lestrange. The conception is thought to have occurred sometime after the Battle of the Department of Mysteries in 1996 and before Voldemort's death at Harry's hands in 1998.
44
+
45
+ Riddle's broken and mutilated soul was then trapped in limbo for eternity, unable to move on to the afterlife or even return as a disembodied spirit.
46
+ Draco Lucius[11] Malfoy (b. 5 June 1980) was a British pure-blood wizard and the only son of Lucius and Narcissa Malfoy (née Black). The son of a Death Eater, Draco was raised to strongly believe in the importance of blood purity. He looked down on half-bloods and Muggle-borns.
47
+
48
+ Draco attended Hogwarts School of Witchcraft and Wizardry from 1991-1998. He was sorted into Slytherin House almost the moment the Sorting Hat touched his head. During his years at Hogwarts, he became friends with Vincent Crabbe, Gregory Goyle, Pansy Parkinson, and other fellow Slytherins, but he quickly developed a rivalry with Harry Potter.
49
+
50
+ He was made a prefect of his house and was a member of the Inquisitorial Squad during his fifth year, at the end of which his father was imprisoned in Azkaban following the Battle of the Department of Mysteries. Lord Voldemort charged Draco with making up for Lucius's failure, and he became a Death Eater at age sixteen but quickly became disillusioned and uncomfortable with the lifestyle.
51
+
52
+ Draco was unable to complete his task of murdering Albus Dumbledore, which was later taken over by Severus Snape, and only performed his other duties fearfully and reluctantly. He and his family defected hours before the end of the Second Wizarding War, fearing for their lives.
53
+
54
+ By 2017, Draco had married Astoria Greengrass and had one child, Scorpius Hyperion Malfoy.Professor Severus Snape (9 January 1960[1] – 2 May 1998)[2] was an English half-blood[3] wizard serving as Potions Master (1981-1996), Head of Slytherin House (1981-1997), Defence Against the Dark Arts professor (1996-1997), and Headmaster (1997-1998) of the Hogwarts School of Witchcraft and Wizardry as well as a member of the Order of the Phoenix and a Death Eater. His double life played an extremely important role in both of the Wizarding Wars against Voldemort.
55
+
56
+ The only child of Muggle Tobias Snape and pure-blood witch Eileen Snape (née Prince), Severus was raised in the Muggle dwelling of Spinner's End, which was in close proximity to the home of the Evans family, though in a poorer area. He met Lily and Petunia Evans when he was nine. He fell deeply in love with Lily upon their meeting and became a close friend of hers.[7]
57
+
58
+ Severus started at Hogwarts with Lily in the 1971–1972 school year, where he was Sorted into Slytherin House. This put him in the same year as Lily but unfortunately for him she was Sorted into Gryffindor House. Severus became the immediate enemy of James Potter and Sirius Black and was a frequent victim of their bullying. This led him to be irritable towards James's son Harry when he was a professor. Snape, when young, developed a passion for the Dark Arts, which increased as his desire for revenge against his bullies grew stronger.
59
+
60
+ Snape became involved with the bullies in Slytherin House, many of whom were pure-blood supremacists. This put his friendship with Lily, a Muggle-born, under great strain until it was eventually broken in their fifth year.[6][7] In a misguided attempt to win back Lily's affections,[18] Snape joined the Death Eaters along with a group of his fellow Slytherins.
61
+
62
+ Severus was made a member of the Slug Club presumably because of his talent in Potion-making. Horace Slughorn even kept a picture of him as a student, clutching his copy of Advanced Potion-Making. However, Horace did not actually have many hopes for Severus's future, as his photograph was kept behind many others.[19]
63
+
64
+ As an active Death Eater, one of Snape's biggest contributions was letting Lord Voldemort know about the prophecy Sybill Trelawney made to Dumbledore. This part of the prophecy could have applied to either Harry Potter or Neville Longbottom, who was also born at the end of July 1980 to parents who were members of the Order. Once Lord Voldemort decided to target the Potters over the Longbottoms, Snape begged Voldemort to at least spare Lily. Voldemort ostensibly agreed, but did not actually follow through on this promise due to Lily's refusal to allow him to kill Harry, resulting in her sacrificing her life to save her son's.
65
+
66
+ Shortly before Lily Evans was murdered by Lord Voldemort, Snape changed sides and became a member of the Order of the Phoenix as well as a double agent during the Second Wizarding War. With tremendous difficulty, Snape prevented Lord Voldemort from learning the truth about his loyalties.
67
+
68
+ Despite the opinions of most others including Harry during his early life, Albus Dumbledore trusted Snape for reasons that were kept between them both until their deaths. Despite Snape killing Dumbledore, it is learned that they had a special agreement for him to do so. When he died, it was revealed that his deep strong love for Lily Evans caused him to redeem himself, joining Dumbledore's cause for her protection (and, after her death, that of her son) from Lord Voldemort.[7]
69
+
70
+ The relationship between Dumbledore and Snape would be one of unusually strong loyalty, so much so that Snape agreed to kill Dumbledore upon the latter's own request. Before Dumbledore's death, Snape promised to protect the students of Hogwarts from the Death Eaters, who would inevitably take control of the Ministry of Magic as well as the school.[7]
71
+
72
+ Snape later participated in the Battle of Hogwarts but was murdered by Lord Voldemort who mistakenly believed that Snape was the master of the Elder Wand (an immensely strong and powerful wand that Voldemort deeply desired, as well as one of the Deathly Hallows)[20] when in reality, Harry Potter was the true master of the Elder Wand, because Draco Malfoy had disarmed Dumbledore on the night of Dumbledore's death at the top of the Astronomy Tower, and Harry had disarmed Draco at Malfoy Manor.[21]
73
+
74
+ After Snape's death, Harry Potter ensured that his portrait remained at Hogwarts, honouring him as a hero, despite their significant personal differences.[21] In addition, Harry later named his second son Albus Severus Potter in honour of Dumbledore and Severus, both of whom were an inspiration in Harry's life after the Battle of Hogwarts. Harry also was influenced by Severus' Hogwarts house, and freely admitted that his son Albus could choose Slytherin if he wished.[22]
75
+ The Sorting Hat was a sentient magical hat at Hogwarts that was used to determine which of the four school Houses was the best fit for each new student. These four Houses were Gryffindor, the house of bravery, Hufflepuff, the house of hard work, Ravenclaw, the house of intelligence, and Slytherin, the house of ambition.[2] The Sorting Hat originally belonged to Godric Gryffindor, one of the four founders of Hogwarts.[3] It was normally kept in the Headmaster's office until it was needed for the Sorting Ceremony at the annual Start-of-Term Feast.[1]
76
+ The famous Hogwarts Sorting Hat gave an account of its own genesis in a series of songs.[3] During the opening banquet at the beginning of each school year, the first-year students were lined up and their names read aloud alphabetically by last name. Each then took a seat on a stool and placed the hat on their heads.[2]
77
+
78
+ The hat was battered and old by 1991; it was patched, frayed, and extremely dirty. In order to speak and sing, a tear along the brim opened like a mouth. It spoke to the wearer inside of the hat with a small, quiet voice, using Legilimency to interpret their thoughts and respond to them. After a time of consideration, the hat announced its choice aloud for all to hear, and the student joined the selected house.[3] The moment of consideration varied in time period from less than a second to over five minutes (known as "hatstall").[4]
79
+
80
+ The Sorting Hat was notorious for refusing to admit it had ever made a mistake in its sorting of a student. On those occasions when Slytherins behaved altruistically or selflessly, when Ravenclaws flunked all their exams, when Hufflepuffs proved lazy yet academically gifted, and when Gryffindors exhibited cowardice, the Hat steadfastly backed its original decision. On balance, however, the Hat had made remarkably few errors of judgement over the many centuries it had been at work.[4]
81
+
82
+ Although most people believed that the Sorting Hat used its Legilimency to see which House would be the most suitable for the student, the Hat actually used its Legilimency to see which House the student would be happiest in, which it judged by the student's mental reaction to mention of the House. This, it claimed, was the reason why it was never wrong when picking a House, as it wasn't the Hat picking the House at all but instead the students themselves.[5]
83
+
84
+ History
85
+ Main article: Sorting Ceremony
86
+ "Sings a different [song] every year. It's got to be a pretty boring life, hasn't it, being a hat? I suppose it spends all year making up the next one."
87
+ — Ron Weasley's opinion of the Sorting Hat's existence[src]
88
+ Sorting
89
+ The Sorting Hat, Sorting students into their respective Houses at the ceremony
90
+
91
+ Legend had it that the Sorting Hat was sewn roughly one thousand years ago and began as a normal hat belonging to Godric Gryffindor. When Gryffindor, along with Salazar Slytherin, Rowena Ravenclaw and Helga Hufflepuff, wondered how they would continue to sort the students when the four were dead, Gryffindor pulled his hat from his head and, along with the other founders, enchanted it with their combined intelligence. All four founders wanted to ensure that students would be sorted into their eponymous Houses, which would be selected according to each founder's particular preferences in students.[3]
92
+
93
+ B2C12M1 Sorting Hat in Dumbledore's office
94
+ The Sorting Hat on a shelf in the Headmaster's office
95
+
96
+ The Sorting Hat, now animated, was able to sort and divide the students into the Hogwarts Houses, and continued to do so for a millennium, passed down to each new Headmaster or Headmistress of the school. When not in use, it is placed on a shelf in the current Headmaster's or Headmistress's office.[1]
97
+
98
+ The hat continued to carry out its duty each September at the beginning of the start-of-term feast, when it was placed on a three-legged (sometimes four-legged) stool at the front of the Great Hall.[2]
99
+
100
+ In 1858, Angus Buchanan, an eleven-year old Scottish Squib managed to sneak into the feast with the help of his wizard brother and put the Sorting Hat on his head. The Sorting Hat then kindly said he was a good-hearted chap, but no wizard.[6]
101
+
102
+ Sword appears in hat
103
+ The Sword of Gryffindor appears in the hat
104
+
105
+ The Sword of Gryffindor, one of the founder's only other known relics, could be magically pulled out of the hat by any Gryffindor considered worthy, no matter how secure the sword's location.[7] It was unknown why this was possible, but was linked to loyalty of the highest regard in relation to Hogwarts. There were two known instances of this happening within 1992 to 1998, once when Harry Potter was in the Chamber of Secrets,[7] and once when Neville Longbottom refused to join Lord Voldemort during the Battle of Hogwarts.[8]
106
+
107
+ During the Calamity in the late 2010s or early 2020s, the Sorting Hat spoke with a Calamity Investigator working for the Statute of Secrecy Task Force who was visiting Hogwarts, with the investigator being surprised by its ability to more articulately explain its experience being a Foundable than anyone else they had interviewed. The investigator theorised that the hat's enchanted origins might have enabled it to better understand the Calamity's effects.[9]
108
+
109
+ The Sorting Hat and the Sword of Gryffindor were the only two relics that hadn't been turned into Horcruxes by former Hogwarts student Tom Marvolo Riddle during his transformation into Lord Voldemort.
110
+ Hogwarts School of Witchcraft and Wizardry was divided into four Houses: Gryffindor, founded by Godric Gryffindor; Hufflepuff, founded by Helga Hufflepuff; Ravenclaw, founded by Rowena Ravenclaw; and Slytherin, founded by Salazar Slytherin.[1]
111
+ Hogwarts School of Witchcraft and Wizardry, often shortened to Hogwarts, was the British wizarding school located in the Scottish Highlands.[6][7] Any magical children residing in Great Britain or Ireland who had reached the age of eleven by the start of the incoming term were accepted as students.[8][9] It was a state-owned school, funded by the British Ministry of Magic.[10]
112
+
113
+ The precise location of the school could never be uncovered because it was rendered Unplottable. To Muggles, the school appeared to be an old, abandoned castle. Similarly, most wizarding schools' locations were protected in order to protect the students and schools themselves from any harm.[4]
114
+
115
+ Established around the 10th century, Hogwarts was considered to be one of the finest magical institutions in the wizarding world, though other notable schools included Beauxbatons Academy of Magic in France, the Durmstrang Institute implied to be in northern Europe, and Ilvermorny School of Witchcraft and Wizardry in the United States.[4]
116
+
117
+ British and Irish children with magical abilities were enrolled at birth by the Quill of Acceptance and Book of Admittance,[11] and acceptance was confirmed by owl post at age eleven. However, if the child in question was a Muggle-born or raised by Muggles, like Harry Potter, with no knowledge of the wizarding world, a special messenger from the school visited the child and his or her family in order to inform them of their magical heritage and the existence of the wizarding world.[12] It was possible for students to transfer to the school as well.
118
+
119
+ The school's motto was Draco Dormiens Nunquam Titillandus (Draco Dormiens Nvnqvam Titillandvs), which, translated from Latin, means "Never tickle a sleeping dragon".
data/overview.txt ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Harry James Potter (born. 31 July 1980) was an English half-blood wizard, and one of the most famous wizards of modern times. The only child and son of James and Lily Potter (née Evans), Harry's birth was overshadowed by a prophecy, naming either himself or Neville Longbottom as the one with the power to vanquish his arch-enemy, Lord Voldemort, one of the most powerful and feared Dark wizards in the world. After half of the prophecy was reported to Voldemort, courtesy of Severus Snape, Harry was chosen as the target due to his many similarities with the Dark Lord. This in turn caused the Potter family to go into hiding.
2
+
3
+ Voldemort made his first attempt to circumvent the prophecy when Harry was a year and three months old. During this attempt, he murdered Harry's parents as they tried to protect him, but this unsuccessful attempt to kill Harry led to Voldemort's first downfall. This downfall marked the end of the First Wizarding War, and to Harry henceforth being known as "The Boy Who Lived", as he was the only known survivor of the Killing Curse due to being magically protected by his mother's loving sacrifice. In accordance with the terms of the prophecy, this attempt on his life also established him, not Neville, as Voldemort's nemesis.
4
+
5
+ One consequence of Lily's sacrifice was that her orphaned son had to be raised by her only remaining blood relative, his Muggle aunt Petunia Dursley, and her husband, uncle Vernon Dursley. While in their care, he would be protected from Lord Voldemort due to the Bond of Blood charm that Albus Dumbledore placed upon him. This powerful charm would protect him until he either came of age, or no longer called his aunt's house home. Due to Petunia's resentment of her sister and her magical abilities, Harry grew up abused and neglected.
6
+
7
+ Shortly before Harry's eleventh birthday, there were several attempts from Hogwarts School of Witchcraft and Wizardry to send him a letter inviting him to not only come to Hogwarts, but also to explain his magical heritage. Though Vernon Dursley ultimately made the decision to leave their residence at 4 Privet Drive temporarily to evade the letters, his attempts were ultimately for naught. On his eleventh birthday, Harry learned from Rubeus Hagrid that he was a wizard.[59] Rubeus Hagrid took Harry from the Dursley family the next day to Diagon Alley. This is where Harry withdrew some of his money from Gringotts Wizarding Bank, where Hagrid bought Harry's snow-white owl, Hedwig. While he was in Diagon Alley, Harry also got his school supplies (i.e., books from Flourish and Blotts, his robes from Madam Malkin's Robes for All Occasions, and his potion equipment from Slug & Jiggers Apothecary).
8
+
9
+ Harry began attending Hogwarts in 1991. The Sorting Hat told Harry that he would do well in Slytherin House, but Harry pleaded "not Slytherin". The Hat heeded this plea and sorted the young wizard into Gryffindor House.[54] At school, Harry became best friends with Ron Weasley and Hermione Granger. He was also the youngest Seeker in a century, making the house team in his first year when Minerva McGonagall introduced him to Oliver Wood, Gryffindor's Quidditch captain at the time. He later became the captain of the Gryffindor Quidditch Team in his sixth year, winning two Quidditch Cups. While in school, Harry also demonstrated an extraordinary talent for Defence Against the Dark Arts.
10
+
11
+ He became even better-known in his early years for protecting the Philosopher's Stone from Voldemort, saving Ron's sister Ginny Weasley, solving the mystery of the Chamber of Secrets, slaying Salazar Slytherin's basilisk, and learning how to conjure a corporeal Patronus at the age of thirteen, which took the form of a stag. In his fourth year, Harry won the Triwizard Tournament, although the person had to be seventeen to enter, and Harry was only fourteen. The competition ended with the tragic death of Cedric Diggory and the return of Lord Voldemort. During the next school year, in defiance of Dolores Umbridge and the Ministry of Magic's strict regime against the teaching of Defence Against the Dark Arts and the continued introduction of the new Educational Decree, Ronald Weasley and Hermione Granger first had the idea of creating an illegal Defence Against the Dark Arts group called Dumbledore's Army. Harry was initially reluctant to the idea, but he gradually came to like it and eventually agreed to start the group. He also fought in the Battle of the Department of Mysteries, during which he lost his godfather, Sirius Black, and the prophecy was destroyed.
12
+
13
+ Harry played a significant role in many other battles of the Second Wizarding War. During what would have been his seventh year, he, Ron, and Hermione hunted down and destroyed Voldemort's Horcruxes. During the Battle of Hogwarts, Harry personally saw the deaths of Severus Snape and Fred Weasley while learning that Remus Lupin, Nymphadora Tonks, Colin Creevey, and many others had fallen in the battle as well. He encountered Voldemort and nearly sacrificed himself, knowing that his doing so was the only way to destroy the fragment of Voldemort's soul inside of him. While in limbo, after Voldemort cursed him in the forest clearing, Dumbledore gave Harry advice and background information on the Dark Lord. When told that he could choose to live or to "move on", Harry ultimately chose to live. After he awoke, Harry confronted Voldemort and defeated him once and for all.
14
+
15
+ Harry was also noted for being the only known Master of Death, having united the three Deathly Hallows at the mere age of seventeen.
16
+
17
+ After the war, Harry became an Auror and helped reform and revolutionise the Ministry of Magic. At some point, he married Ginny, with whom he had three children: James Sirius, Albus Severus, and Lily Luna. He was also named the godfather of Edward "Teddy" Remus Lupin. In 2007, Harry was promoted to Head of the British Auror Office at the age of 26, and would occasionally deliver Defence Against the Dark Arts lectures at Hogwarts. He was later promoted to Head of the Department of Magical Law Enforcement by the summer of 2020.
18
+ Harry was guided further to his destiny on 1 September 1991, when he was dropped off by the Dursleys at King's Cross Station. Vernon helped Harry wheel his things to the platforms and left laughing about how Platform Nine and Three-Quarters had not yet been built. The family then drove off, leaving Harry at the busy station all alone. With only ten minutes left until the train's departure at eleven a.m., Harry was panicking when he overheard a red-headed family complaining about the station being packed with Muggles, and he noticed that they had similar belongings.
19
+
20
+ Harry meeting the Weasleys PSF
21
+ Harry meeting the Weasleys for the first time at King's Cross Station
22
+
23
+ Harry watched as the older boys magically passed through the barrier between platforms nine and ten. Harry nervously and politely approached them and was introduced to their youngest son Ron, who was starting his first year as well. Molly (Ron's mother) kindly gave him instructions on how to board the platform. Harry quickly ran through the barrier, followed by Ron, and arrived on the Platform, catching his first sight of the Hogwarts Express. As most of the compartments were full, Harry pushed his things to near the end of the train, and with some help from twins from the red-headed family, was able to get his things aboard.[83]
24
+
25
+ While helping, the twins noticed his scar and recognised that he must be Harry Potter. While Harry sat near the window in his compartment watching the family, the twins Fred and George Weasley reported this discovery to the amazement of their little sister Ginny and their older brother Percy who was a Prefect and had already changed into his robes. After boarding, the train soon departed and Ron asked if he could sit with Harry, who agreed. Ron asked Harry about his scar, and Harry asked if Ron's whole family were wizards, which they are except for his mother's second cousin, who was an accountant. When the trolley witch came by around half-past twelve, Harry bought some of the treats from the cart and shared them with Ron.
26
+
27
+ Trio meet
28
+ The trio meeting for the first time aboard the Hogwarts Express
29
+
30
+ Neville Longbottom soon came by searching for his lost toad, Trevor. While Ron was attempting a magic spell to change his rat Scabbers, a girl named Hermione Granger came by trying to help find Trevor. After Ron's spell failed, Hermione introduced herself and upon learning Harry's name, informed him that he was in several books on magical history. While speculating which house they might be in, Harry and Ron were interrupted by Draco Malfoy, Vincent Crabbe, and Gregory Goyle. Although Draco attempted to ingratiate himself, Harry refused to shake his hand after he insulted Ron and his family. When Goyle attempted to take some of their Chocolate Frog Cards, Scabbers bit his finger and helped drive them off. Ron afterwards explained that the Malfoy family were on Voldemort's side, but came back saying they had been bewitched, although his father did not believe it.
31
+
32
+ Upon arriving, Hagrid gathered all the first years together and they followed a path down to the edge of a Great Lake. In groups of no more than four, they climbed aboard a little fleet of small boats, and sailed across the lake to an underground harbour. Helping to remind Neville to take his toad, Hagrid took them up a passageway to the gates of the castle.
33
+
34
+ The Sorting Ceremony
35
+ "It is our choices, Harry, that show what we truly are, far more than our abilities."
36
+ — Harry's choice to be sorted into Gryffindor
37
+ The new students were greeted at the castle door by Professor Minerva McGonagall, who explained the four houses of Hogwarts: Gryffindor, Hufflepuff, Ravenclaw, and Slytherin, as well as the rules of the House Cup. McGonagall led the first years into a small room off the Entrance Hall and told them to wait until she returned. When she did, they were led into the Great Hall, where they were greeted by the rest of the students, and more importantly, a shabby wizard's hat on a small stool.
38
+
39
+ Harry during the Sorting Ceremony
40
+
41
+ Harry was particularly anxious, as he did not feel that any of the Houses as they were described in the Hat's song were right for him. Harry noted that Draco Malfoy, whom Harry had met in Diagon Alley, was instantly placed in Slytherin, and remembered what Hagrid and Ron had told him about Slytherin's reputation for turning out Dark wizards, and that Voldemort had been in Slytherin. When Harry put on the Hat, it slipped down past his eyes, and the hat told him that he "would do well in Slytherin". Thinking of Voldemort, Harry desperately repeated the phrase "not Slytherin". The Hat heeded Harry's request and placed Harry in Gryffindor along with Ron and Hermione Granger. In his second year during a visit to Dumbledore's office, the Sorting Hat re-emphasised the idea that Harry "would do well in Slytherin".
42
+
43
+ The Sorting Ceremony was followed by the Start-of-Term Feast. Having previously never been allowed to eat as much as he wanted, Harry was overwhelmed by the sheer variety of foods in front of him. Dessert arrived, and talk turned to the first-years' families. Neville Longbottom told the others how his family thought he was a Squib until his great-uncle accidentally dropped him out of a window, and he bounced.
44
+
45
+ HP1-1294
46
+ Harry as his scar began to hurt during the Sorting Ceremony after making eye contact with Severus Snape
47
+
48
+ It was during the feast that Harry's scar hurt for the first time: Harry was looking up at the staff table at Professor Quirrell when the hook-nosed teacher Quirrell was talking to looked past Quirrell at Harry, who immediately felt a sharp pain in his scar. This was because Lord Voldemort was inhabiting Quirrell's body, and anytime Voldemort was near Harry, his scar hurt. After the last morsels melted from the golden plates and goblets, Dumbledore gave a speech welcoming the new students to the school and the old students back. He added a few warnings about staying away from the Forbidden Forest, and avoiding the third-floor corridor before leading the school in singing the school song and sending everyone off to their dormitories.
49
+
50
+ First-years' lessons/classes
51
+ Severus Snape: "What is the difference, Potter, between monkshood and wolfsbane?"
52
+ Harry: "I don't know. I think Hermione does, though, why don't you try her?"
53
+ Severus Snape: "...And a point will be taken from Gryffindor House for your cheek, Potter"
54
+ — From this first interaction Harry deduces Snape's hatred of him
55
+ In his first ever Potions class, Harry discovered that Professor Snape hated him, mocking him as the school's "new celebrity" before teaching the class how to brew a Boil-Cure Potion. Harry and Ron went down to Hagrid's hut for tea, where they met Hagrid's huge and fierce-looking dog, Fang. Hagrid told Harry that he was overreacting to Snape's treatment, asserting that he would have no reason to hate him. Hagrid and Ron began to talk about Ron's older brother Charlie Weasley. While they were talking, Harry picked up a cutting from the Daily Prophet that was lying on the table. The article detailed a break-in that occurred on Harry's birthday at Gringotts. The vault that was broken into was number 713, the same vault Hagrid visited with Harry on their trip to Diagon Alley.
56
+
57
+
58
+ The first flying class of the 1991–1992 school year
59
+
60
+ One of the things that Harry had been looking forward to was learning to fly until he found out that the Gryffindors would be taking flying lessons with the Slytherins. Malfoy had been bragging about his skill to anyone who would listen. Madam Rolanda Hooch taught the class by starting with basic broom control. After learning the theory, the students were told to hover gently off the ground on Madam Hooch's go-ahead. Terrified of being left behind, Neville panicked and kicked off before anyone else, rising fifty feet in the air before falling off and breaking his wrist. Madam Hooch took Neville to the Hospital Wing after warning the other students to stay on the ground until she got back.
61
+
62
+ Draco Malfoy nicked Neville's Remembrall from off the ground and was told to give it to Harry, jeering that he would leave it "up a tree" unless Harry stopped him and took off on his broom. Harry mounted his broom and kicked off after him. As much to his surprise as everyone else's, he discovered that he could not only fly, but that it was something he did not need to be taught. Bending low on the broom handle, he shot towards Draco, who realised that Harry was a better flyer and threw the Remembrall into the air, daring the famous boy to catch it. Harry raced the ball towards the ground, catching it and coming out of his dive a foot from the ground. He toppled lightly onto the grass amidst the cheers of the Gryffindors, grinning wildly.
63
+
64
+ Harry-potter1-seeker harry
65
+ McGonagall discovering Harry's potential as the new Gryffindor Seeker
66
+
67
+ His euphoria did not last long, however, as Professor McGonagall quickly arrived on the scene. Having seen the dive, she ordered Harry to follow her. Expecting punishment, Harry was instead introduced to Oliver Wood, whom she pulled out of a Charms class. Captain of the Gryffindor Quidditch team, Wood's confusion at being introduced to a first year quickly turned to excitement and ecstasy upon hearing McGonagall recount the dive. He told her that Harry would need a decent broom if they were to compete, and explained to Harry that he would make an excellent Seeker, perhaps rivalling the legendary Charlie Weasley.
68
+
69
+ The midnight duel
70
+ Harry: "You're a lot braver now that you're back on the ground and you've got your little friends with you."
71
+ Draco Malfoy: "I'd take you on anytime on my own. Tonight, if you want. Wizard's duel. Wands only — no contact. What's the matter? Never heard of a wizard's duel before, I suppose?"
72
+ — Malfoy challenging Harry to a one-on-one duel
73
+ Harry told Ron about everything that happened after he left with McGonagall over dinner that night, but warned him that Wood wanted to keep it a secret. Much calmer on the ground, and with his cronies Vincent Crabbe and Gregory Goyle flanking him, Draco came over to taunt Harry about getting in trouble earlier. Enraged that Harry not only escaped trouble, but was instead rewarded, he challenged Harry to a wizard's duel. In spite of Hermione's attempt to dissuade them from breaking the school rules, (or perhaps because of it), Harry accepted the challenge.
74
+
75
+ Trio third floor
76
+ The trio trespassing in the restricted third-floor corridor
77
+
78
+ As they left the tower, the trio found Neville (whose wrist had been fixed by Madam Pomfrey) waiting outside, having forgotten the password. Neville decided to go with them and the four arrived at the Trophy Room (the site of the duel), but Malfoy was nowhere to be found. They speculated that he may have chickened out and were deciding what to do next when they heard the school caretaker Argus Filch and his cat, Mrs Norris, enter the room. Realising that Draco tricked them and informed Filch of their location, they attempted to quietly exit the room. A nearby doorknob rattled and Peeves, Hogwarts's resident poltergeist, burst into the hallway, threatening to expose them.[84]
79
+
80
+ B1C9M3 ForbiddenCorridorFluffy Moment
81
+ The trio encountering Fluffy, a three-headed dog, guarding a trap door
82
+
83
+ Growing desperate, Ron took a swipe at Peeves, who began to bellow their whereabouts as loud as he could, attracting Filch. The four kids ran right to the end of the corridor, where they found themselves stopped by a locked door, which Hermione opened with the Unlocking Charm using Harry's wand. They hurried inside, thinking themselves out of danger until turning around and coming face-to-face with a monstrous sight: a giant three-headed dog. Choosing Filch over death, the children ran for it, somehow managing to get back to their dormitory without running into anyone along the way. Though shaken by the night's events, Harry's interest in exploring was piqued by Hermione's pointing out that the dog was standing on a trapdoor.
84
+
85
+ The Nimbus 2000
86
+ "Wood — I've found you a Seeker. The boy's a natural. I've never seen anything like it. Was that your first time on a broomstick, Potter?"
87
+ — Harry becoming Gryffindor's new Seeker
88
+ Broomstick PS
89
+ Harry receiving a Nimbus 2000, a gift from Professor McGonagall
90
+
91
+ Over breakfast the next morning, Harry and Ron were discussing what Fluffy could be guarding when the mail arrived. Harry, who had received no mail apart from Hagrid's letter, was intrigued as everyone else by the long, oddly-shaped package in the mail that morning, and was even more surprised than the others when he discovered what was inside: a Nimbus 2000, and with a note from Professor McGonagall warning him not to open the package at the table, and that he was to meet Wood that night for Quidditch practice.
92
+
93
+ Taking the broom up to their dormitory, Harry and Ron were stopped by Malfoy, who seized the package and told Harry that first-year students are not allowed their own brooms. When Professor Flitwick walked up to them, he tried to report Harry, but Flitwick, who had been told of Harry's special permission, just expressed admiration for Harry's talent.
94
+
95
+ Hallowe'en
96
+ "But from that moment on, Hermione Granger became their friend. There are some things you can't share without ending up liking each other, and knocking out a twelve-foot mountain troll is one of them."
97
+ — Harry and Ron forming a friendship with Hermione
98
+ Harry-potter1-harry seamus charm lesson
99
+ A Hallowe'en lesson on the Levitation Charm
100
+
101
+ On Hallowe'en, Professor Flitwick began teaching his students how to perform the Levitation Charm. Only Hermione succeeded; offended by her air of superiority, Ron later made a nasty comment that she overheard. The comment was about her lack of friends, causing her to run off to lock herself in the girl's bathroom in tears and making him and Harry feel slightly guilty. When the two went down to the Hallowe'en feast later, their guilt was forgotten amidst the splendour of the decorations.
102
+
103
+ Partway into the feast, Quirrell arrived to announce that there was a twelve-foot Mountain Troll in the dungeons before fainting where he stood. The prefects led the students back to their dorms, but Harry realised that Hermione did not know about the troll and convinced Ron to help save her, since they were responsible. They sneaked off to the girl's bathroom to warn Hermione, locking the troll inside. However, they did not realise their mistake until they heard Hermione's terrified scream emanate from the bathroom. A horrified Harry and Ron ran back into the bathroom to rescue her. After a brief skirmish, during which Harry stuck his wand up the troll's nose, Ron finally knocked the troll out, by levitating the troll's own club to smash into its head.
104
+
105
+ B1C10M3 Troll in First-Floor Girls' Toilets
106
+ The mountain troll in the bathroom, where Hermione was hiding
107
+
108
+ Attracted by all the noise, the teachers arrived to find Harry, Ron, and Hermione covered in dust, and the bathroom in disarray. Professor McGonagall, head of Gryffindor, began scolding the boys for not going straight to their dormitories with the rest of their house, but instead putting themselves in grave danger. Much to Harry and Ron's surprise, Hermione lied to McGonagall and told her that she had gone looking for the troll, as she thought she could handle them. She claimed that Harry and Ron were looking for her, (which was true), and she would most likely be dead if the boys had failed to rescue her (also true). The three bonded over the shared experience, and were friends thereafter.
109
+
110
+ Quidditch
111
+ Harry: "I've got the Snitch!"
112
+ Marcus Flint: "He didn't catch it, he nearly swallowed it!"
113
+ — Harry catching the Golden Snitch with his mouth
114
+ As the Quidditch season began, Harry became increasingly nervous. The first match of the season was against Slytherin. Harry was under increasing pressure to show that he was not just a famous name. In an attempt to calm his nerves, Harry borrowed a book entitled Quidditch Through the Ages, a book that was comprehensive history of the sport, that Hermione must have checked out from the library. During break on the day before the match, Harry, Ron, and Hermione were huddled together around a jar of flames, which had been conjured by Hermione, to keep warm.
115
+
116
+ Harry-potter1-snape limp
117
+ Harry noticing the blood on Snape's robes and a slight limp when he walked
118
+
119
+ Snape noticed their guilty faces, and looking for a reason to punish them, confiscated Harry's book on the feeble pretext that library books were not to be taken outside. Harry noticed that Snape was limping, as though his leg was injured, strengthening his suspicions that the Potions Master was after whatever it was that Fluffy was guarding. Nervous about the next day's match, Harry decided to ask Snape for the book back; realising that he would most likely be in the staffroom and that it would be harder for Snape to bully him if there were other teachers around, Harry decided to confront him. Approaching the door, Harry overheard Snape complaining to Filch about Fluffy. After opening the door, Harry saw Filch helping Snape to bandage his leg. He returned to the Common Room and then told his two friends everything he had seen.
120
+
121
+ Main article: Gryffindor vs Slytherin Quidditch match (1991)
122
+ B1C11M2 Gryffindor vs Slytherin Quidditch match
123
+ Harry playing in his first Quidditch match against Slytherin
124
+
125
+ Harry had little time to dwell on Snape's injury as the first Quidditch match began the very next morning. Harry's job as being the Gryffindor Seeker was to catch the Golden Snitch. The snitch is a walnut-sized gold ball, that is extremely fast and difficult to see. The entire match rested upon the retrieval of the Snitch. Harry's first attempt to catch the Snitch was foiled when the Slytherin Seeker blatched him. Though the Seeker was penalised, the move succeeded in stopping Harry from getting to the Snitch.
126
+
127
+ Soon after, Harry's broom began bucking uncontrollably as if trying to unseat him. The bucking became even more violent with each passing second, until Harry was hanging from the broom with just one hand. As the crowd looked on with horror, some of the professors had their wands at the ready should he fall. Ron and Hermione began to wonder if the other was at fault. That is until Hagrid, who had arrived to watch the game, noted that it would take powerful dark magic to make a broomstick so hard to manage, magic that powerful was well above the level of a second year and therefore had to have been cast by a qualified wizard. This announcement did nothing to help the worried friends.
128
+
129
+ Harry catches the snitch
130
+ Harry catching the Snitch and winning the opening match against Slytherin
131
+
132
+ Hermione, who had turned her gaze away from Harry, and was scanning the stands, noticed that Snape was staring unblinkingly at Harry and muttering non-stop under his breath. Thinking quickly, Hermione took advantage of the fact that everyone's attention was now focused on Harry (and the Weasley twins' attempts to rescue him) to run around the entire stadium, knocking over Professor Quirrell, and ending up behind Snape. Muttering a few "well-chosen words" Hermione lit Snape's robes on fire with Bluebell Flames; a yell of shock told her she had done her job and she scooped the fire into a jar. Suddenly, up in the air, the spell on Harry's broomstick was broken and he was once again able to control his broom. The spectators watched in confusion as Harry dove towards the ground, only to clasp his hand to his mouth as if he was being violently sick the instant he landed. In actuality, Harry had caught the Snitch in his mouth. His capture of the Snitch ended the match, resulting in Gryffindor's victory.
133
+
134
+ B1C14M1 Trio in the Hogwarts Library
135
+ The trio searching for information on Flamel in the Hogwarts Library
136
+
137
+ After the match, Hagrid took the three back to his hut. Ron and Hermione told Harry and Hagrid about what was happening on the other side of the stands, and how Snape was cursing his broomstick. Hagrid, however, did not believe them, asking why Snape would try to kill Harry; Harry told Hagrid about Snape being bitten by the dog in the third-floor corridor. Surprised by their knowledge, Hagrid involuntarily revealed that the dog belonged to him, and that what the dog was guarding did not concern them, as it was a secret known only to Albus Dumbledore and a man named Nicolas Flamel.
138
+
139
+ Christmas season
140
+ "It does not do to dwell on dreams and forget to live, remember that."
141
+ — Albus Dumbledore warning Harry about the Mirror
142
+ Impressed as they were with the fact that Harry had managed to hold onto a bucking broomstick, Malfoy soon found that the rest of the school no longer found his taunts that Harry was to be replaced amusing, and so reverted to teasing Harry about having to stay at Hogwarts for the holidays. Harry, however, was looking forward to spending Christmas away from the Dursleys, especially in light of the fact that Ron was also staying at Hogwarts, but also because it would give them some time to look up Nicolas Flamel; they were certain that the librarian would be able to find a book on Flamel in an instant, but were worried that it might be suspicious, and were thus forced to search for themselves.
143
+
144
+ Invisibility Cloak GIF
145
+ Harry receiving his father's Cloak of Invisibility as a Christmas gift
146
+
147
+ On Christmas day, Harry and Ron awoke to a pile of presents, each at the foot of their beds. At the bottom of the pile, he found a package containing an Invisibility Cloak and an anonymous note telling him only that the cloak once belonged to his father, and to "use it well". That night, Harry thought on the Cloak and decided to try it out. Realising he could go anywhere, he snuck back to the library and headed straight for the Restricted Section. Knowing he had to start somewhere, Harry pulled down one of the heavier books, and let it fall open on his knee. To his shock and horror, the silence was rent by a blood-curdling scream that issued from the book in front of him. He stuffed the book back in its place and ran for the door, knocking over the lantern he brought with him in his haste. Ducking under Filch's outstretched arms, Harry ran down the dark corridors, away from the library, and away from Filch.
148
+
149
+ Thinking he had escaped, Harry was scared to hear Filch's voice approaching, and horrified when he realised whom Filch was talking to: Snape. As Snape and Filch rounded the corner, Harry realised that although his father's cloak made him invisible, it did not stop him from being solid, and that he had no chance of sneaking past them, as the corridor was particularly narrow. Thinking quickly, (and panicking slightly), Harry noticed a door to his left; slipping inside, he found himself in an abandoned classroom. After Filch and Snape passed his hiding place, Harry relaxed and took in more details about the room he was in.
150
+
151
+ MirrorOfErised PM
152
+ Harry standing in front of the Mirror of Erised
153
+
154
+ In doing so, he noticed something he had missed the first time: an old, gilded mirror bearing the inscription “Erised stra ehru oyt ube cafru oyt on wohsi”. Stepping in front of the mirror, Harry very nearly cried out in shock: inside the mirror he saw a large crowd of people standing behind him. Shocked, Harry turned around to look at the room, but saw no one there. Turning back to the mirror and looking more closely, Harry realised that the man and woman in the front looked oddly like him. The man looked just like him, from his untidy hair to his glasses, and the woman, Harry saw, had the same eyes he had. Understanding, Harry focused on other members of the crowd, and saw others who had his untidy hair, his eyes, and even an old man who had Harry's knees; Harry was looking at his family, for the first time in his life.
155
+
156
+ The next night, Harry brought Ron with him to the mirror room. Ron did not see Harry's family in the mirror, but instead saw himself standing alone, tall, holding the House Cup, wearing badges indicating he was Head Boy and Quidditch Captain. The two began arguing over who would be allowed to look in the mirror; Mrs Norris, Filch's prowling cat, entered the room, attracted by the sounds of their argument. The next day, Ron, worried about being caught and about his friend's obsession with the mirror, warned Harry not to return; Harry, however, was not to be dissuaded.
157
+
158
+ Dumbledore talking to Harry about the Mirror of Erised
159
+ Dumbledore advising Harry from seeking out the mirror again
160
+
161
+ Going to the room that evening, Harry was ready to stay there all night, staring at the family he lost. However, in his haste, he failed to notice Professor Dumbledore standing by the door until after he removed his cloak. Dumbledore, who had been waiting for Harry, explained that the mirror, which was known as the “Mirror of Erised”, displayed the deepest, most desperate desire of whoever looked into it. Harry, who had never known his family, saw them standing around him; Ron, ever overshadowed by his older brothers, saw himself standing alone, the best of them all.
162
+
163
+ Before sending Harry back to bed, however, Dumbledore warned him that the mirror was a dangerous object; men had been driven mad by what they saw in the mirror, not knowing if what they saw was real, or even possible. He told Harry that the mirror was to be moved to a new location, and warned Harry to not go looking for it. Before he left, Harry asked the headmaster what he saw in the mirror. Much to his surprise, Dumbledore told Harry that he saw himself with a pair of woollen socks. Despite his promise to Dumbledore, Harry found it difficult to forget the image of his parents, until Wood told the team that Snape would be refereeing the next Quidditch match against Hufflepuff.
164
+
165
+ Neville got a Leg-Locker Curse (1991)
166
+ Harry cheering Neville up after the latter was bullied by Draco Malfoy
167
+
168
+ Shortly before the match, Malfoy, ever eager to pick on Neville, used the Leg-Locker Curse on Neville, forcing him to bunny hop his way back to the Common Room. Harry told Neville he was "worth twelve of Malfoy" and gave him a Chocolate Frog. Thanking Harry, Neville handed the card back to Harry for his collection. Looking down, Harry noted it was Dumbledore's card, the first he ever got. He turned the card over and suddenly remembered where he read the name Nicolas Flamel before: on the train on the way to school.
169
+
170
+ As the Gryffindor vs. Hufflepuff Quidditch match drew nearer, tensions mounted in the school; Gryffindor had failed to overtake Slytherin in the Inter-House Quidditch Cup for seven years, and Harry was worried that it would be impossible with Snape as a referee. Unknown to Harry, Ron and Hermione had been practising the Leg-Locker Curse to use on Snape, should he show any signs of wanting to harm Harry. Before the match began, Wood took Harry aside and reminded him that he needed to catch the Snitch as soon as he could, before Snape could favour Hufflepuff too much. Harry performed his role as Seeker phenomenally, catching the Snitch within five minutes of the game starting, putting Gryffindor in first place for the Quidditch Cup.
171
+
172
+ Detention
173
+ Firenze: "It will keep you alive, even if you are an inch from death, but at a terrible price. You have slain something pure and defenceless to save yourself, and you will have but a half-life, a cursed life, from the moment the blood touches your lips."
174
+ Harry: "But — who would choose such a life?"
175
+ — Firenze explaining the properties of unicorn blood
176
+ Egg hatching ps
177
+ The trio watching a dragon egg hatch while in Hagrid's Hut
178
+
179
+ Having realised how much Harry, Ron, and Hermione had worked out about the Stone after running into them in the library, Rubeus Hagrid told them to meet him in his hut later. When the trio arrived later, they noticed that the fire was lit, despite the heat of the day. Although he was reluctant to answer their questions, Hermione managed to manipulate him into talking about the various protections used to guard it: Fluffy, the three headed dog, was Hagrid's, along with enchantments from Professors Sprout, Flitwick, McGonagall, Quirrell, and Snape. Growing uncomfortable in the heat, Harry asked Hagrid to open a window, something Hagrid refused to do as he had a dragon egg in the fire. Unfortunately, Draco Malfoy discovered the dragon, and decided to use the knowledge to get revenge by getting them into trouble for possessing an illegal dragon. So Malfoy in turn, tried to tell McGonagall of the trio's activities.
180
+
181
+ B1C14M3 Astronomy Tower farewell Norbert
182
+ Harry and Hermione waiting on top of the Astronomy tower with baby Norbert
183
+
184
+ To save everyone involved, Harry, Ron, and Hermione convinced Hagrid to send Norbert off to Ron's brother Charlie Weasley, who would take Norbert to a Romanian dragon preserve. While helping Hagrid to prepare Norbert for the journey, the dragon bit Ron's hand, causing it to swell up and forcing Ron to see Madam Pomfrey. On the pre-arranged night, Harry and Hermione managed to smuggle Norbert in a crate up to the Astronomy Tower under Harry's Invisibility Cloak. On the way up, they witnessed Professor McGonagall hauling Malfoy away for being out of bed at night, who protested that Harry was in possession of a dragon. Harry and Hermione passed the crate off to Charlie's friends and headed back down the stairs, where they were confronted by a gleeful Argus Filch; they had left the Cloak behind.
185
+
186
+ WB F1 Fang HarryMalfoyFangInForest 11428
187
+ Harry in the Dark Forest with Draco Malfoy during their detention
188
+
189
+ Filch took them to McGonagall's office, where they found Neville, who had attempted to warn them about Malfoy. Minerva McGonagall, who was very disappointed in them, took fifty points each from Harry, Hermione, and Neville and gave the three detention, which they were to serve along with Malfoy. Riding high on his Quidditch wins, Harry's popularity plummeted: no one wanted to talk to the "stupid first year" who, along with two others, had lost Gryffindor their chance of defeating Slytherin, and winning the House Cup. A week or so before exams, Harry's new determination to not meddle in others' business was tested when he heard Professor Quirrell, whimpering as if he was being threatened. Believing that Snape was the one threatening Quirrell, and that he was going after the Stone, Hermione suggested that they go talk to Professor Dumbledore, but Harry dismissed the idea, as they had no proof.
190
+
191
+ The next morning, Harry, Hermione, and Neville received notes from Professor McGonagall informing them their detention would begin at eleven that night. Argus Filch took them out to the Forbidden Forest, where Hagrid was waiting for them. Hagrid led them into the Forbidden Forest and showed them a pool of unicorn blood on the ground. They split up, Hagrid taking Harry and Hermione, while Neville and Malfoy went off with Fang. After Malfoy scared Neville into sending up red sparks, Hagrid sent Harry off with Malfoy, deciding that Malfoy would be less likely to scare Harry.
192
+
193
+ Harry Potter and Firenze in the Forbidden Forest
194
+ Harry with Firenze in the forest discussing the properties of unicorn blood
195
+
196
+ As they continued, Harry noticed the pools of unicorn blood they were following seemed to be growing larger and larger, as if the animal had been thrashing around. Eventually, they came to a clearing and found the laying on the ground, and very dead. As they watched, a hooded figure emerged from the bushes and began to drink the unicorn's blood. Malfoy screamed and bolted away with Fang, leaving Harry, half blinded by the pain in his scar to stumble away from the advancing figure. Harry was saved by Firenze, a palamino centaur, who allowed Harry to ride on his back out of the forest. Firenze told Harry the properties of unicorn blood. Harry realised that there would only be one person who would be so desperate as to kill a unicorn: Lord Voldemort.
197
+
198
+ The Philosopher's Stone
199
+ Harry: ""Professor, I think — I know — that someone's going to try and steal the Stone. I've got to talk to Professor Dumbledore."
200
+ Minerva McGonagall: "Professor Dumbledore will be back tomorrow. I don't know how you found out about the Stone, but rest assured, no one can possibly steal it, it's too well protected."
201
+ — The trio warning McGonagall of the plot to steal the stone
202
+ While talking to Ron and Hermione after finishing their exams, Harry realised the strange coincidence that had occurred: Hagrid wanted a dragon more than anything else, only to meet a stranger who had one to give him. They ran down to ask Hagrid more about the man who gave him Norbert, only to find out that the stranger never lowered his hood, something of a fashion in the Hogs head. Hagrid explained that he could not remember much, as the man kept buying him drinks, but said that he thought they talked about Hogwarts and the kinds of creatures Hagrid looks after there.
203
+
204
+ Transfiguration homework
205
+ The trio attempting to tell McGonagall about the plot to steal the Philosopher's Stone
206
+
207
+ Focused on remembering what had happened that night, Hagrid accidentally let slip that Fluffy fell asleep when played music. Now convinced that Snape had all the information he needed to get past Fluffy,Harry, Ron, and Hermione decided to go see Professor Dumbledore and tell him their suspicions. While walking across the Entrance Hall, they were stopped by Professor McGonagall, and decided to tell her what they had found out. She insisted that no one could steal the Stone, and told them that Dumbledore was in London for the day.
208
+
209
+ Main article: Skirmish in the Underground Chambers
210
+ As the trio set off that night to stop Snape, they were stopped themselves by Neville, who believed that they were sneaking out without reason again, and was worried that they would loose Gryffindor even more points. Desperate as they were for time, Hermione paralysed Neville. When they arrived the third-floor corridor, it was to find Fluffy awake, but a harp by his feet. Remembering what Hagrid told them, Harry began to blow into the wooden flute Hagrid gave him for Christmas. From the first notes, Fluffy's eyes began to droop, and he quickly fell asleep. Jumping through the trapdoor, they found themselves in Professor Sprout's room, filled with Devil's Snare, which almost smothered them.
211
+
212
+ Harry-Potter and the winged-keys PS
213
+ Harry retrieving the right key from the swarm
214
+
215
+ The next room, Professor Flitwick's, held a bunch of flying keys and some broomsticks. Harry found the correct key, caught it, and unlocked the next door with it. The next room was Professor McGonagall's, and had a large chessboard, for a game of Wizard's Chess which Ron won at the cost of sacrificing himself. Harry and Hermione continued to the next room (leaving an unconscious Ron where they could return for him), to find an unconscious troll; Professor Quirrell's room. Lastly, they entered Professor Snape's room, and found seven potions in bottles along with a roll of paper giving clues on which one to drink to continue: a logic puzzle. Hermione solved the puzzle, and at Harry's urging, drank the potion that allowed her to head back so that she could get Ron out, while Harry drank the potion to go forward to the final room.
216
+
217
+ B1C17M1 Quirrell and Harry by the Mirror of Erised
218
+ Harry and Quirrell in the Dungeon
219
+
220
+ Once inside the room, Harry's attention was drawn to two things: the Mirror of Erised, and Quirrell. Quirrell bound Harry before explaining that the Mirror was the key to finding the Stone. Desperate to distract him from the Mirror, Harry questioned Quirrell, who revealed that he was serving Lord Voldemort, and although Snape hated Harry (because Snape allegedly loathed his father in their time at school), he never wanted him dead. Unable to locate the Stone, Quirrell asked Voldemort for help; much to Harry's surprise, a voice which seemed to issue from Quirrell himself said "to use the boy"
221
+
222
+ Looking in the Mirror, Harry saw his reflection pull the Stone out of his pocket and replace it, at which point he felt the real Stone drop his real pocket. He told Quirrell that he saw himself winning the House Cup, but Voldemort, an accomplished Legilimens, informed Quirrell that Harry was lying, and ordered Quirrell to allow him to speak "to the boy". Quirrell unwrapped his turban, and turned away from Harry. Sticking out of the back of Quirrell's head, Voldemort demanded that Harry give him the Stone. Harry refused, and Quirrell seized him, causing Harry's scar to sear with pain; but contact with Harry's skin burned Quirrell's hands, forcing him to release Harry. Realising that contact caused Quirrell pain, Harry grabbed Quirrell's arm and held on until he blacked out
223
+
224
+ B1C17M2 Harry's first Hospital Wing stay
225
+ Harry in the Hospital Wing after the attack in the Underground Chambers
226
+
227
+ He woke in the Hospital Wing, where Albus Dumbledore reassured him that Quirrell did not succeed at getting the Stone, and that the Stone had in fact, been destroyed. Dumbledore then explained the reason why Quirrell could not touch him was that because Harry's mother had died to save him, granting him protection against Voldemort
228
+
229
+ At the End-of-Term Feast, after seemingly congratulating Slytherin on winning the House Cup, Dumbledore awarded Ron and Hermione fifty points, Harry sixty points, and Neville ten, which allowed them to win the cup
main_final.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from litellm import completion
3
+ import glob
4
+ import os
5
+ from retriever_reranker_final import Retriever
6
+
7
+ PROMPT = """\
8
+ You are a helpful assistant that can answer questions.
9
+ Rules:
10
+ - Provide clear and concise answers to all questions.
11
+ - Always paraphrase the context when forming your response. Do not copy the text directly.
12
+ - Use only the provided context to answer questions.
13
+ - Only give the answer.
14
+ - Avoid repeating long phrases from the context.
15
+ - Structure your responses in a way that is easy to read and understand.
16
+
17
+ """
18
+ #- Include only information relevant to the question and ignore unrelated details.
19
+
20
+ class QuestionAnsweringBot:
21
+
22
+ def __init__(self, docs) -> None:
23
+ self.retriever = Retriever(docs)
24
+ self.max_citations = 3
25
+
26
+ def answer_question(self, question: str, api_key: str, methods: list[str]) -> list[str]:
27
+ try:
28
+ os.environ['GROQ_API_KEY'] = api_key
29
+
30
+ if not methods:
31
+ return ["No search method selected. Please select at least one search method.", "-", "-"]
32
+
33
+ retr_context = self.retriever.get_docs(question, methods)
34
+ reranked_context = self.retriever.rerank(question, retr_context)
35
+
36
+ context_with_all_chunks = "\n".join([
37
+ f"[{chunk_id}] {chunk_text}" for chunk_id, chunk_text in reranked_context.items()
38
+ ])
39
+
40
+ messages = [
41
+ {"role": "system", "content": PROMPT},
42
+ {"role": "user", "content": f"Context:\n{context_with_all_chunks}\nQuestion: {question}"}
43
+ ]
44
+
45
+ response = completion(
46
+ model="groq/llama3-8b-8192",
47
+ messages=messages
48
+ )
49
+ response_text = response.choices[0].message.content
50
+
51
+ used_chunks = self._filter_used_chunks(response_text, reranked_context)
52
+ top_chunks = self._get_top_chunks(used_chunks, self.max_citations)
53
+
54
+ context_with_citations = "\n".join([
55
+ f"[{chunk_id}] {chunk_text}" for chunk_id, chunk_text in top_chunks.items()
56
+ ])
57
+ used_sources = " ".join([f"[{chunk_id}]" for chunk_id in top_chunks.keys()])
58
+ final_response = f"{response_text} \nUsed chunks: {used_sources}"
59
+
60
+ return [
61
+ final_response,
62
+ context_with_citations,
63
+ f"{reranked_context}"
64
+ ]
65
+ except Exception as e:
66
+ print(f"Error: {e}")
67
+ return ["Error occurred during processing.", "-", "-"]
68
+
69
+ def _filter_used_chunks(self, response_text, reranked_context):
70
+ used_chunks = {}
71
+ for chunk_id, chunk_text in reranked_context.items():
72
+ if any(word.lower() in response_text.lower() for word in chunk_text.split()):
73
+ used_chunks[chunk_id] = chunk_text
74
+ return used_chunks
75
+
76
+ def _get_top_chunks(self, used_chunks, max_citations):
77
+ unique_chunks = {}
78
+ added_texts = set()
79
+ for chunk_id, chunk_text in used_chunks.items():
80
+ if any(chunk_text[:50] in text for text in added_texts):
81
+ continue
82
+ unique_chunks[chunk_id] = chunk_text
83
+ added_texts.add(chunk_text[:50])
84
+ if len(unique_chunks) >= max_citations:
85
+ break
86
+ return unique_chunks
87
+
88
+
89
+ # Load documents
90
+ all_docs = {}
91
+ for path in glob.glob("data/*.txt"):
92
+ with open(path) as f:
93
+ doc_name = os.path.basename(path)
94
+ all_docs[doc_name] = f.read()
95
+
96
+ bot = QuestionAnsweringBot(all_docs)
97
+
98
+ with gr.Blocks() as demo:
99
+ gr.HTML("""
100
+ <div style="text-align: center; font-size: 40px; font-weight: bold; margin-bottom: 10px;">
101
+ Harry Potter Encyclopedia
102
+ </div>
103
+ <div style="text-align: center; font-size: 25px ;;margin-bottom: 10px; color: #ffdac7">
104
+ Performed by Subtelna Sofiia (CS-414) and Iryna Iskovych (CS-415)
105
+ </div>
106
+ <div style="font-size: 20px; ">
107
+ This bot answers questions about the world of Harry Potter and the plot of the first book. System uses 3 files - general information about the world, main characters and the text of the first book. However, some of the information about the universe or later years can be missing.
108
+
109
+ <div style="font-weight: bold; margin-top: 25px;">
110
+ Instructions
111
+ </div>
112
+ <ol>
113
+ <li>Enter your Groq API Key in the textbox below.</li>
114
+ <li>The API key can be generated using this <a href="https://console.groq.com/keys">link</a></li>
115
+ <li>Input your query</li>
116
+ <li>Select the scoring method from the proposed ones:</li>
117
+ <ul>
118
+ <li>BM25</li>
119
+ <li>Semantic search</li>
120
+ <li>Combined search (combination of BM25 and semantic search)</li>
121
+ </ul>
122
+ <li>Click "Submit" button</li>
123
+
124
+ </ol>
125
+ </div>
126
+ """)
127
+ question_input = gr.Textbox(
128
+ label="Question",
129
+ placeholder="Ask your question here",
130
+ )
131
+
132
+ api_key_input = gr.Textbox(label="API key", placeholder="Provide API key here", info="Input key from Groq")
133
+ search_method = gr.Radio(
134
+ info="Choose the search method from the proposed ones",
135
+ choices=["BM25", "semantic", "combined search"],
136
+ label="Search Method",
137
+ value="BM25"
138
+ )
139
+
140
+ submit_button = gr.Button("Submit")
141
+
142
+ output_bot = gr.Textbox(label="Answer")
143
+ output_citations = gr.Textbox(label="Citations")
144
+ #output_context = gr.Textbox(label="Context")
145
+
146
+ submit_button.click(
147
+ bot.answer_question,
148
+ inputs=[question_input, api_key_input, search_method],
149
+ outputs=[output_bot,output_citations]#, output_context]
150
+ )
151
+
152
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ litellm
3
+ torch
4
+ numpy
5
+ rank-bm25
6
+ sentence-transformers
retriever_reranker_final.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from rank_bm25 import BM25Okapi
3
+ from sentence_transformers import SentenceTransformer
4
+ from chunker_final import chunk_documents_to_dict
5
+ import numpy as np
6
+
7
+ class Retriever:
8
+ def __init__(self, docs: dict) -> None:
9
+ self.chunked_docs = chunk_documents_to_dict(docs)
10
+ self.chunk_ids = list(self.chunked_docs.keys())
11
+ self.chunk_texts = list(self.chunked_docs.values())
12
+ tokenized_chunks = [text.lower().split(" ") for text in self.chunk_texts]
13
+ self.bm25 = BM25Okapi(tokenized_chunks)
14
+ self.sbert = SentenceTransformer('sentence-transformers/all-distilroberta-v1')
15
+ self.doc_embeddings = self.sbert.encode(self.chunk_texts)
16
+ def get_docs(self, query, method, n=15) -> dict:
17
+
18
+ if method == "BM25":
19
+ scores = self._get_bm25_scores(query)
20
+ elif method == "semantic":
21
+ scores = self._get_semantic_scores(query)
22
+ elif method == "combined search":
23
+ bm25_scores = self._get_bm25_scores(query)
24
+ semantic_scores = self._get_semantic_scores(query)
25
+ scores = 0.3 * bm25_scores + 0.7 * semantic_scores
26
+ else:
27
+ raise ValueError(f"Invalid search method: {method}")
28
+
29
+ sorted_indices = scores.argsort(descending=True)
30
+ result = {self.chunk_ids[i]: self.chunk_texts[i] for i in sorted_indices[:n]}
31
+ return result
32
+
33
+ def rerank(self, query, retrieved_docs: dict) -> dict:
34
+ query_embedding = self.sbert.encode(query)
35
+ rerank_scores = {}
36
+
37
+ for chunk_id, chunk_text in retrieved_docs.items():
38
+ chunk_embedding = self.sbert.encode(chunk_text)
39
+ similarity = np.dot(query_embedding, chunk_embedding) / (
40
+ np.linalg.norm(query_embedding) * np.linalg.norm(chunk_embedding)
41
+ )
42
+ rerank_scores[chunk_id] = similarity
43
+
44
+ sorted_chunks = sorted(rerank_scores.items(), key=lambda x: x[1], reverse=True)
45
+
46
+ reranked_docs = {chunk_id: retrieved_docs[chunk_id] for chunk_id, _ in sorted_chunks}
47
+ return reranked_docs
48
+
49
+ def _get_bm25_scores(self, query):
50
+ tokenized_query = query.lower().split(" ")
51
+ return torch.tensor(self.bm25.get_scores(tokenized_query))
52
+
53
+ def _get_semantic_scores(self, query):
54
+ query_embedding = self.sbert.encode(query)
55
+ scores = np.dot(self.doc_embeddings, query_embedding) / (
56
+ np.linalg.norm(self.doc_embeddings, axis=1) * np.linalg.norm(query_embedding)
57
+ )
58
+ return torch.tensor(scores)