added main file
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
# Check if GPU is available, otherwise use CPU
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
|
8 |
+
# Load pre-trained GPT-2 model and tokenizer
|
9 |
+
model_name = "gpt2-large"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
12 |
+
|
13 |
+
|
14 |
+
def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_repeat_ngram_size=2):
|
15 |
+
"""
|
16 |
+
Generate text based on the given input text.
|
17 |
+
|
18 |
+
Parameters:
|
19 |
+
- input_text (str): The input text to start generation from.
|
20 |
+
- max_length (int): Maximum length of the generated text.
|
21 |
+
- num_beams (int): Number of beams for beam search.
|
22 |
+
- do_sample (bool): Whether to use sampling or not.
|
23 |
+
- no_repeat_ngram_size (int): Size of the n-gram to avoid repetition.
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
- generated_text (str): The generated text.
|
27 |
+
"""
|
28 |
+
# Encode the input text and move it to the appropriate device
|
29 |
+
input_ids = tokenizer(input_text, return_tensors='pt')['input_ids'].to(device)
|
30 |
+
# Generate text using the model
|
31 |
+
output = model.generate(input_ids, max_length=max_length, num_beams=num_beams,
|
32 |
+
do_sample=do_sample, no_repeat_ngram_size=no_repeat_ngram_size)
|
33 |
+
# Decode the generated output
|
34 |
+
generated_text = tokenizer.decode(output[0])
|
35 |
+
return generated_text
|
36 |
+
|
37 |
+
|
38 |
+
def generate_text_with_nucleus_search(input_text, max_length=16, do_sample=True, top_p=0.9):
|
39 |
+
"""
|
40 |
+
Generate text with nucleus sampling based on the given input text.
|
41 |
+
|
42 |
+
Parameters:
|
43 |
+
- input_text (str): The input text to start generation from.
|
44 |
+
- max_length (int): Maximum length of the generated text.
|
45 |
+
- do_sample (bool): Whether to use sampling or not.
|
46 |
+
- top_p (float): Nucleus sampling parameter.
|
47 |
+
|
48 |
+
Returns:
|
49 |
+
- generated_text (str): The generated text.
|
50 |
+
"""
|
51 |
+
# Encode the input text and move it to the appropriate device
|
52 |
+
input_ids = tokenizer(input_text, return_tensors='pt')['input_ids'].to(device)
|
53 |
+
# Generate text using nucleus sampling
|
54 |
+
output = model.generate(input_ids, max_length=max_length, do_sample=do_sample, top_p=top_p)
|
55 |
+
# Decode the generated output
|
56 |
+
generated_text = tokenizer.decode(output[0])
|
57 |
+
return generated_text
|
58 |
+
|
59 |
+
|
60 |
+
# Create Gradio interface
|
61 |
+
input_textbox = gr.Textbox(lines=7, label="Input Text", placeholder="Enter your text here...")
|
62 |
+
output_textbox = gr.Textbox(label="Generated Text", placeholder="Generated text will appear here...")
|
63 |
+
|
64 |
+
gr.Interface(
|
65 |
+
[generate_text, generate_text_with_nucleus_search],
|
66 |
+
inputs=input_textbox,
|
67 |
+
outputs=output_textbox,
|
68 |
+
title="Text Generation with GPT-2",
|
69 |
+
description="Enter some text and generate new text using GPT-2 model.",
|
70 |
+
allow_flagging=False
|
71 |
+
).launch(share=True)
|