Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tiktoken
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
import model
|
5 |
+
from model import run_train, gen_text
|
6 |
+
|
7 |
+
|
8 |
+
class GPTConfig:
|
9 |
+
block_size: int = 1024 # max sequence length
|
10 |
+
vocab_size: int = 50304 # number of tokens: 50,000 BPE merges + 256 bytes tokens + 1 <|endoftext|> token
|
11 |
+
n_layer: int = 12 # number of layers
|
12 |
+
n_head: int = 12 # number of heads
|
13 |
+
n_embd: int = 768 # embedding dimension
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
# Define generation function
|
18 |
+
def generate_text(prompt, max_length=50, num_return_sequences=10):
|
19 |
+
""" Generates humorous text using the GPT-2 model based on the provided prompt and user-specified parameters.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
prompt (str): The starting text for the generation.
|
23 |
+
max_length (int, optional): The maximum length of the generated text. Defaults to 100.
|
24 |
+
num_return_sequences (int, optional): The number of different text sequences to generate. Defaults to 10.
|
25 |
+
|
26 |
+
Returns:
|
27 |
+
list: A list of generated humorous text sequences.
|
28 |
+
"""
|
29 |
+
|
30 |
+
start_tokens = prompt
|
31 |
+
generated_texts = gen_text(model2, start_tokens, max_length, num_return_sequences)
|
32 |
+
return generated_texts
|
33 |
+
|
34 |
+
# Humorous prompt options
|
35 |
+
prompts = [
|
36 |
+
"The automatic doors at the grocery store, tired of people holding them open for conversations, developed a mischievous sense of humor.",
|
37 |
+
"The self-driving car, fed up with rush hour traffic, decided to take a scenic detour through the countryside.",
|
38 |
+
"The fridge goes on a hunger strike",
|
39 |
+
"A colony of ants, inspired by a motivational poster, embarked on a quest to climb the tallest tree in the garden.",
|
40 |
+
"A particularly chatty parrot accidentally spilled the villain's evil plan during a casual conversation with the local mailman.",
|
41 |
+
"TA squirrel declares war on the birdfeeder...",
|
42 |
+
"The refrigerator, overflowing with forgotten groceries, staged a silent protest, refusing to cool anything until some order was restored.",
|
43 |
+
"A fortune cookie predicts world domination"
|
44 |
+
]
|
45 |
+
|
46 |
+
# Gradio interface with user inputs and dropdown for prompt selection
|
47 |
+
interface = gr.Interface(
|
48 |
+
fn=generate_text,
|
49 |
+
inputs=[
|
50 |
+
gr.Dropdown(choices=prompts, label="Pre-defined Prompt"),
|
51 |
+
gr.Slider(minimum=10, maximum=200, label="Max Text Length", value=100, step = 1),
|
52 |
+
gr.Slider(minimum=1, maximum=20, label="Number of Outputs", value=10,step = 1)
|
53 |
+
],
|
54 |
+
outputs="text",
|
55 |
+
title="Humorous Text Generator with GPT-2",
|
56 |
+
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!",
|
57 |
+
)
|
58 |
+
|
59 |
+
# Launch the Gradio app
|
60 |
+
interface.launch()
|