Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import os
|
|
7 |
TITLE = "✍️ AI Story Outliner"
|
8 |
DESCRIPTION = """
|
9 |
Enter a prompt and get 10 unique story outlines from a CPU-friendly AI model.
|
10 |
-
The app uses **
|
11 |
|
12 |
**How it works:**
|
13 |
1. Enter your story idea.
|
@@ -25,21 +25,33 @@ examples = [
|
|
25 |
]
|
26 |
|
27 |
# --- Model Initialization ---
|
28 |
-
# This section loads
|
|
|
29 |
generator = None
|
30 |
model_error = None
|
31 |
|
32 |
try:
|
33 |
print("Initializing model... This may take a moment.")
|
34 |
|
35 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
generator = pipeline(
|
37 |
"text-generation",
|
38 |
-
model="
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
)
|
42 |
-
print("✅
|
43 |
|
44 |
except Exception as e:
|
45 |
model_error = e
|
@@ -61,21 +73,11 @@ def generate_stories(prompt: str) -> list[str]:
|
|
61 |
# Return a list of 10 empty strings to clear the outputs
|
62 |
return [""] * 10
|
63 |
|
64 |
-
# A
|
65 |
-
story_prompt = f"""
|
66 |
-
|
67 |
-
|
68 |
-
Create a short story outline based on this idea.
|
69 |
-
|
70 |
### 🎬 The Hook
|
71 |
-
A dramatic opening.
|
72 |
-
|
73 |
-
### 🎼 The Ballad
|
74 |
-
The main story, told concisely.
|
75 |
-
|
76 |
-
### 🔚 The Finale
|
77 |
-
A clear and satisfying ending.
|
78 |
-
---
|
79 |
"""
|
80 |
|
81 |
# Parameters for the pipeline to generate 10 diverse results.
|
@@ -92,11 +94,15 @@ A clear and satisfying ending.
|
|
92 |
# Generate 10 different story variations
|
93 |
outputs = generator(story_prompt, **params)
|
94 |
|
95 |
-
# Extract the generated text.
|
96 |
stories = []
|
97 |
for out in outputs:
|
|
|
98 |
full_text = out['generated_text']
|
99 |
-
|
|
|
|
|
|
|
100 |
|
101 |
# Ensure we return exactly 10 stories, padding if necessary.
|
102 |
while len(stories) < 10:
|
@@ -113,7 +119,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 95% !i
|
|
113 |
with gr.Column(scale=1):
|
114 |
input_area = gr.TextArea(
|
115 |
lines=5,
|
116 |
-
label="Your Story Prompt
|
117 |
placeholder="e.g., 'The last dragon on Earth lived not in a cave, but in a library...'"
|
118 |
)
|
119 |
generate_button = gr.Button("Generate 10 Outlines ✨", variant="primary")
|
@@ -147,4 +153,4 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 95% !i
|
|
147 |
)
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
-
demo.launch()
|
|
|
7 |
TITLE = "✍️ AI Story Outliner"
|
8 |
DESCRIPTION = """
|
9 |
Enter a prompt and get 10 unique story outlines from a CPU-friendly AI model.
|
10 |
+
The app uses **Microsoft's Phi-2**, a powerful small language model, to generate creative outlines.
|
11 |
|
12 |
**How it works:**
|
13 |
1. Enter your story idea.
|
|
|
25 |
]
|
26 |
|
27 |
# --- Model Initialization ---
|
28 |
+
# This section loads the Phi-2 model, which requires authentication.
|
29 |
+
# It will automatically use the HF_TOKEN secret when deployed on Hugging Face Spaces.
|
30 |
generator = None
|
31 |
model_error = None
|
32 |
|
33 |
try:
|
34 |
print("Initializing model... This may take a moment.")
|
35 |
|
36 |
+
# Explicitly load the token from environment variables (for HF Spaces secrets).
|
37 |
+
hf_token = os.environ.get("HF_TOKEN")
|
38 |
+
|
39 |
+
if hf_token:
|
40 |
+
print("✅ HF_TOKEN secret found.")
|
41 |
+
else:
|
42 |
+
# If no token is found, raise an error to prevent the app from crashing later.
|
43 |
+
raise ValueError("Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings.")
|
44 |
+
|
45 |
+
# Using 'microsoft/phi-2'. This model requires authentication and trust_remote_code=True.
|
46 |
generator = pipeline(
|
47 |
"text-generation",
|
48 |
+
model="microsoft/phi-2",
|
49 |
+
token=hf_token,
|
50 |
+
torch_dtype=torch.bfloat16, # More performant data type
|
51 |
+
device_map="auto", # Will use GPU if available, otherwise CPU
|
52 |
+
trust_remote_code=True # Required for Phi-2 model
|
53 |
)
|
54 |
+
print("✅ microsoft/phi-2 model loaded successfully!")
|
55 |
|
56 |
except Exception as e:
|
57 |
model_error = e
|
|
|
73 |
# Return a list of 10 empty strings to clear the outputs
|
74 |
return [""] * 10
|
75 |
|
76 |
+
# A clear, instructive prompt format that works well with models like Phi-2.
|
77 |
+
story_prompt = f"""Instruct: Create a short story outline based on this idea: "{prompt}"
|
78 |
+
The outline should have three parts: a dramatic hook, a concise ballad, and a satisfying finale. Use emojis.
|
79 |
+
Output:
|
|
|
|
|
80 |
### 🎬 The Hook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
"""
|
82 |
|
83 |
# Parameters for the pipeline to generate 10 diverse results.
|
|
|
94 |
# Generate 10 different story variations
|
95 |
outputs = generator(story_prompt, **params)
|
96 |
|
97 |
+
# Extract the generated text.
|
98 |
stories = []
|
99 |
for out in outputs:
|
100 |
+
# The model will generate the prompt plus the continuation. We extract just the new part.
|
101 |
full_text = out['generated_text']
|
102 |
+
# Add back the part of the prompt we want to see in the output
|
103 |
+
story_start = "### 🎬 The Hook\n"
|
104 |
+
generated_part = full_text.split(story_start)[-1]
|
105 |
+
stories.append(story_start + generated_part)
|
106 |
|
107 |
# Ensure we return exactly 10 stories, padding if necessary.
|
108 |
while len(stories) < 10:
|
|
|
119 |
with gr.Column(scale=1):
|
120 |
input_area = gr.TextArea(
|
121 |
lines=5,
|
122 |
+
label="Your Story Prompt �",
|
123 |
placeholder="e.g., 'The last dragon on Earth lived not in a cave, but in a library...'"
|
124 |
)
|
125 |
generate_button = gr.Button("Generate 10 Outlines ✨", variant="primary")
|
|
|
153 |
)
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
+
demo.launch()
|