Stkong
Add application file
ae31f55
raw
history blame contribute delete
No virus
10.1 kB
# Example context and question
example_1 = "The galaxy is in the midst of a civil war. Spies for the Rebel Alliance have stolen plans to the Galactic Empire's Death Star, a heavily armed space station capable of destroying planets. Rebel leader Princess Leia has the plans, but her ship is captured by Imperial forces under the command of the evil Darth Vader. Before she is captured, Leia hides the plans in the memory of an astromech droid, R2-D2, along with a holographic recording. The droid flees to the surface of the desert planet Tatooine with C-3PO, a protocol droid. The droids are captured by Jawa traders, who sell them to moisture farmers Owen and Beru Lars and their nephew, Luke Skywalker. While cleaning R2-D2, Luke accidentally triggers part of Leia's message, in which she requests help from Obi-Wan Kenobi. The next morning, Luke finds R2-D2 searching for Obi-Wan, and meets Ben Kenobi, an old hermit who lives in the hills and reveals himself to be Obi-Wan. Obi-Wan tells Luke of his days as a Jedi Knight, former Galactic Republic peacekeepers with supernatural powers derived from an energy called The Force, who were all but wiped out by the Empire. Contrary to his uncle's statements, Luke learns that his father, Anakin, fought alongside Obi-Wan as a Jedi Knight. Obi-Wan tells Luke that Vader was his former pupil who turned to the dark side of the Force and killed Anakin. Obi-Wan presents Luke his father's weapon - a lightsaber. Obi-Wan views Leia's complete message, in which she begs him to take the Death Star plans to her home planet of Alderaan and give them to her father for analysis. Obi-Wan invites Luke to accompany him to Alderaan and learn the ways of the Force. Luke declines, but changes his mind after discovering that Imperial stormtroopers searching for C-3PO and R2-D2 have destroyed his home and killed his aunt and uncle. Obi-Wan and Luke hire smuggler Han Solo and his Wookiee first mate Chewbacca to transport them to Alderaan on Han's ship, the Millennium Falcon. Upon the Falcon's arrival at the location of Alderaan, the group discover that the planet has been destroyed by order of the Death Star's commanding officer, Grand Moff Tarkin, as a show of power. The Falcon is captured by the Death Star's tractor beam and brought into its hangar bay. While Obi-Wan goes to disable the tractor beam, Luke discovers that Leia is imprisoned aboard, and with the help of Han and Chewbacca, rescues her. After several escapes, the group makes its way back to the Falcon. Obi-Wan disables the tractor beam, and on the way back to the Falcon, he engages in a lightsaber duel with Vader. Once he is sure the others can escape, Obi-Wan allows himself to be killed. The Falcon escapes the Death Star, unknowingly carrying a tracking beacon, which the Empire follows to the Rebels' hidden base on Yavin IV. The Rebels analyze the Death Star's plans and identify a vulnerable exhaust port that connects to the station's main reactor. Luke joins the Rebel assault squadron, while Han collects his payment for the transport and intends to leave, despite Luke's request that he stay and help. In the ensuing battle, the Rebels suffer heavy losses after several unsuccessful attack runs, leaving Luke as one of the few surviving pilots. Vader leads a squad of TIE fighters and prepares to attack Luke's X-wing fighter, but Han returns and fires on the Imperials, sending Vader spiraling away. Helped by guidance from Obi-Wan's spirit, Luke uses the Force and successfully destroys the Death Star seconds before it can fire on the Rebel base. Leia awards Luke and Han with medals for their heroism."
qst_1 = "What is a Jedi's main weapon?"
ground_truth_1 = "lightsaber"
example_2 = """As a spacecraft departs a planet, a humanoid alien drinks an iridescent liquid then dissolves. The remains of the alien cascade into a waterfall. The alien's DNA strands mix with the water. In 2089, archaeologists Elizabeth Shaw and Charlie Holloway discover a star map in Scotland that matches others from several unconnected ancient cultures. They interpret this as an invitation from humanity's forerunners, the "Engineers". Peter Weyland, the elderly CEO of Weyland Corporation, funds an expedition to follow the map to the distant moon LV-223 aboard the scientific vessel Prometheus. The ship's crew travels in stasis while the android David monitors their voyage. Arriving in 2093, mission director Meredith Vickers informs them of their mission to find the Engineers, and not to make contact without her permission. The Prometheus lands on the barren, mountainous surface near a large artificial structure, which a team explores. Inside they find stone cylinders, a monolithic statue of a humanoid head, and the decapitated corpse of a large alien, thought to be an Engineer; Shaw recovers its head. The crew finds other bodies, leading them to surmise the species is extinct. Crew members Millburn and Fifield grow uncomfortable with the discoveries and attempt to return to Prometheus, but become stranded in the structure when they get lost. The expedition is cut short when a storm forces the crew to return to the ship. David secretly takes a cylinder from the structure, while the remaining cylinders begin leaking a dark liquid. In the ship's lab, the Engineer's DNA is found to match that of humans. David investigates the cylinder and the liquid inside. He intentionally taints a drink with the liquid and gives it to the unsuspecting Holloway, who had stated he would do anything for answers. Shortly after, Shaw and Holloway have sex. Inside the structure, a snake-like creature kills Millburn and sprays a corrosive fluid that melts Fifield's helmet. Fifield falls face-first into a puddle of dark liquid. When the crew returns, they find Millburn's corpse. David separately discovers a control room containing a surviving Engineer in stasis, and a star map highlighting Earth. Meanwhile, Holloway sickens rapidly. He is rushed back to Prometheus, but Vickers refuses to let him aboard, and at his urging, burns him to death with a flamethrower. Later, a medical scan reveals that Shaw, despite being sterile, is pregnant. Fearing the worst, she uses an automated surgery table to extract a squid-like creature from her abdomen. Shaw then discovers that Weyland has been in stasis aboard Prometheus. He explains that he wants to ask the Engineers to prevent his death from old age. As Weyland prepares to leave for the structure, Vickers addresses him as "Father." A monstrous, mutated Fifield attacks the Prometheus's hangar bay and kills several crew members before he is killed. The Prometheus's captain, Janek, speculates that the structure was an Engineer military installation that lost control of a virulent biological weapon, the dark liquid. He also determines that the structure houses a spacecraft. Weyland and a team return to the structure, accompanied by Shaw. David wakes the Engineer from stasis and speaks to him in an attempt to explain what Weyland wants. The Engineer responds by decapitating David and killing Weyland and his team, before reactivating the spacecraft. Shaw flees and warns Janek that the Engineer is planning to release the liquid on Earth, convincing him to stop the spacecraft. Janek ejects the lifeboat and rams Prometheus into the alien craft, while Vickers flees in an escape pod. The Engineer's disabled spacecraft crashes onto the ground, killing Vickers. Shaw goes to the lifeboat and finds her alien offspring is alive and has grown to gigantic size. David's still-active head warns Shaw that the Engineer has survived. The Engineer forces open the lifeboat's airlock and attacks Shaw, who releases her alien offspring onto the Engineer; it thrusts an ovipositor down the Engineer's throat, subduing him. Shaw recovers David's remains, and with his help, launches another Engineer spacecraft. She intends to reach the Engineers' homeworld in an attempt to understand why they wanted to destroy humanity. In the lifeboat, an alien creature bursts out of the Engineer's chest."""
qst_2 = "Where does the alien burst out of?"
ground_truth_2 = "Engineer's chest"
# Import libraries and set up the models
import json
import torch
import gradio as gr
from transformers import pipeline
# Initialize two different pipelines
model1 = pipeline("question-answering", model="suthanhcong/bert-finetuned-squad-finetuned-DouRC_squad")
model3 = pipeline("question-answering", model="suthanhcong/roberta-base-squad2-finetuned-DouRC_squad")
# model2 = pipeline("question-answering", model="suthanhcong/distilbert-base-uncased-finetuned-DouRC_squad")
# Function to get answers from both models
def func(question, ground_truth, context):
# result1 = model1(question=question, context=context)
# result2 = model2(question=question, context=context)
result1 = model3(question=question, context=context)
# Prepare the results as dictionaries
result1_dict = {"Answer": result1['answer']}
# result2_dict = {"Model 2": result2}
# result3_dict = {"Model 3": result3}
ground_truth_dict = {"Ground Truth": ground_truth}
return ground_truth_dict, result1_dict
# , result2_dict, result3_dict
# Creating the interface
app = gr.Interface(
fn=func,
inputs=[
gr.Textbox(label="Question", lines=2, placeholder="Enter the question here..."),
gr.Textbox(label="Ground Truth", lines=2, placeholder="Enter the ground truth here..."),
gr.Textbox(label="Context", lines=10, placeholder="Enter the context here..."),
],
outputs=[
gr.JSON(label="Ground Truth"),
gr.JSON(label="BERT"),
# gr.JSON(label="DistilBERT base uncased"),
# gr.JSON(label="RoBERTa base uncased"),
],
theme = gr.Theme.from_hub("bethecloud/storj_theme"),
title='Question Answering Bot with Two Models and Ground Truth',
description='Input question, context, and ground truth, then get answers from two models!',
examples=[[qst_1,ground_truth_1, example_1 ],[qst_2,ground_truth_2, example_2 ]]
)
# Launch the app
app.launch(inline=False)