Spaces:
Sleeping
Sleeping
mohamedemam
commited on
Commit
•
ca6c9ce
1
Parent(s):
eda23da
Update app.py
Browse files
app.py
CHANGED
@@ -17,40 +17,57 @@ recommended_words = [
|
|
17 |
"may", "might", "must",
|
18 |
]
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Function to generate questions and answers with configurable parameters
|
21 |
-
def generate_qa(context, temperature, top_p,
|
22 |
-
|
23 |
-
input_text =
|
24 |
-
input_ids = tokenizer(input_text, max_length=400, truncation=True, padding="max_length", return_tensors='pt')
|
25 |
|
26 |
# Generate with configurable parameters
|
27 |
output = model.generate(
|
28 |
**input_ids,
|
29 |
temperature=temperature,
|
30 |
-
top_p=top_p,
|
|
|
31 |
)
|
32 |
|
33 |
generated_text = tokenizer.batch_decode(output, skip_special_tokens=True)
|
34 |
return generated_text
|
35 |
-
example_contexts=[ "when: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
36 |
-
, "where: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
37 |
-
, "how: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
38 |
-
, "what: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
39 |
-
,"where: Egypt (Egyptian Arabic: مصر Maṣr Egyptian Arabic pronunciation: [mɑsˤr]), officially the Arab Republic of Egypt, is a transcontinental country spanning the northeast corner of Africa and the Sinai Peninsula in the southwest corner of Asia. It is bordered by the Mediterranean Sea to the north, the Gaza Strip of Palestine and Israel to the northeast, the Red Sea to the east, Sudan to the south, and Libya to the west. The Gulf of Aqaba in the northeast separates Egypt from Jordan and Saudi Arabia. Cairo is the capital and largest city of Egypt, while Alexandria, the second-largest city, is an important industrial and tourist hub at the Mediterranean coast.[11] At approximately 100 million inhabitants, Egypt is the 14th-most populated country in the world, and the third-most populated in Africa, behind Nigeria and Ethiopia."
|
40 |
-
, "where: There is evidence of rock carvings along the Nile terraces and in desert oases. In the 10th millennium BCE, a culture of hunter-gatherers and fishers was replaced by a grain-grinding culture. Climate changes or overgrazing around 8000 BCE began to desiccate the pastoral lands of Egypt, forming the Sahara. Early tribal peoples migrated to the Nile River where they developed a settled agricultural economy and more centralized society."
|
41 |
-
, "when: By about 6000 BCE, a Neolithic culture took root in the Nile Valley.[31] During the Neolithic era, several predynastic cultures developed independently in Upper and Lower Egypt. The Badarian culture and the successor Naqada series are generally regarded as precursors to dynastic Egypt. The earliest known Lower Egyptian site, Merimda, predates the Badarian by about seven hundred years. Contemporaneous Lower Egyptian communities coexisted with their southern counterparts for more than two thousand years. The earliest known evidence of Egyptian hieroglyphic inscriptions appeared during the predynastic period on Naqada III pottery vessels, dated to about 3200 BCE.[32]"
|
42 |
-
, "whose : or the next three millennia. Egyptian culture flourished during this long period and remained distinctively Egyptian in its religion, arts, language and customs. The first two ruling dynasties of a unified Egypt set the stage for the Old Kingdom period, c. 2700–2200 BCE, which constructed many pyramids, most notably the Third Dynasty pyramid of Djoser and the Fourth Dynasty Giza pyramids."
|
43 |
-
, "who:The First Intermediate Period ushered in a time of political upheaval for about 150 years.[33] Stronger Nile floods and stabilisation of government, however, brought back renewed prosperity for the country in the Middle Kingdom c. 2040 BCE, reaching a peak during the reign of Pharaoh Amenemhat III. A second period of disunity heralded the arrival of the first foreign ruling dynasty in Egypt, that of the Semitic Hyksos. The Hyksos invaders took over much of Lower Egypt around 1650 BCE and founded a new capital at Avaris. They were driven out by an Upper Egyptian force led by Ahmose I, who founded the Eighteenth Dynasty and relocated the capital from Memphis to Thebes."]
|
44 |
|
45 |
# Create the Gradio interface with sliders for temperature and top-p
|
46 |
iface = gr.Interface(
|
47 |
fn=generate_qa,
|
48 |
-
inputs=[
|
49 |
-
|
50 |
-
gr.inputs.
|
51 |
-
gr.inputs.Slider(minimum=0.
|
52 |
-
gr.inputs.Slider(minimum=1, maximum=
|
53 |
-
|
54 |
],
|
55 |
outputs=gr.outputs.Textbox(label="Generated Sentences", type="list"),
|
56 |
title="Question Generation and Answering",
|
@@ -58,4 +75,4 @@ iface = gr.Interface(
|
|
58 |
)
|
59 |
|
60 |
# Launch the interface
|
61 |
-
iface.launch()
|
|
|
17 |
"may", "might", "must",
|
18 |
]
|
19 |
|
20 |
+
#
|
21 |
+
example_contexts=[ "when: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
22 |
+
, "where: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
23 |
+
, "how: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
24 |
+
, "what: Lionel Andrés Messi[note 1] (Spanish pronunciation: [ljoˈnel anˈdɾes ˈmesi] (listen); born 24 June 1987), also known as Leo Messi, is an Argentine professional footballer who plays as a forward for and captains both Major League Soccer club Inter Miami and the Argentina national team. Widely regarded as one of the greatest players of all time, Messi has won a record seven Ballon d'Or awards[note 2] and a record six European Golden Shoes, and in 2020 he was named to the Ballon d'Or Dream Team. Until leaving the club in 2021, he had spent his entire professional career with Barcelona, where he won a club-record 34"
|
25 |
+
,"where: Egypt (Egyptian Arabic: مصر Maṣr Egyptian Arabic pronunciation: [mɑsˤr]), officially the Arab Republic of Egypt, is a transcontinental country spanning the northeast corner of Africa and the Sinai Peninsula in the southwest corner of Asia. It is bordered by the Mediterranean Sea to the north, the Gaza Strip of Palestine and Israel to the northeast, the Red Sea to the east, Sudan to the south, and Libya to the west. The Gulf of Aqaba in the northeast separates Egypt from Jordan and Saudi Arabia. Cairo is the capital and largest city of Egypt, while Alexandria, the second-largest city, is an important industrial and tourist hub at the Mediterranean coast.[11] At approximately 100 million inhabitants, Egypt is the 14th-most populated country in the world, and the third-most populated in Africa, behind Nigeria and Ethiopia."
|
26 |
+
, "where: There is evidence of rock carvings along the Nile terraces and in desert oases. In the 10th millennium BCE, a culture of hunter-gatherers and fishers was replaced by a grain-grinding culture. Climate changes or overgrazing around 8000 BCE began to desiccate the pastoral lands of Egypt, forming the Sahara. Early tribal peoples migrated to the Nile River where they developed a settled agricultural economy and more centralized society."
|
27 |
+
, "when: By about 6000 BCE, a Neolithic culture took root in the Nile Valley.[31] During the Neolithic era, several predynastic cultures developed independently in Upper and Lower Egypt. The Badarian culture and the successor Naqada series are generally regarded as precursors to dynastic Egypt. The earliest known Lower Egyptian site, Merimda, predates the Badarian by about seven hundred years. Contemporaneous Lower Egyptian communities coexisted with their southern counterparts for more than two thousand years. The earliest known evidence of Egyptian hieroglyphic inscriptions appeared during the predynastic period on Naqada III pottery vessels, dated to about 3200 BCE.[32]"
|
28 |
+
, "whose : or the next three millennia. Egyptian culture flourished during this long period and remained distinctively Egyptian in its religion, arts, language and customs. The first two ruling dynasties of a unified Egypt set the stage for the Old Kingdom period, c. 2700–2200 BCE, which constructed many pyramids, most notably the Third Dynasty pyramid of Djoser and the Fourth Dynasty Giza pyramids."
|
29 |
+
, "who:The First Intermediate Period ushered in a time of political upheaval for about 150 years.[33] Stronger Nile floods and stabilisation of government, however, brought back renewed prosperity for the country in the Middle Kingdom c. 2040 BCE, reaching a peak during the reign of Pharaoh Amenemhat III. A second period of disunity heralded the arrival of the first foreign ruling dynasty in Egypt, that of the Semitic Hyksos. The Hyksos invaders took over much of Lower Egypt around 1650 BCE and founded a new capital at Avaris. They were driven out by an Upper Egyptian force led by Ahmose I, who founded the Eighteenth Dynasty and relocated the capital from Memphis to Thebes."]
|
30 |
+
import gradio as gr
|
31 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
32 |
+
|
33 |
+
# Load the tokenizer and model
|
34 |
+
model_name = "mohamedemam/QA_GeneraTor"
|
35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
36 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
37 |
+
|
38 |
+
# Recommended words for users to choose from
|
39 |
+
recommended_words = [
|
40 |
+
"which", "how", "when", "where", "who", "whom", "whose", "why",
|
41 |
+
"can", "could", "may", "might", "will", "would", "shall", "should",
|
42 |
+
"do", "does", "did", "is", "are", "am", "was", "were", "be", "being", "been",
|
43 |
+
"have", "has", "had", "if", "must",
|
44 |
+
]
|
45 |
+
|
46 |
# Function to generate questions and answers with configurable parameters
|
47 |
+
def generate_qa(context, recommended_word, temperature, top_p, num_samples=3):
|
48 |
+
input_text = f"{recommended_word}: {context}"
|
49 |
+
input_ids = tokenizer(input_text, max_length=512, truncation=True, padding="max_length", return_tensors='pt')
|
|
|
50 |
|
51 |
# Generate with configurable parameters
|
52 |
output = model.generate(
|
53 |
**input_ids,
|
54 |
temperature=temperature,
|
55 |
+
top_p=top_p,
|
56 |
+
num_return_sequences=num_samples
|
57 |
)
|
58 |
|
59 |
generated_text = tokenizer.batch_decode(output, skip_special_tokens=True)
|
60 |
return generated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Create the Gradio interface with sliders for temperature and top-p
|
63 |
iface = gr.Interface(
|
64 |
fn=generate_qa,
|
65 |
+
inputs=[
|
66 |
+
"text",
|
67 |
+
gr.inputs.Radio(recommended_words, label="Choose a Recommended Word"),
|
68 |
+
gr.inputs.Slider(minimum=0.2, maximum=2, default=1, step=0.1, label="Temperature"),
|
69 |
+
gr.inputs.Slider(minimum=0.1, maximum=1, default=0.8, step=0.1, label="Top-p"),
|
70 |
+
gr.inputs.Number(default=3, label="Number of Samples")
|
71 |
],
|
72 |
outputs=gr.outputs.Textbox(label="Generated Sentences", type="list"),
|
73 |
title="Question Generation and Answering",
|
|
|
75 |
)
|
76 |
|
77 |
# Launch the interface
|
78 |
+
iface.launch()
|