Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
# Load the gpt-neo-125M model for text generation
|
|
@@ -13,7 +13,7 @@ def start_story(prompt):
|
|
| 13 |
# Generate the initial story segment, ensuring the prompt is the starting point
|
| 14 |
result = generator(
|
| 15 |
prompt,
|
| 16 |
-
max_new_tokens=
|
| 17 |
temperature=0.7, # Lower temperature for less randomness
|
| 18 |
top_k=50, # Filter to top 50 likely next words
|
| 19 |
do_sample=True, # Enable sampling for creativity
|
|
@@ -29,7 +29,7 @@ def continue_story():
|
|
| 29 |
# Continue the story, using the current story as the prompt
|
| 30 |
result = generator(
|
| 31 |
current_story,
|
| 32 |
-
max_new_tokens=
|
| 33 |
temperature=0.7,
|
| 34 |
top_k=50,
|
| 35 |
do_sample=True,
|
|
@@ -63,128 +63,5 @@ with gr.Blocks(title="AI Story Generator") as demo:
|
|
| 63 |
continue_button.click(fn=continue_story, inputs=None, outputs=output)
|
| 64 |
reset_button.click(fn=reset_story, inputs=None, outputs=output)
|
| 65 |
|
| 66 |
-
# Launch the app
|
| 67 |
-
demo.launch()'''
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
import gradio as gr
|
| 71 |
-
from transformers import pipeline
|
| 72 |
-
|
| 73 |
-
# Load TinyLlama model for text generation
|
| 74 |
-
generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 75 |
-
|
| 76 |
-
# Store the current story and last options in global variables
|
| 77 |
-
current_story = ""
|
| 78 |
-
last_options = []
|
| 79 |
-
|
| 80 |
-
def start_story(prompt):
|
| 81 |
-
global current_story, last_options
|
| 82 |
-
# Format the prompt for TinyLlama to generate a detailed story
|
| 83 |
-
formatted_prompt = f"Human: Write a creative story starting with: '{prompt}' in about 100-150 words."
|
| 84 |
-
# Generate a longer initial story segment
|
| 85 |
-
result = generator(
|
| 86 |
-
formatted_prompt,
|
| 87 |
-
max_new_tokens=150, # ~100-150 words
|
| 88 |
-
temperature=0.7, # Controlled creativity
|
| 89 |
-
top_k=50, # Coherence
|
| 90 |
-
do_sample=True,
|
| 91 |
-
truncation=True
|
| 92 |
-
)[0]["generated_text"]
|
| 93 |
-
# Strip the "Human:" prefix from the output
|
| 94 |
-
story_text = result.replace(formatted_prompt, "").strip()
|
| 95 |
-
current_story = story_text
|
| 96 |
-
|
| 97 |
-
# Generate dynamic options based on the prompt
|
| 98 |
-
last_options = generate_options(prompt)
|
| 99 |
-
options_text = "\n\n**Choose an option:**\n" + "\n".join([f"{i+1}. {opt}" for i, opt in enumerate(last_options)])
|
| 100 |
-
return current_story + options_text
|
| 101 |
-
|
| 102 |
-
def continue_story(option_num):
|
| 103 |
-
global current_story, last_options
|
| 104 |
-
if not current_story:
|
| 105 |
-
return "Please start a story first!"
|
| 106 |
-
if not last_options or not (1 <= int(option_num) <= 3):
|
| 107 |
-
return "Invalid option or no story started yet!"
|
| 108 |
-
|
| 109 |
-
# Use the selected option to guide the next segment
|
| 110 |
-
chosen_option = last_options[int(option_num) - 1]
|
| 111 |
-
formatted_prompt = f"Human: Continue this story: '{current_story}' with this direction: '{chosen_option}' in about 100-150 words."
|
| 112 |
-
result = generator(
|
| 113 |
-
formatted_prompt,
|
| 114 |
-
max_new_tokens=150,
|
| 115 |
-
temperature=0.7,
|
| 116 |
-
top_k=50,
|
| 117 |
-
do_sample=True,
|
| 118 |
-
truncation=True
|
| 119 |
-
)[0]["generated_text"]
|
| 120 |
-
# Update the story
|
| 121 |
-
story_text = result.replace(formatted_prompt, "").strip()
|
| 122 |
-
current_story = story_text
|
| 123 |
-
|
| 124 |
-
# Generate new options based on the updated story
|
| 125 |
-
last_options = generate_options(current_story)
|
| 126 |
-
options_text = "\n\n**Choose an option:**\n" + "\n".join([f"{i+1}. {opt}" for i, opt in enumerate(last_options)])
|
| 127 |
-
return current_story + options_text
|
| 128 |
-
|
| 129 |
-
def reset_story():
|
| 130 |
-
global current_story, last_options
|
| 131 |
-
current_story = ""
|
| 132 |
-
last_options = []
|
| 133 |
-
return "Story reset. Enter a new prompt to begin!"
|
| 134 |
-
|
| 135 |
-
def generate_options(text):
|
| 136 |
-
# Dynamic option generation based on keywords in the prompt or story
|
| 137 |
-
text_lower = text.lower()
|
| 138 |
-
if "girl" in text_lower or "woman" in text_lower or "she" in text_lower:
|
| 139 |
-
return [
|
| 140 |
-
"She decided to investigate the mystery further.",
|
| 141 |
-
"She encountered someone unexpected who changed her path.",
|
| 142 |
-
"She chose to keep her discovery a secret for now."
|
| 143 |
-
]
|
| 144 |
-
elif "man" in text_lower or "he" in text_lower or "boy" in text_lower:
|
| 145 |
-
return [
|
| 146 |
-
"He set out to confront the source of the mystery.",
|
| 147 |
-
"He stumbled upon a hidden truth that tested his resolve.",
|
| 148 |
-
"He decided to seek help from an old ally."
|
| 149 |
-
]
|
| 150 |
-
elif "book" in text_lower or "library" in text_lower or "knowledge" in text_lower:
|
| 151 |
-
return [
|
| 152 |
-
"The knowledge within led to a surprising revelation.",
|
| 153 |
-
"A stranger appeared, claiming ownership of the discovery.",
|
| 154 |
-
"The pursuit of answers took a dangerous turn."
|
| 155 |
-
]
|
| 156 |
-
elif "cat" in text_lower or "dog" in text_lower or "animal" in text_lower:
|
| 157 |
-
return [
|
| 158 |
-
"It followed a strange sound into the night.",
|
| 159 |
-
"It met a companion that altered its journey.",
|
| 160 |
-
"It uncovered a hidden clue beneath the surface."
|
| 161 |
-
]
|
| 162 |
-
else:
|
| 163 |
-
return [
|
| 164 |
-
"The situation escalated into an unexpected conflict.",
|
| 165 |
-
"A new clue emerged, pointing to a distant location.",
|
| 166 |
-
"The choice was made to abandon the path and start anew."
|
| 167 |
-
]
|
| 168 |
-
|
| 169 |
-
# Create the Gradio interface
|
| 170 |
-
with gr.Blocks(title="AI Story Generator") as demo:
|
| 171 |
-
gr.Markdown("# AI Story Generator")
|
| 172 |
-
gr.Markdown("Enter any prompt to start your story, then choose an option to continue or reset!")
|
| 173 |
-
|
| 174 |
-
# Input for the initial prompt
|
| 175 |
-
prompt_input = gr.Textbox(label="Start your story with a prompt", placeholder="E.g., 'A cat sat on a rooftop...'")
|
| 176 |
-
output = gr.Textbox(label="Your Story", lines=15)
|
| 177 |
-
|
| 178 |
-
# Buttons and option input
|
| 179 |
-
start_button = gr.Button("Start Story")
|
| 180 |
-
option_input = gr.Textbox(label="Enter option number (1-3)", placeholder="E.g., 1", lines=1)
|
| 181 |
-
continue_button = gr.Button("Continue Story")
|
| 182 |
-
reset_button = gr.Button("Reset Story")
|
| 183 |
-
|
| 184 |
-
# Connect buttons to functions
|
| 185 |
-
start_button.click(fn=start_story, inputs=prompt_input, outputs=output)
|
| 186 |
-
continue_button.click(fn=continue_story, inputs=option_input, outputs=output)
|
| 187 |
-
reset_button.click(fn=reset_story, inputs=None, outputs=output)
|
| 188 |
-
|
| 189 |
# Launch the app
|
| 190 |
demo.launch()
|
|
|
|
| 1 |
|
| 2 |
+
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
# Load the gpt-neo-125M model for text generation
|
|
|
|
| 13 |
# Generate the initial story segment, ensuring the prompt is the starting point
|
| 14 |
result = generator(
|
| 15 |
prompt,
|
| 16 |
+
max_new_tokens=150, # Limit new tokens to keep it concise
|
| 17 |
temperature=0.7, # Lower temperature for less randomness
|
| 18 |
top_k=50, # Filter to top 50 likely next words
|
| 19 |
do_sample=True, # Enable sampling for creativity
|
|
|
|
| 29 |
# Continue the story, using the current story as the prompt
|
| 30 |
result = generator(
|
| 31 |
current_story,
|
| 32 |
+
max_new_tokens=150,
|
| 33 |
temperature=0.7,
|
| 34 |
top_k=50,
|
| 35 |
do_sample=True,
|
|
|
|
| 63 |
continue_button.click(fn=continue_story, inputs=None, outputs=output)
|
| 64 |
reset_button.click(fn=reset_story, inputs=None, outputs=output)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Launch the app
|
| 67 |
demo.launch()
|