Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
4 |
+
|
5 |
+
# Load pre-trained model and tokenizer
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
7 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
8 |
+
|
9 |
+
# Define a function to generate text
|
10 |
+
def generate_text(input_text):
|
11 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
12 |
+
|
13 |
+
# Generate text
|
14 |
+
with torch.no_grad():
|
15 |
+
output = model.generate(input_ids, max_length=100, no_repeat_ngram_size=True, do_sample=True, temperature=0.9)
|
16 |
+
|
17 |
+
# Decode the generated text
|
18 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
19 |
+
|
20 |
+
return generated_text
|
21 |
+
|
22 |
+
# Create a Gradio interface
|
23 |
+
|
24 |
+
# Create a Gradio interface
|
25 |
+
iface = gr.Interface(
|
26 |
+
generate_text,
|
27 |
+
gr.Textbox(lines=5, label="Input Text"),
|
28 |
+
"textbox",
|
29 |
+
examples=[
|
30 |
+
["Once upon a time, in a land far, far away..."]
|
31 |
+
],
|
32 |
+
title="GPT-2 Text Generation",
|
33 |
+
description="Enter some text and GPT-2 will generate more!",
|
34 |
+
theme="compact"
|
35 |
+
)
|
36 |
+
iface.launch()
|