Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -110,19 +110,19 @@ def respond(message, history, style, max_tokens, temperature, top_p):
|
|
| 110 |
if not is_premium:
|
| 111 |
max_tokens = min(max_tokens, FREE_MAX_TOKENS)
|
| 112 |
if style != "neutral":
|
| 113 |
-
return history + [{"role": "assistant", "content": f"π
|
| 114 |
|
| 115 |
system_message = STYLE_PRESETS.get(style, STYLE_PRESETS["neutral"])
|
| 116 |
|
| 117 |
# Generate response based on tier
|
| 118 |
if is_premium:
|
| 119 |
reply = premium_generation(user_id, system_message, history, message, max_tokens, temperature, top_p)
|
| 120 |
-
tier_badge = "β¨ PREMIUM"
|
| 121 |
else:
|
| 122 |
reply = free_tier_generation(system_message, history, message, max_tokens, temperature)
|
| 123 |
-
tier_badge = f"π FREE
|
| 124 |
|
| 125 |
-
full_reply = f"{reply}\n\n---\
|
| 126 |
return history + [{"role": "user", "content": message}, {"role": "assistant", "content": full_reply}]
|
| 127 |
|
| 128 |
def export_conversation(history):
|
|
@@ -130,7 +130,7 @@ def export_conversation(history):
|
|
| 130 |
is_premium, paywall_url = check_paywall(user_id)
|
| 131 |
|
| 132 |
if not is_premium:
|
| 133 |
-
return None, f"π Export requires
|
| 134 |
|
| 135 |
content = f"Creative Writing Session - {datetime.now().strftime('%Y-%m-%d %H:%M')}\n\n"
|
| 136 |
for msg in history:
|
|
@@ -143,36 +143,221 @@ def export_conversation(history):
|
|
| 143 |
|
| 144 |
return filepath, "β
Exported successfully!"
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# Build UI
|
| 147 |
-
with gr.Blocks(title="Creative Writing Assistant") as demo:
|
| 148 |
-
gr.Markdown("# π Creative Writing Assistant\n**Free:** 150 tokens | **Premium:** 2048 tokens + styles + export")
|
| 149 |
|
|
|
|
| 150 |
with gr.Row():
|
| 151 |
-
with gr.Column(
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
style = gr.Dropdown(
|
| 158 |
choices=list(STYLE_PRESETS.keys()),
|
| 159 |
value="neutral",
|
| 160 |
-
label="
|
| 161 |
-
info="
|
| 162 |
)
|
| 163 |
-
max_tokens = gr.Slider(FREE_MAX_TOKENS, PREMIUM_MAX_TOKENS, value=FREE_MAX_TOKENS, step=50, label="Max Tokens")
|
| 164 |
-
temperature = gr.Slider(0.1, 2.0, value=0.8, step=0.1, label="Creativity")
|
| 165 |
-
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Diversity")
|
| 166 |
|
| 167 |
gr.Markdown("---")
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# Event handlers
|
| 173 |
submit.click(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot]).then(lambda: "", None, [msg])
|
| 174 |
msg.submit(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot]).then(lambda: "", None, [msg])
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
demo.launch()
|
|
|
|
| 110 |
if not is_premium:
|
| 111 |
max_tokens = min(max_tokens, FREE_MAX_TOKENS)
|
| 112 |
if style != "neutral":
|
| 113 |
+
return history + [{"role": "assistant", "content": f"π **Premium Feature Locked**\n\nStyle customization is available for Premium users only.\n\n[β¨ Upgrade to Premium]({paywall_url})"}]
|
| 114 |
|
| 115 |
system_message = STYLE_PRESETS.get(style, STYLE_PRESETS["neutral"])
|
| 116 |
|
| 117 |
# Generate response based on tier
|
| 118 |
if is_premium:
|
| 119 |
reply = premium_generation(user_id, system_message, history, message, max_tokens, temperature, top_p)
|
| 120 |
+
tier_badge = "β¨ **PREMIUM**"
|
| 121 |
else:
|
| 122 |
reply = free_tier_generation(system_message, history, message, max_tokens, temperature)
|
| 123 |
+
tier_badge = f"π **FREE TIER** β’ 150 tokens limit\n\nπ [Unlock Premium Features]({paywall_url}) β’ Longer stories β’ Custom styles β’ Export"
|
| 124 |
|
| 125 |
+
full_reply = f"{reply}\n\n---\n{tier_badge}"
|
| 126 |
return history + [{"role": "user", "content": message}, {"role": "assistant", "content": full_reply}]
|
| 127 |
|
| 128 |
def export_conversation(history):
|
|
|
|
| 130 |
is_premium, paywall_url = check_paywall(user_id)
|
| 131 |
|
| 132 |
if not is_premium:
|
| 133 |
+
return None, f"π Export feature requires Premium.\n\n[Upgrade Now]({paywall_url})"
|
| 134 |
|
| 135 |
content = f"Creative Writing Session - {datetime.now().strftime('%Y-%m-%d %H:%M')}\n\n"
|
| 136 |
for msg in history:
|
|
|
|
| 143 |
|
| 144 |
return filepath, "β
Exported successfully!"
|
| 145 |
|
| 146 |
+
# Custom CSS for elegant design
|
| 147 |
+
custom_css = """
|
| 148 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 149 |
+
|
| 150 |
+
* {
|
| 151 |
+
font-family: 'Inter', sans-serif !important;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
.gradio-container {
|
| 155 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.main-header {
|
| 159 |
+
background: rgba(255, 255, 255, 0.95);
|
| 160 |
+
backdrop-filter: blur(10px);
|
| 161 |
+
border-radius: 20px;
|
| 162 |
+
padding: 30px;
|
| 163 |
+
margin-bottom: 25px;
|
| 164 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
| 165 |
+
text-align: center;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
.main-header h1 {
|
| 169 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 170 |
+
-webkit-background-clip: text;
|
| 171 |
+
-webkit-text-fill-color: transparent;
|
| 172 |
+
font-size: 2.5em;
|
| 173 |
+
font-weight: 700;
|
| 174 |
+
margin-bottom: 10px;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.main-header p {
|
| 178 |
+
color: #64748b;
|
| 179 |
+
font-size: 1.1em;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.chat-container {
|
| 183 |
+
background: rgba(255, 255, 255, 0.98);
|
| 184 |
+
border-radius: 20px;
|
| 185 |
+
padding: 25px;
|
| 186 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
.controls-container {
|
| 190 |
+
background: rgba(255, 255, 255, 0.98);
|
| 191 |
+
border-radius: 20px;
|
| 192 |
+
padding: 25px;
|
| 193 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
.premium-badge {
|
| 197 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 198 |
+
color: white;
|
| 199 |
+
padding: 8px 16px;
|
| 200 |
+
border-radius: 20px;
|
| 201 |
+
font-weight: 600;
|
| 202 |
+
font-size: 0.85em;
|
| 203 |
+
display: inline-block;
|
| 204 |
+
margin-left: 10px;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
.feature-card {
|
| 208 |
+
background: rgba(102, 126, 234, 0.1);
|
| 209 |
+
border-radius: 12px;
|
| 210 |
+
padding: 15px;
|
| 211 |
+
margin-bottom: 15px;
|
| 212 |
+
border-left: 4px solid #667eea;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
button {
|
| 216 |
+
border-radius: 12px !important;
|
| 217 |
+
font-weight: 600 !important;
|
| 218 |
+
transition: all 0.3s ease !important;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
button:hover {
|
| 222 |
+
transform: translateY(-2px) !important;
|
| 223 |
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15) !important;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
.primary-btn {
|
| 227 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
input, textarea, select {
|
| 231 |
+
border-radius: 10px !important;
|
| 232 |
+
border: 2px solid #e2e8f0 !important;
|
| 233 |
+
transition: all 0.3s ease !important;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
input:focus, textarea:focus, select:focus {
|
| 237 |
+
border-color: #667eea !important;
|
| 238 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
.slider {
|
| 242 |
+
accent-color: #667eea !important;
|
| 243 |
+
}
|
| 244 |
+
"""
|
| 245 |
+
|
| 246 |
# Build UI
|
| 247 |
+
with gr.Blocks(css=custom_css, title="Creative Writing Assistant", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 248 |
|
| 249 |
+
# Header
|
| 250 |
with gr.Row():
|
| 251 |
+
with gr.Column():
|
| 252 |
+
gr.HTML("""
|
| 253 |
+
<div class="main-header">
|
| 254 |
+
<h1>β¨ Creative Writing Assistant</h1>
|
| 255 |
+
<p>Transform your ideas into captivating stories with AI-powered creativity</p>
|
| 256 |
+
<div style="margin-top: 20px;">
|
| 257 |
+
<span style="background: rgba(102, 126, 234, 0.1); padding: 10px 20px; border-radius: 25px; color: #667eea; font-weight: 600; margin: 0 10px;">
|
| 258 |
+
π Free: 150 tokens
|
| 259 |
+
</span>
|
| 260 |
+
<span style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); padding: 10px 20px; border-radius: 25px; color: white; font-weight: 600; margin: 0 10px;">
|
| 261 |
+
β¨ Premium: 2048 tokens + Styles + Export
|
| 262 |
+
</span>
|
| 263 |
+
</div>
|
| 264 |
+
</div>
|
| 265 |
+
""")
|
| 266 |
+
|
| 267 |
+
# Main Content
|
| 268 |
+
with gr.Row(equal_height=True):
|
| 269 |
+
# Chat Section
|
| 270 |
+
with gr.Column(scale=2, elem_classes="chat-container"):
|
| 271 |
+
gr.Markdown("### π¬ Your Creative Space")
|
| 272 |
+
chatbot = gr.Chatbot(
|
| 273 |
+
type="messages",
|
| 274 |
+
height=550,
|
| 275 |
+
show_copy_button=True,
|
| 276 |
+
avatar_images=(None, "https://em-content.zobj.net/source/twitter/376/sparkles_2728.png")
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
with gr.Row():
|
| 280 |
+
msg = gr.Textbox(
|
| 281 |
+
label="",
|
| 282 |
+
placeholder="π¨ Write your creative prompt here... (e.g., 'Write a story about a magical garden')",
|
| 283 |
+
lines=3,
|
| 284 |
+
scale=9
|
| 285 |
+
)
|
| 286 |
|
| 287 |
+
with gr.Row():
|
| 288 |
+
submit = gr.Button("β¨ Generate Story", variant="primary", scale=2, size="lg")
|
| 289 |
+
clear = gr.Button("ποΈ Clear", scale=1, size="lg")
|
| 290 |
+
|
| 291 |
+
# Controls Section
|
| 292 |
+
with gr.Column(scale=1, elem_classes="controls-container"):
|
| 293 |
+
gr.Markdown("### ποΈ Creative Controls")
|
| 294 |
+
|
| 295 |
+
gr.HTML('<div class="feature-card"><b>π¨ Writing Style</b><span class="premium-badge">PREMIUM</span></div>')
|
| 296 |
style = gr.Dropdown(
|
| 297 |
choices=list(STYLE_PRESETS.keys()),
|
| 298 |
value="neutral",
|
| 299 |
+
label="",
|
| 300 |
+
info="Choose your narrative voice"
|
| 301 |
)
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
gr.Markdown("---")
|
| 304 |
+
|
| 305 |
+
gr.Markdown("**π Generation Length**")
|
| 306 |
+
max_tokens = gr.Slider(
|
| 307 |
+
FREE_MAX_TOKENS,
|
| 308 |
+
PREMIUM_MAX_TOKENS,
|
| 309 |
+
value=FREE_MAX_TOKENS,
|
| 310 |
+
step=50,
|
| 311 |
+
label="Max Tokens",
|
| 312 |
+
info="Free tier limited to 150 tokens",
|
| 313 |
+
elem_classes="slider"
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
gr.Markdown("**π‘οΈ Creativity Level**")
|
| 317 |
+
temperature = gr.Slider(
|
| 318 |
+
0.1,
|
| 319 |
+
2.0,
|
| 320 |
+
value=0.8,
|
| 321 |
+
step=0.1,
|
| 322 |
+
label="Temperature",
|
| 323 |
+
info="Higher = more creative",
|
| 324 |
+
elem_classes="slider"
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
gr.Markdown("**π² Diversity**")
|
| 328 |
+
top_p = gr.Slider(
|
| 329 |
+
0.1,
|
| 330 |
+
1.0,
|
| 331 |
+
value=0.95,
|
| 332 |
+
step=0.05,
|
| 333 |
+
label="Top-p",
|
| 334 |
+
info="Response variety",
|
| 335 |
+
elem_classes="slider"
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
gr.Markdown("---")
|
| 339 |
+
|
| 340 |
+
gr.HTML('<div class="feature-card"><b>πΎ Export</b><span class="premium-badge">PREMIUM</span></div>')
|
| 341 |
+
export_btn = gr.Button("π₯ Download Story", variant="secondary", size="lg")
|
| 342 |
+
download_file = gr.File(label="Your File", visible=False)
|
| 343 |
+
export_status = gr.Textbox(label="Status", interactive=False, visible=False)
|
| 344 |
+
|
| 345 |
+
# Footer
|
| 346 |
+
gr.HTML("""
|
| 347 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; background: rgba(255, 255, 255, 0.9); border-radius: 15px;">
|
| 348 |
+
<p style="color: #64748b; font-size: 0.95em;">
|
| 349 |
+
Powered by AI β’ Built with β€οΈ using Gradio & Paywalls.ai
|
| 350 |
+
</p>
|
| 351 |
+
</div>
|
| 352 |
+
""")
|
| 353 |
|
| 354 |
# Event handlers
|
| 355 |
submit.click(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot]).then(lambda: "", None, [msg])
|
| 356 |
msg.submit(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot]).then(lambda: "", None, [msg])
|
| 357 |
+
clear.click(lambda: None, None, [chatbot])
|
| 358 |
+
export_btn.click(export_conversation, [chatbot], [download_file, export_status]).then(
|
| 359 |
+
lambda: (gr.update(visible=True), gr.update(visible=True)), None, [download_file, export_status]
|
| 360 |
+
)
|
| 361 |
|
| 362 |
if __name__ == "__main__":
|
| 363 |
demo.launch()
|