vivekkumarbarman
commited on
Commit
•
1e537fd
1
Parent(s):
30d4848
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
|
4 |
+
# Load the GPT-2 model and tokenizer
|
5 |
+
model_name = "gpt2"
|
6 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Define the sentence completion function
|
10 |
+
def complete_sentence(sentence):
|
11 |
+
input_ids = tokenizer.encode(sentence, return_tensors="pt")
|
12 |
+
output = model.generate(input_ids, max_length=50, num_return_sequences=1)
|
13 |
+
completed_sentence = tokenizer.decode(output[0], skip_special_tokens=True)
|
14 |
+
return completed_sentence
|
15 |
+
|
16 |
+
# Create the Gradio interface
|
17 |
+
iface = gr.Interface(
|
18 |
+
fn=complete_sentence,
|
19 |
+
inputs="text",
|
20 |
+
outputs="text",
|
21 |
+
title="Sentence Completion",
|
22 |
+
description="Enter a sentence to complete",
|
23 |
+
example="I love to"
|
24 |
+
)
|
25 |
+
|
26 |
+
# Launch the Gradio interface
|
27 |
+
if __name__ == "__main__":
|
28 |
+
iface.launch()
|