Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
4 |
+
|
5 |
+
# Load the pre-trained GPT-2 model and tokenizer
|
6 |
+
model_name = "gpt2" # You can change this to another model if needed
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
8 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
9 |
+
|
10 |
+
def generate_text(prompt):
|
11 |
+
# Encode the input text and generate a continuation
|
12 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
13 |
+
output = model.generate(input_ids, max_length=100, num_return_sequences=1, pad_token_id=50256)
|
14 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
15 |
+
|
16 |
+
# Save the generated text to a text file
|
17 |
+
with open("generated_text.txt", "w") as file:
|
18 |
+
file.write(generated_text)
|
19 |
+
|
20 |
+
return generated_text
|
21 |
+
|
22 |
+
iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
|
23 |
+
iface.launch()
|