File size: 1,025 Bytes
f150bb6
 
 
4d75a6f
 
 
 
 
 
f150bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import gradio as gr
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import subprocess

upgrade_command = "python -m pip install --upgrade pip"

# Run the command
subprocess.run(upgrade_command, shell=True)

# Load the pre-trained GPT-2 model and tokenizer
model_name = "gpt2"  # You can change this to another model if needed
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

def generate_text(prompt):
    # Encode the input text and generate a continuation
    input_ids = tokenizer.encode(prompt, return_tensors="pt")
    output = model.generate(input_ids, max_length=100, num_return_sequences=1, pad_token_id=50256)
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
    
    # Save the generated text to a text file
    with open("generated_text.txt", "w") as file:
        file.write(generated_text)
    
    return generated_text

iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
iface.launch()