Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| from transformers import pipeline | |
| MODEL_ID = "Amitesh007/text_generation-finetuned-gpt2" | |
| # β Load PyTorch model explicitly | |
| generator = pipeline( | |
| task="text-generation", | |
| model=MODEL_ID, | |
| framework="pt", # π FIX | |
| device=-1 # CPU (use 0 for GPU) | |
| ) | |
| def generate(text): | |
| if not text or not text.strip(): | |
| return "Please enter some text.", "" | |
| outputs = generator( | |
| text, | |
| max_length=120, | |
| num_return_sequences=2, | |
| do_sample=True, | |
| temperature=0.8, | |
| top_p=0.95 | |
| ) | |
| return outputs[0]["generated_text"], outputs[1]["generated_text"] | |
| with gr.Blocks(title="GPT-2 Text Generator") as demo: | |
| gr.Markdown( | |
| """ | |
| # βοΈ GPT-2 Text Generator | |
| Fine-tuned GPT-2 model trained on a *Game of Thrones* dataset. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox( | |
| lines=5, | |
| label="Input Prompt", | |
| placeholder="Type a sentence to start generating text..." | |
| ) | |
| generate_button = gr.Button("Generate π") | |
| with gr.Column(): | |
| output_text1 = gr.Textbox(label="Generated Text 1", lines=6) | |
| output_text2 = gr.Textbox(label="Generated Text 2", lines=6) | |
| gr.Examples( | |
| examples=[ | |
| "A light snow had fallen the night before, and there were", | |
| "The pig face had been smashed in with a mace, but Tyrion" | |
| ], | |
| inputs=input_text | |
| ) | |
| generate_button.click( | |
| fn=generate, | |
| inputs=input_text, | |
| outputs=[output_text1, output_text2] | |
| ) | |
| demo.queue(concurrency_count=2) | |
| demo.launch() | |