Kumarkishalaya commited on
Commit
4619405
1 Parent(s): 6b02864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -7,19 +7,30 @@ trained_model = GPT2LMHeadModel.from_pretrained("Kumarkishalaya/GPT-2-next-word-
7
  untrained_model = GPT2Tokenizer.from_pretrained("gpt2")
8
  untrained_tokenizer = ("gpt2")
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
10
 
11
  def generate(commentary_text):
12
- input_ids = trained_tokenizer(commentary_text, return_tensors="pt")
13
- input_ids = input_ids['input_ids'].to(device)
14
- output = trained_model.generate(input_ids, max_length=60, num_beams=5, do_sample=False)
15
- return trained_tokenizer.decode(output[0])
 
 
 
 
 
 
 
16
 
17
  # Create Gradio interface
18
- iface = gr.Interface(fn=generate,
19
- inputs="text",
20
- outputs="text",
21
- title="GPT-2 Text Generation",
22
- description="Enter a prompt and GPT-2 will generate the continuation of the text.")
 
 
23
 
24
  # Launch the app
25
  if __name__ == "__main__":
 
7
  untrained_model = GPT2Tokenizer.from_pretrained("gpt2")
8
  untrained_tokenizer = ("gpt2")
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ trained_model.to(device)
11
+ untrained_model.to(device)
12
 
13
  def generate(commentary_text):
14
+ # Generate text using the trained model
15
+ input_ids = trained_tokenizer(commentary_text, return_tensors="pt").input_ids.to(device)
16
+ trained_output = trained_model.generate(input_ids, max_length=60, num_beams=5, do_sample=False)
17
+ trained_text = trained_tokenizer.decode(trained_output[0], skip_special_tokens=True)
18
+
19
+ # Generate text using the untrained model
20
+ input_ids = untrained_tokenizer(commentary_text, return_tensors="pt").input_ids.to(device)
21
+ untrained_output = untrained_model.generate(input_ids, max_length=60, num_beams=5, do_sample=False)
22
+ untrained_text = untrained_tokenizer.decode(untrained_output[0], skip_special_tokens=True)
23
+
24
+ return trained_text, untrained_text
25
 
26
  # Create Gradio interface
27
+ iface = gr.Interface(
28
+ fn=generate,
29
+ inputs="text",
30
+ outputs=["text", "text"],
31
+ title="GPT-2 Text Generation",
32
+ description="start writing a cricket commentary and GPT-2 will continue it using both a trained and untrained model."
33
+ )
34
 
35
  # Launch the app
36
  if __name__ == "__main__":