Update app.py
Browse files
app.py
CHANGED
|
@@ -67,21 +67,21 @@ def chat_completion(user_id, system_message, history, user_message, max_tokens,
|
|
| 67 |
return f"Error: {e}"
|
| 68 |
|
| 69 |
def respond(message, history, style, max_tokens, temperature, top_p):
|
| 70 |
-
user_id = "demo_user"
|
| 71 |
is_premium, paywall_url = check_paywall(user_id)
|
| 72 |
|
| 73 |
-
# Apply free tier limits
|
| 74 |
if not is_premium:
|
| 75 |
max_tokens = min(max_tokens, FREE_MAX_TOKENS)
|
| 76 |
if style != "neutral":
|
| 77 |
-
return f"π Style customization requires premium. [Unlock here]({paywall_url})"
|
| 78 |
|
| 79 |
system_message = STYLE_PRESETS.get(style, STYLE_PRESETS["neutral"])
|
| 80 |
reply = chat_completion(user_id, system_message, history, message, max_tokens, temperature, top_p)
|
| 81 |
|
| 82 |
-
# Add tier indicator
|
| 83 |
tier_badge = "β¨ PREMIUM" if is_premium else "π FREE"
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def export_conversation(history):
|
| 87 |
user_id = "demo_user"
|
|
@@ -90,7 +90,6 @@ def export_conversation(history):
|
|
| 90 |
if not is_premium:
|
| 91 |
return None, f"π Export requires premium. [Unlock here]({paywall_url})"
|
| 92 |
|
| 93 |
-
# Generate text file
|
| 94 |
content = f"Creative Writing Session - {datetime.now().strftime('%Y-%m-%d %H:%M')}\n\n"
|
| 95 |
for msg in history:
|
| 96 |
role = msg["role"].upper()
|
|
@@ -129,8 +128,8 @@ with gr.Blocks(title="Creative Writing Assistant") as demo:
|
|
| 129 |
export_status = gr.Textbox(label="Status", interactive=False)
|
| 130 |
|
| 131 |
# Event handlers
|
| 132 |
-
submit.click(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot])
|
| 133 |
-
msg.submit(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot])
|
| 134 |
export_btn.click(export_conversation, [chatbot], [download_file, export_status])
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
|
|
|
| 67 |
return f"Error: {e}"
|
| 68 |
|
| 69 |
def respond(message, history, style, max_tokens, temperature, top_p):
|
| 70 |
+
user_id = "demo_user"
|
| 71 |
is_premium, paywall_url = check_paywall(user_id)
|
| 72 |
|
|
|
|
| 73 |
if not is_premium:
|
| 74 |
max_tokens = min(max_tokens, FREE_MAX_TOKENS)
|
| 75 |
if style != "neutral":
|
| 76 |
+
return history + [{"role": "assistant", "content": f"π Style customization requires premium. [Unlock here]({paywall_url})"}]
|
| 77 |
|
| 78 |
system_message = STYLE_PRESETS.get(style, STYLE_PRESETS["neutral"])
|
| 79 |
reply = chat_completion(user_id, system_message, history, message, max_tokens, temperature, top_p)
|
| 80 |
|
|
|
|
| 81 |
tier_badge = "β¨ PREMIUM" if is_premium else "π FREE"
|
| 82 |
+
full_reply = f"{reply}\n\n---\n_{tier_badge}_"
|
| 83 |
+
|
| 84 |
+
return history + [{"role": "user", "content": message}, {"role": "assistant", "content": full_reply}]
|
| 85 |
|
| 86 |
def export_conversation(history):
|
| 87 |
user_id = "demo_user"
|
|
|
|
| 90 |
if not is_premium:
|
| 91 |
return None, f"π Export requires premium. [Unlock here]({paywall_url})"
|
| 92 |
|
|
|
|
| 93 |
content = f"Creative Writing Session - {datetime.now().strftime('%Y-%m-%d %H:%M')}\n\n"
|
| 94 |
for msg in history:
|
| 95 |
role = msg["role"].upper()
|
|
|
|
| 128 |
export_status = gr.Textbox(label="Status", interactive=False)
|
| 129 |
|
| 130 |
# Event handlers
|
| 131 |
+
submit.click(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot]).then(lambda: "", None, [msg])
|
| 132 |
+
msg.submit(respond, [msg, chatbot, style, max_tokens, temperature, top_p], [chatbot]).then(lambda: "", None, [msg])
|
| 133 |
export_btn.click(export_conversation, [chatbot], [download_file, export_status])
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|