File size: 1,675 Bytes
9e5bfe7
 
 
7ed7214
 
 
 
c34a675
470394e
 
c34a675
470394e
7ed7214
 
 
 
c71cc0f
 
398d8c1
 
 
 
7ed7214
 
c34a675
9e5bfe7
 
c34a675
 
 
 
7ed7214
c34a675
 
b89dcf1
9e5bfe7
 
 
398d8c1
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
import gradio as gr
from transformers import pipeline

# Load the text generation model
text_generator = pipeline("text-generation", model="gpt2")

# Define the function for story generation
def generate_story(prompt, word_count):
    # Calculate the maximum length based on word count
    max_length = word_count + len(prompt.split())
    # Generate a story based on the user's prompt and word count
    generated_text = text_generator(prompt, max_length=max_length, num_return_sequences=1)[0]['generated_text']
    return generated_text

# Define example inputs for the Gradio interface
example_inputs = [
    ["Once upon a time, in a magical forest, there was a curious rabbit named Oliver.", 100],
    ["Amidst the hustle and bustle of a busy city, there lived a lonely street musician.", 150],
    ["On a distant planet, explorers discovered an ancient alien artifact buried in the sand.", 200],
    ["Hidden in the attic of an old house, a forgotten journal revealed a family secret.", 250],
    ["In a futuristic world, a brilliant scientist invented a time-traveling device.", 300],
    ["Deep in the ocean, an underwater explorer encountered a mysterious and ancient creature.", 350]
]

# Create a Gradio interface with examples and a word count slider
iface = gr.Interface(
    fn=generate_story,
    inputs=[
        gr.components.Textbox(label="Prompt"),
        gr.components.Slider(minimum=50, maximum=500, default=100, label="Word Count")
    ],
    outputs="text",
    title="Story Generator with Word Count",
    description="Enter a prompt and select the word count to generate a story.",
    examples=example_inputs
)

# Launch the interface
iface.launch()