SanaAdeel commited on
Commit
3290a27
Β·
verified Β·
1 Parent(s): 7f87f37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
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" # Replace with gr.Request for real user sessions
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
- return f"{reply}\n\n---\n_{tier_badge}_"
 
 
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__":