Spaces:
Paused
Paused
import tiktoken | |
import torch | |
import gradio as gr | |
import model | |
from model import run_train, gen_text | |
class GPTConfig: | |
block_size: int = 1024 # max sequence length | |
vocab_size: int = 50304 # number of tokens: 50,000 BPE merges + 256 bytes tokens + 1 <|endoftext|> token | |
n_layer: int = 12 # number of layers | |
n_head: int = 12 # number of heads | |
n_embd: int = 768 # embedding dimension | |
# Define generation function | |
def generate_text(prompt, max_length=50, num_return_sequences=10): | |
""" Generates humorous text using the GPT-2 model based on the provided prompt and user-specified parameters. | |
Args: | |
prompt (str): The starting text for the generation. | |
max_length (int, optional): The maximum length of the generated text. Defaults to 100. | |
num_return_sequences (int, optional): The number of different text sequences to generate. Defaults to 10. | |
Returns: | |
list: A list of generated humorous text sequences. | |
""" | |
start_tokens = prompt | |
generated_texts = gen_text(model2, start_tokens, max_length, num_return_sequences) | |
return generated_texts | |
# Humorous prompt options | |
prompts = [ | |
"The automatic doors at the grocery store, tired of people holding them open for conversations, developed a mischievous sense of humor.", | |
"The self-driving car, fed up with rush hour traffic, decided to take a scenic detour through the countryside.", | |
"The fridge goes on a hunger strike", | |
"A colony of ants, inspired by a motivational poster, embarked on a quest to climb the tallest tree in the garden.", | |
"A particularly chatty parrot accidentally spilled the villain's evil plan during a casual conversation with the local mailman.", | |
"TA squirrel declares war on the birdfeeder...", | |
"The refrigerator, overflowing with forgotten groceries, staged a silent protest, refusing to cool anything until some order was restored.", | |
"A fortune cookie predicts world domination" | |
] | |
# Gradio interface with user inputs and dropdown for prompt selection | |
interface = gr.Interface( | |
fn=generate_text, | |
inputs=[ | |
gr.Dropdown(choices=prompts, label="Pre-defined Prompt"), | |
gr.Slider(minimum=10, maximum=200, label="Max Text Length", value=100, step = 1), | |
gr.Slider(minimum=1, maximum=20, label="Number of Outputs", value=10,step = 1) | |
], | |
outputs="text", | |
title="Humorous Text Generator with GPT-2", | |
description="Get a chuckle with AI-generated funny stories! Provide a prompt (or choose one), adjust the desired text length and number of outputs, and let the AI do the rest!", | |
) | |
# Launch the Gradio app | |
interface.launch() |