Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -97,12 +97,10 @@ def verify_admin(auth_header):
|
|
| 97 |
return uid
|
| 98 |
|
| 99 |
# ---------- Dummy Admin Creation on Startup ----------
|
| 100 |
-
|
| 101 |
def create_dummy_admin():
|
| 102 |
|
| 103 |
-
|
| 104 |
-
admin_password = "rai1034R!"
|
| 105 |
-
|
| 106 |
try:
|
| 107 |
# Try to get the user if it exists
|
| 108 |
admin_user = auth.get_user_by_email(admin_email)
|
|
@@ -121,6 +119,7 @@ def create_dummy_admin():
|
|
| 121 |
'created_at': datetime.utcnow().isoformat()
|
| 122 |
})
|
| 123 |
print(f"Dummy admin ready: {admin_email}")
|
|
|
|
| 124 |
# ---------- Authentication Endpoints ----------
|
| 125 |
|
| 126 |
@app.route('/api/auth/signup', methods=['POST'])
|
|
@@ -255,13 +254,12 @@ def generate_story_endpoint():
|
|
| 255 |
user_ref = db.reference(f"users/{uid}")
|
| 256 |
user_data = user_ref.get() or {}
|
| 257 |
current_credits = user_data.get("credits", 0)
|
| 258 |
-
|
| 259 |
if current_credits < 5:
|
| 260 |
return jsonify({'error': 'Insufficient credits. You need at least 5 credits to generate a story.'}), 403
|
| 261 |
|
| 262 |
# --- Read Request Data ---
|
| 263 |
data = request.form.to_dict() # For multipart/form-data
|
| 264 |
-
input_type = data.get('input_type', 'text')
|
| 265 |
prompt = data.get('prompt') # For "text" only
|
| 266 |
story_type = data.get('story_type', 'free_form')
|
| 267 |
style = data.get('style', 'whimsical')
|
|
@@ -272,12 +270,19 @@ def generate_story_endpoint():
|
|
| 272 |
if input_type not in ["text", "pdf", "wiki", "bible", "youtube", "dataframe"]:
|
| 273 |
return jsonify({'error': 'Unsupported input_type'}), 400
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
from stories import (
|
| 276 |
generate_story_from_text,
|
| 277 |
get_pdf_text,
|
| 278 |
get_df,
|
| 279 |
generate_story_from_dataframe,
|
| 280 |
-
generateResponse
|
| 281 |
)
|
| 282 |
story_gen_start = time.time()
|
| 283 |
full_story = None
|
|
@@ -297,32 +302,26 @@ def generate_story_endpoint():
|
|
| 297 |
|
| 298 |
elif input_type == "dataframe":
|
| 299 |
uploaded_file = request.files.get("file")
|
| 300 |
-
ext = data.get("ext") # "csv", "xlsx", "xls"
|
| 301 |
if not uploaded_file or not ext:
|
| 302 |
return jsonify({'error': 'File and ext are required for dataframe input'}), 400
|
| 303 |
-
|
| 304 |
df = get_df(uploaded_file, ext)
|
| 305 |
if df is None:
|
| 306 |
return jsonify({'error': f'Failed to read {ext} file'}), 400
|
| 307 |
-
|
| 308 |
full_story = generate_story_from_dataframe(df, story_type)
|
| 309 |
|
| 310 |
elif input_type == "wiki":
|
| 311 |
-
wiki_url = data.get("wiki_url")
|
| 312 |
if not wiki_url:
|
| 313 |
return jsonify({'error': 'wiki_url is required for input_type "wiki"'}), 400
|
| 314 |
from stories import generate_story_from_wiki
|
| 315 |
full_story = generate_story_from_wiki(wiki_url, story_type)
|
| 316 |
|
| 317 |
elif input_type == "bible":
|
| 318 |
-
bible_reference = data.get("bible_reference")
|
| 319 |
if not bible_reference:
|
| 320 |
return jsonify({'error': 'bible_reference is required for input_type "bible"'}), 400
|
| 321 |
from stories import generate_story_from_bible
|
| 322 |
full_story = generate_story_from_bible(bible_reference, story_type)
|
| 323 |
|
| 324 |
elif input_type == "youtube":
|
| 325 |
-
youtube_url = data.get("youtube_url")
|
| 326 |
if not youtube_url:
|
| 327 |
return jsonify({'error': 'youtube_url is required for input_type "youtube"'}), 400
|
| 328 |
from stories import generate_story_from_youtube
|
|
@@ -349,28 +348,26 @@ def generate_story_endpoint():
|
|
| 349 |
from image_gen import generate_image_with_retry
|
| 350 |
from audio_gen import generate_audio
|
| 351 |
|
| 352 |
-
# If input_type is "dataframe",
|
| 353 |
df = None
|
| 354 |
if input_type == "dataframe":
|
| 355 |
uploaded_file = request.files.get("file")
|
| 356 |
-
|
| 357 |
-
|
| 358 |
|
| 359 |
# 3) Process each section
|
| 360 |
for section_text in sections_raw:
|
| 361 |
-
# Extract an image prompt between angle brackets
|
| 362 |
img_prompt_match = re.search(r"<(.*?)>", section_text)
|
| 363 |
img_prompt = img_prompt_match.group(1).strip() if img_prompt_match else section_text[:100]
|
| 364 |
|
| 365 |
image_start = time.time()
|
| 366 |
image_obj = None
|
| 367 |
|
| 368 |
-
#
|
| 369 |
if input_type == "dataframe" and df is not None:
|
| 370 |
try:
|
| 371 |
-
chart_str = generateResponse(img_prompt, df)
|
| 372 |
if chart_str and chart_str.startswith("data:image/png;base64,"):
|
| 373 |
-
# decode base64 -> PIL Image
|
| 374 |
base64_data = chart_str.split(",", 1)[1]
|
| 375 |
chart_bytes = base64.b64decode(base64_data)
|
| 376 |
image_obj = Image.open(io.BytesIO(chart_bytes))
|
|
@@ -411,6 +408,23 @@ def generate_story_endpoint():
|
|
| 411 |
# 4) Store the story
|
| 412 |
story_id = str(uuid.uuid4())
|
| 413 |
story_ref = db.reference(f"stories/{story_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
story_record = {
|
| 415 |
"uid": uid,
|
| 416 |
"full_story": full_story,
|
|
@@ -422,7 +436,8 @@ def generate_story_endpoint():
|
|
| 422 |
},
|
| 423 |
"created_at": datetime.utcnow().isoformat(),
|
| 424 |
"input_type": input_type,
|
| 425 |
-
"story_type": story_type
|
|
|
|
| 426 |
}
|
| 427 |
story_ref.set(story_record)
|
| 428 |
|
|
@@ -438,7 +453,8 @@ def generate_story_endpoint():
|
|
| 438 |
"preview": preview,
|
| 439 |
"sections": sections,
|
| 440 |
"generation_times": story_record["generation_times"],
|
| 441 |
-
"new_credits": new_credits
|
|
|
|
| 442 |
})
|
| 443 |
|
| 444 |
except Exception as e:
|
|
@@ -1162,5 +1178,5 @@ def admin_update_credits(uid):
|
|
| 1162 |
# ---------- Main ----------
|
| 1163 |
if __name__ == '__main__':
|
| 1164 |
# Create dummy admin account if it doesn't exist
|
| 1165 |
-
create_dummy_admin()
|
| 1166 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
|
| 97 |
return uid
|
| 98 |
|
| 99 |
# ---------- Dummy Admin Creation on Startup ----------
|
| 100 |
+
"""
|
| 101 |
def create_dummy_admin():
|
| 102 |
|
| 103 |
+
|
|
|
|
|
|
|
| 104 |
try:
|
| 105 |
# Try to get the user if it exists
|
| 106 |
admin_user = auth.get_user_by_email(admin_email)
|
|
|
|
| 119 |
'created_at': datetime.utcnow().isoformat()
|
| 120 |
})
|
| 121 |
print(f"Dummy admin ready: {admin_email}")
|
| 122 |
+
"""
|
| 123 |
# ---------- Authentication Endpoints ----------
|
| 124 |
|
| 125 |
@app.route('/api/auth/signup', methods=['POST'])
|
|
|
|
| 254 |
user_ref = db.reference(f"users/{uid}")
|
| 255 |
user_data = user_ref.get() or {}
|
| 256 |
current_credits = user_data.get("credits", 0)
|
|
|
|
| 257 |
if current_credits < 5:
|
| 258 |
return jsonify({'error': 'Insufficient credits. You need at least 5 credits to generate a story.'}), 403
|
| 259 |
|
| 260 |
# --- Read Request Data ---
|
| 261 |
data = request.form.to_dict() # For multipart/form-data
|
| 262 |
+
input_type = data.get('input_type', 'text')
|
| 263 |
prompt = data.get('prompt') # For "text" only
|
| 264 |
story_type = data.get('story_type', 'free_form')
|
| 265 |
style = data.get('style', 'whimsical')
|
|
|
|
| 270 |
if input_type not in ["text", "pdf", "wiki", "bible", "youtube", "dataframe"]:
|
| 271 |
return jsonify({'error': 'Unsupported input_type'}), 400
|
| 272 |
|
| 273 |
+
# Optionally retrieve these fields if relevant
|
| 274 |
+
wiki_url = data.get("wiki_url")
|
| 275 |
+
bible_reference = data.get("bible_reference")
|
| 276 |
+
youtube_url = data.get("youtube_url")
|
| 277 |
+
ext = data.get("ext") # For dataframe usage
|
| 278 |
+
|
| 279 |
+
# Prepare for story generation
|
| 280 |
from stories import (
|
| 281 |
generate_story_from_text,
|
| 282 |
get_pdf_text,
|
| 283 |
get_df,
|
| 284 |
generate_story_from_dataframe,
|
| 285 |
+
generateResponse
|
| 286 |
)
|
| 287 |
story_gen_start = time.time()
|
| 288 |
full_story = None
|
|
|
|
| 302 |
|
| 303 |
elif input_type == "dataframe":
|
| 304 |
uploaded_file = request.files.get("file")
|
|
|
|
| 305 |
if not uploaded_file or not ext:
|
| 306 |
return jsonify({'error': 'File and ext are required for dataframe input'}), 400
|
|
|
|
| 307 |
df = get_df(uploaded_file, ext)
|
| 308 |
if df is None:
|
| 309 |
return jsonify({'error': f'Failed to read {ext} file'}), 400
|
|
|
|
| 310 |
full_story = generate_story_from_dataframe(df, story_type)
|
| 311 |
|
| 312 |
elif input_type == "wiki":
|
|
|
|
| 313 |
if not wiki_url:
|
| 314 |
return jsonify({'error': 'wiki_url is required for input_type "wiki"'}), 400
|
| 315 |
from stories import generate_story_from_wiki
|
| 316 |
full_story = generate_story_from_wiki(wiki_url, story_type)
|
| 317 |
|
| 318 |
elif input_type == "bible":
|
|
|
|
| 319 |
if not bible_reference:
|
| 320 |
return jsonify({'error': 'bible_reference is required for input_type "bible"'}), 400
|
| 321 |
from stories import generate_story_from_bible
|
| 322 |
full_story = generate_story_from_bible(bible_reference, story_type)
|
| 323 |
|
| 324 |
elif input_type == "youtube":
|
|
|
|
| 325 |
if not youtube_url:
|
| 326 |
return jsonify({'error': 'youtube_url is required for input_type "youtube"'}), 400
|
| 327 |
from stories import generate_story_from_youtube
|
|
|
|
| 348 |
from image_gen import generate_image_with_retry
|
| 349 |
from audio_gen import generate_audio
|
| 350 |
|
| 351 |
+
# If input_type is "dataframe", re-use df for chart generation
|
| 352 |
df = None
|
| 353 |
if input_type == "dataframe":
|
| 354 |
uploaded_file = request.files.get("file")
|
| 355 |
+
if uploaded_file and ext:
|
| 356 |
+
df = get_df(uploaded_file, ext)
|
| 357 |
|
| 358 |
# 3) Process each section
|
| 359 |
for section_text in sections_raw:
|
|
|
|
| 360 |
img_prompt_match = re.search(r"<(.*?)>", section_text)
|
| 361 |
img_prompt = img_prompt_match.group(1).strip() if img_prompt_match else section_text[:100]
|
| 362 |
|
| 363 |
image_start = time.time()
|
| 364 |
image_obj = None
|
| 365 |
|
| 366 |
+
# Attempt chart generation if dataframe
|
| 367 |
if input_type == "dataframe" and df is not None:
|
| 368 |
try:
|
| 369 |
+
chart_str = generateResponse(img_prompt, df)
|
| 370 |
if chart_str and chart_str.startswith("data:image/png;base64,"):
|
|
|
|
| 371 |
base64_data = chart_str.split(",", 1)[1]
|
| 372 |
chart_bytes = base64.b64decode(base64_data)
|
| 373 |
image_obj = Image.open(io.BytesIO(chart_bytes))
|
|
|
|
| 408 |
# 4) Store the story
|
| 409 |
story_id = str(uuid.uuid4())
|
| 410 |
story_ref = db.reference(f"stories/{story_id}")
|
| 411 |
+
|
| 412 |
+
# ---------- STORE INPUT PARAMS -----------
|
| 413 |
+
input_params = {
|
| 414 |
+
"input_type": input_type,
|
| 415 |
+
"prompt": prompt, # might be None if pdf/dataframe
|
| 416 |
+
"wiki_url": wiki_url, # might be None
|
| 417 |
+
"bible_reference": bible_reference,
|
| 418 |
+
"youtube_url": youtube_url,
|
| 419 |
+
"story_type": story_type,
|
| 420 |
+
"style": style,
|
| 421 |
+
"voice_model": voice_model,
|
| 422 |
+
"image_model": image_model,
|
| 423 |
+
"audio_model": audio_model,
|
| 424 |
+
"ext": ext # for dataframe
|
| 425 |
+
}
|
| 426 |
+
# -----------------------------------------
|
| 427 |
+
|
| 428 |
story_record = {
|
| 429 |
"uid": uid,
|
| 430 |
"full_story": full_story,
|
|
|
|
| 436 |
},
|
| 437 |
"created_at": datetime.utcnow().isoformat(),
|
| 438 |
"input_type": input_type,
|
| 439 |
+
"story_type": story_type,
|
| 440 |
+
"input_params": input_params # <-- store them here
|
| 441 |
}
|
| 442 |
story_ref.set(story_record)
|
| 443 |
|
|
|
|
| 453 |
"preview": preview,
|
| 454 |
"sections": sections,
|
| 455 |
"generation_times": story_record["generation_times"],
|
| 456 |
+
"new_credits": new_credits,
|
| 457 |
+
"input_params": input_params # Return them if you want
|
| 458 |
})
|
| 459 |
|
| 460 |
except Exception as e:
|
|
|
|
| 1178 |
# ---------- Main ----------
|
| 1179 |
if __name__ == '__main__':
|
| 1180 |
# Create dummy admin account if it doesn't exist
|
| 1181 |
+
#create_dummy_admin()
|
| 1182 |
app.run(debug=True, host="0.0.0.0", port=7860)
|