Spaces:
Sleeping
Sleeping
Fix credits balance
Browse files
main.py
CHANGED
|
@@ -235,7 +235,6 @@ def upload_log():
|
|
| 235 |
# Content
|
| 236 |
# -----------------------
|
| 237 |
# ---------- Story Generation Endpoint ----------
|
| 238 |
-
|
| 239 |
@app.route('/api/story/generate', methods=['POST'])
|
| 240 |
def generate_story_endpoint():
|
| 241 |
try:
|
|
@@ -248,6 +247,14 @@ def generate_story_endpoint():
|
|
| 248 |
if not uid:
|
| 249 |
return jsonify({'error': 'Invalid or expired token'}), 401
|
| 250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
# --- Read Request Data ---
|
| 252 |
data = request.form.to_dict() # For multipart/form-data
|
| 253 |
input_type = data.get('input_type', 'text') # "text", "pdf", "wiki", "bible", "youtube", "dataframe"
|
|
@@ -416,10 +423,7 @@ def generate_story_endpoint():
|
|
| 416 |
story_ref.set(story_record)
|
| 417 |
|
| 418 |
# Subtract 5 Credits
|
| 419 |
-
|
| 420 |
-
user_data = user_ref.get() or {}
|
| 421 |
-
current_credits = user_data.get("credits", 0)
|
| 422 |
-
new_credits = max(0, current_credits - 5)
|
| 423 |
user_ref.update({"credits": new_credits})
|
| 424 |
|
| 425 |
preview = sections[0] if sections else {}
|
|
@@ -464,6 +468,14 @@ def generate_video_endpoint():
|
|
| 464 |
'log_url': upload_log()
|
| 465 |
}), 401
|
| 466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
data = request.get_json()
|
| 468 |
story_id = data.get('story_id')
|
| 469 |
if not story_id:
|
|
@@ -553,9 +565,7 @@ def generate_video_endpoint():
|
|
| 553 |
story_ref.update({"video_url": video_url})
|
| 554 |
|
| 555 |
# Deduct 5 credits
|
| 556 |
-
|
| 557 |
-
user_data = user_ref.get() or {}
|
| 558 |
-
current_credits = user_data.get("credits", 0)
|
| 559 |
new_credits = max(0, current_credits - 5)
|
| 560 |
user_ref.update({"credits": new_credits})
|
| 561 |
logging.info(f"✅ Deducted 5 credits. New credit balance: {new_credits}")
|
|
|
|
| 235 |
# Content
|
| 236 |
# -----------------------
|
| 237 |
# ---------- Story Generation Endpoint ----------
|
|
|
|
| 238 |
@app.route('/api/story/generate', methods=['POST'])
|
| 239 |
def generate_story_endpoint():
|
| 240 |
try:
|
|
|
|
| 247 |
if not uid:
|
| 248 |
return jsonify({'error': 'Invalid or expired token'}), 401
|
| 249 |
|
| 250 |
+
# --- Check User Credits Before Generation ---
|
| 251 |
+
user_ref = db.reference(f"users/{uid}")
|
| 252 |
+
user_data = user_ref.get() or {}
|
| 253 |
+
current_credits = user_data.get("credits", 0)
|
| 254 |
+
|
| 255 |
+
if current_credits < 5:
|
| 256 |
+
return jsonify({'error': 'Insufficient credits. You need at least 5 credits to generate a story.'}), 403
|
| 257 |
+
|
| 258 |
# --- Read Request Data ---
|
| 259 |
data = request.form.to_dict() # For multipart/form-data
|
| 260 |
input_type = data.get('input_type', 'text') # "text", "pdf", "wiki", "bible", "youtube", "dataframe"
|
|
|
|
| 423 |
story_ref.set(story_record)
|
| 424 |
|
| 425 |
# Subtract 5 Credits
|
| 426 |
+
new_credits = current_credits - 5
|
|
|
|
|
|
|
|
|
|
| 427 |
user_ref.update({"credits": new_credits})
|
| 428 |
|
| 429 |
preview = sections[0] if sections else {}
|
|
|
|
| 468 |
'log_url': upload_log()
|
| 469 |
}), 401
|
| 470 |
|
| 471 |
+
user_ref = db.reference(f"users/{uid}")
|
| 472 |
+
user_data = user_ref.get() or {}
|
| 473 |
+
current_credits = user_data.get("credits", 0)
|
| 474 |
+
|
| 475 |
+
if current_credits < 5:
|
| 476 |
+
return jsonify({'error': 'Insufficient credits. You need at least 5 credits to generate a video.'}), 403
|
| 477 |
+
|
| 478 |
+
|
| 479 |
data = request.get_json()
|
| 480 |
story_id = data.get('story_id')
|
| 481 |
if not story_id:
|
|
|
|
| 565 |
story_ref.update({"video_url": video_url})
|
| 566 |
|
| 567 |
# Deduct 5 credits
|
| 568 |
+
|
|
|
|
|
|
|
| 569 |
new_credits = max(0, current_credits - 5)
|
| 570 |
user_ref.update({"credits": new_credits})
|
| 571 |
logging.info(f"✅ Deducted 5 credits. New credit balance: {new_credits}")
|