mohamedemam commited on
Commit
3292ea1
1 Parent(s): 098b4c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -37
app.py CHANGED
@@ -1,35 +1,6 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
- # Load the tokenizer and model
5
- model_name = "mohamedemam/QA_GeneraToR"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
-
9
- # Recommended words for users to choose from
10
- recommended_words = [
11
- "which", "how", "when", "where", "who", "whom", "whose", "why",
12
- "which", "who", "whom", "whose", "whereas",
13
- "can", "could", "may", "might", "will", "would", "shall", "should",
14
- "do", "does", "did", "is", "are", "am", "was", "were", "be", "being", "been",
15
- "have", "has", "had", "if", "is", "are", "am", "was", "were", "do", "does", "did", "can", "could",
16
- "will", "would", "shall", "should", "might", "may", "must",
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)
@@ -43,6 +14,17 @@ recommended_words = [
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}"
@@ -53,26 +35,29 @@ def generate_qa(context, recommended_word, temperature, top_p, num_samples=3):
53
  **input_ids,
54
  temperature=temperature,
55
  top_p=top_p,
56
- num_return_sequences=num_samples,do_sample=True, top_k=0
57
-
 
 
 
 
58
  )
59
 
60
  generated_text = tokenizer.batch_decode(output, skip_special_tokens=True)
61
  return generated_text
62
 
63
- # Create the Gradio interface with sliders for temperature and top-p
64
  # Create the Gradio interface with sliders for temperature and top-p
65
  iface = gr.Interface(
66
  fn=generate_qa,
67
  inputs=[
68
- "text",
69
  gr.inputs.Radio(recommended_words, label="Choose a Recommended Word"),
70
- gr.inputs.Slider(minimum=0.2, maximum=2, default=1, step=0.1, label="Temperature"),
71
- gr.inputs.Slider(minimum=0.1, maximum=1, default=0.8, step=0.1, label="Top-p")
72
  ],
73
- outputs="text",
74
  title="Question Generation and Answering",
75
- description="Enter a context, choose a recommended word, and adjust temperature and top-p. The model will generate a question and answer.",
76
  )
77
 
78
  # Launch the interface
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  # Load the tokenizer and model
5
  model_name = "mohamedemam/QA_GeneraTor"
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
14
  "have", "has", "had", "if", "must",
15
  ]
16
 
17
+ # Example contexts
18
+ example_contexts = [
19
+ "when: Lionel Andrés Messi...",
20
+ "where: Lionel Andrés Messi...",
21
+ "how: Lionel Andrés Messi...",
22
+ "what: Lionel Andrés Messi...",
23
+ "where: Egypt...",
24
+ "where: There is evidence..."
25
+ # Add more examples here
26
+ ]
27
+
28
  # Function to generate questions and answers with configurable parameters
29
  def generate_qa(context, recommended_word, temperature, top_p, num_samples=3):
30
  input_text = f"{recommended_word}: {context}"
 
35
  **input_ids,
36
  temperature=temperature,
37
  top_p=top_p,
38
+ num_return_sequences=3,
39
+ do_sample=True,
40
+ max_length=100,
41
+ num_beams=6,
42
+ length_penalty=1,
43
+ top_k=0
44
  )
45
 
46
  generated_text = tokenizer.batch_decode(output, skip_special_tokens=True)
47
  return generated_text
48
 
 
49
  # Create the Gradio interface with sliders for temperature and top-p
50
  iface = gr.Interface(
51
  fn=generate_qa,
52
  inputs=[
53
+ gr.inputs.Dropdown(example_contexts, label="Choose an Example"),
54
  gr.inputs.Radio(recommended_words, label="Choose a Recommended Word"),
55
+ gr.inputs.Slider(minimum=0.0, maximum=2, default=2.1, step=0.1, label="Temperature"),
56
+ gr.inputs.Slider(minimum=0.0, maximum=1, default=0.5, step=0.1, label="Top-p")
57
  ],
58
+ outputs=gr.outputs.Table(columns=["Generated Sentences"]),
59
  title="Question Generation and Answering",
60
+ description="Select an example context, choose a recommended word, adjust temperature and top-p. The model will generate questions and answers.",
61
  )
62
 
63
  # Launch the interface