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.
|
@@ -34,19 +34,19 @@ try:
|
|
34 |
print("Initializing model... This may take a moment.")
|
35 |
|
36 |
# Explicitly load the token from environment variables (for HF Spaces secrets)
|
37 |
-
# This makes the authentication more robust.
|
38 |
hf_token = os.environ.get("HF_TOKEN", None)
|
39 |
|
40 |
-
# Using a smaller
|
41 |
-
#
|
42 |
generator = pipeline(
|
43 |
"text-generation",
|
44 |
-
model="
|
45 |
-
torch_dtype=torch.
|
46 |
device_map="auto", # Will use GPU if available, otherwise CPU
|
47 |
token=hf_token
|
48 |
)
|
49 |
-
print("✅
|
50 |
|
51 |
except Exception as e:
|
52 |
model_error = e
|
@@ -68,32 +68,19 @@ def generate_stories(prompt: str) -> list[str]:
|
|
68 |
# Return a list of 10 empty strings to clear the outputs
|
69 |
return [""] * 10
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
Create a short story outline based on this idea.
|
77 |
-
|
78 |
-
### The Hook
|
79 |
-
A dramatic opening.
|
80 |
-
|
81 |
-
### The Ballad
|
82 |
-
The main story, told concisely.
|
83 |
-
|
84 |
-
### The Finale
|
85 |
-
A clear and satisfying ending.
|
86 |
-
---
|
87 |
-
"""
|
88 |
|
89 |
# Parameters for the pipeline to generate 10 diverse results.
|
90 |
params = {
|
91 |
-
"max_new_tokens":
|
92 |
"num_return_sequences": 10,
|
93 |
"do_sample": True,
|
94 |
-
"temperature": 0.
|
95 |
-
"top_k": 50,
|
96 |
"top_p": 0.95,
|
|
|
97 |
}
|
98 |
|
99 |
# Generate 10 different story variations
|
@@ -102,9 +89,10 @@ A clear and satisfying ending.
|
|
102 |
# Extract the generated text and clean it up.
|
103 |
stories = []
|
104 |
for out in outputs:
|
105 |
-
#
|
106 |
full_text = out['generated_text']
|
107 |
-
|
|
|
108 |
|
109 |
# Ensure we return exactly 10 stories, padding if necessary.
|
110 |
while len(stories) < 10:
|
|
|
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 **Tencent's Hunyuan-1.8B** to generate creative outlines formatted in Markdown.
|
11 |
|
12 |
**How it works:**
|
13 |
1. Enter your story idea.
|
|
|
34 |
print("Initializing model... This may take a moment.")
|
35 |
|
36 |
# Explicitly load the token from environment variables (for HF Spaces secrets)
|
37 |
+
# This makes the authentication more robust, overriding any bad default credentials.
|
38 |
hf_token = os.environ.get("HF_TOKEN", None)
|
39 |
|
40 |
+
# Using a smaller model from the user's list.
|
41 |
+
# Passing the token explicitly to ensure correct authentication.
|
42 |
generator = pipeline(
|
43 |
"text-generation",
|
44 |
+
model="tencent/Hunyuan-1.8B-Instruct",
|
45 |
+
torch_dtype=torch.bfloat16, # Use bfloat16 for better performance if available
|
46 |
device_map="auto", # Will use GPU if available, otherwise CPU
|
47 |
token=hf_token
|
48 |
)
|
49 |
+
print("✅ Tencent/Hunyuan-1.8B-Instruct model loaded successfully!")
|
50 |
|
51 |
except Exception as e:
|
52 |
model_error = e
|
|
|
68 |
# Return a list of 10 empty strings to clear the outputs
|
69 |
return [""] * 10
|
70 |
|
71 |
+
# This prompt format is specific to the Hunyuan model.
|
72 |
+
system_instruction = "You are an expert storyteller. Your task is to take a user's prompt and write a short story as a Markdown outline. The story must have a dramatic arc and be the length of a song. Use emojis to highlight the story sections."
|
73 |
+
|
74 |
+
story_prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
# Parameters for the pipeline to generate 10 diverse results.
|
77 |
params = {
|
78 |
+
"max_new_tokens": 250,
|
79 |
"num_return_sequences": 10,
|
80 |
"do_sample": True,
|
81 |
+
"temperature": 0.8,
|
|
|
82 |
"top_p": 0.95,
|
83 |
+
"pad_token_id": generator.tokenizer.eos_token_id # Suppress warning
|
84 |
}
|
85 |
|
86 |
# Generate 10 different story variations
|
|
|
89 |
# Extract the generated text and clean it up.
|
90 |
stories = []
|
91 |
for out in outputs:
|
92 |
+
# Remove the prompt part from the full generated text
|
93 |
full_text = out['generated_text']
|
94 |
+
assistant_response = full_text.split("<|im_start|>assistant\n")[-1]
|
95 |
+
stories.append(assistant_response)
|
96 |
|
97 |
# Ensure we return exactly 10 stories, padding if necessary.
|
98 |
while len(stories) < 10:
|