File size: 2,024 Bytes
08437dc 88a6999 abdc137 08437dc af088bf abdc137 af088bf 88a6999 af088bf 08437dc af088bf 88a6999 af088bf 88a6999 6a45166 88a6999 af088bf abdc137 af088bf abdc137 af088bf abdc137 af088bf 88a6999 af088bf abdc137 af088bf 88a6999 abdc137 82883e2 af088bf |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import gradio as gr
from transformers import pipeline, set_seed
import os
# Set environment variables for CPU optimization
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Load model once at startup
generator = pipeline(
"text-generation",
model="openai-community/openai-gpt",
device=-1 # Auto-detect CPU
)
def generate_text(prompt, max_length=30, num_return_sequences=3, seed=42):
"""Generate text with configurable parameters and seed"""
set_seed(seed)
results = generator(
prompt,
max_length=max_length,
num_return_sequences=num_return_sequences,
truncation=True,
pad_token_id=generator.tokenizer.eos_token_id
)
# Format results with numbering
return "\n\n".join([f"{i+1}. {r['generated_text']}" for i, r in enumerate(results)])
# Define UI
with gr.Blocks(theme="soft", title="GPT-1 Text Generator") as demo:
gr.Markdown("""
# π GPT-1 Text Generation Demo
Run on free HuggingFace CPU β’ Model: [openai-community/openai-gpt](https://huggingface.co/openai-community/openai-gpt )
*Note: This is the original 2018 GPT model with known limitations and biases. For production use, consider newer models.*
""")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Input Prompt",
placeholder="Enter your prompt here...",
lines=4
)
seed = gr.Number(value=42, label="Random Seed")
with gr.Column():
max_length = gr.Slider(10, 100, value=30, step=5, label="Max Output Length")
num_return_sequences = gr.Slider(1, 5, value=3, step=1, label="Number of Variants")
generate_btn = gr.Button("β¨ Generate Text")
output = gr.Textbox(label="Generated Results", lines=10)
generate_btn.click(
fn=generate_text,
inputs=[prompt, max_length, num_return_sequences, seed],
outputs=output
)
if __name__ == "__main__":
demo.launch() |