Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -18,6 +18,8 @@ import traceback
|
|
| 18 |
from video_gen import create_video
|
| 19 |
import zipfile
|
| 20 |
from fpdf import FPDF
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Initialize Flask app and CORS
|
| 23 |
app = Flask(__name__)
|
|
@@ -650,15 +652,24 @@ def download_story_archive(story_id):
|
|
| 650 |
# ======================
|
| 651 |
# 1) Generate PDF
|
| 652 |
# ======================
|
|
|
|
|
|
|
| 653 |
pdf = FPDF()
|
| 654 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 655 |
pdf.add_page()
|
| 656 |
pdf.set_font("Arial", size=12)
|
| 657 |
|
| 658 |
# Basic info
|
| 659 |
-
pdf.
|
| 660 |
-
pdf.
|
| 661 |
-
pdf.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
|
| 663 |
# For each section, create a new page
|
| 664 |
for idx, section_obj in enumerate(sections):
|
|
@@ -668,24 +679,39 @@ def download_story_archive(story_id):
|
|
| 668 |
|
| 669 |
pdf.add_page()
|
| 670 |
pdf.set_font("Arial", style='B', size=14)
|
| 671 |
-
pdf.
|
|
|
|
| 672 |
pdf.set_font("Arial", size=12)
|
| 673 |
pdf.multi_cell(0, 10, section_text)
|
|
|
|
| 674 |
|
| 675 |
# If there's an image URL, download & embed in PDF
|
| 676 |
if image_url and "firebasestorage.googleapis.com" in image_url:
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
# Save PDF to memory
|
| 691 |
pdf_buffer = io.BytesIO()
|
|
@@ -706,15 +732,18 @@ def download_story_archive(story_id):
|
|
| 706 |
# ======================
|
| 707 |
# 3) Download each section's image file to the ZIP
|
| 708 |
# ======================
|
| 709 |
-
# If you want each image in a separate folder, you can do "images/section_{idx+1}.jpg"
|
| 710 |
for idx, section_obj in enumerate(sections):
|
| 711 |
image_url = section_obj.get("image_url", "")
|
| 712 |
if image_url and "firebasestorage.googleapis.com" in image_url:
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
|
| 719 |
# ======================
|
| 720 |
# 4) Download each section's audio file to the ZIP
|
|
@@ -722,11 +751,15 @@ def download_story_archive(story_id):
|
|
| 722 |
for idx, section_obj in enumerate(sections):
|
| 723 |
audio_url = section_obj.get("audio_url", "")
|
| 724 |
if audio_url and "firebasestorage.googleapis.com" in audio_url:
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
|
| 731 |
# ------------------------------------------------------------------
|
| 732 |
# Serve ZIP File
|
|
@@ -740,6 +773,8 @@ def download_story_archive(story_id):
|
|
| 740 |
)
|
| 741 |
|
| 742 |
except Exception as e:
|
|
|
|
|
|
|
| 743 |
return jsonify({'error': str(e)}), 500
|
| 744 |
|
| 745 |
# ---------- Credit Request Endpoints ----------
|
|
|
|
| 18 |
from video_gen import create_video
|
| 19 |
import zipfile
|
| 20 |
from fpdf import FPDF
|
| 21 |
+
import tempfile
|
| 22 |
+
import urllib.parse
|
| 23 |
|
| 24 |
# Initialize Flask app and CORS
|
| 25 |
app = Flask(__name__)
|
|
|
|
| 652 |
# ======================
|
| 653 |
# 1) Generate PDF
|
| 654 |
# ======================
|
| 655 |
+
from fpdf import FPDF
|
| 656 |
+
|
| 657 |
pdf = FPDF()
|
| 658 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 659 |
pdf.add_page()
|
| 660 |
pdf.set_font("Arial", size=12)
|
| 661 |
|
| 662 |
# Basic info
|
| 663 |
+
pdf.cell(0, 10, f"Story ID: {story_id}")
|
| 664 |
+
pdf.ln()
|
| 665 |
+
pdf.cell(0, 10, f"Title: {title}")
|
| 666 |
+
pdf.ln(15)
|
| 667 |
+
|
| 668 |
+
# Add the full story text with proper line breaks
|
| 669 |
+
pdf.multi_cell(0, 10, "Full Story:")
|
| 670 |
+
pdf.ln(5)
|
| 671 |
+
pdf.multi_cell(0, 10, full_text)
|
| 672 |
+
pdf.ln(10)
|
| 673 |
|
| 674 |
# For each section, create a new page
|
| 675 |
for idx, section_obj in enumerate(sections):
|
|
|
|
| 679 |
|
| 680 |
pdf.add_page()
|
| 681 |
pdf.set_font("Arial", style='B', size=14)
|
| 682 |
+
pdf.cell(0, 10, f"Section {idx + 1}")
|
| 683 |
+
pdf.ln(15)
|
| 684 |
pdf.set_font("Arial", size=12)
|
| 685 |
pdf.multi_cell(0, 10, section_text)
|
| 686 |
+
pdf.ln(10)
|
| 687 |
|
| 688 |
# If there's an image URL, download & embed in PDF
|
| 689 |
if image_url and "firebasestorage.googleapis.com" in image_url:
|
| 690 |
+
try:
|
| 691 |
+
bucket = storage.bucket()
|
| 692 |
+
blob_path = urllib.parse.unquote(image_url.split('/o/')[-1].split('?')[0])
|
| 693 |
+
blob = bucket.blob(blob_path)
|
| 694 |
+
|
| 695 |
+
# Create a temporary file to store the image
|
| 696 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
|
| 697 |
+
temp_img_path = temp_file.name
|
| 698 |
+
# Download image to the temporary file
|
| 699 |
+
blob.download_to_filename(temp_img_path)
|
| 700 |
+
|
| 701 |
+
# Calculate appropriate image width (max 180 for A4 with margins)
|
| 702 |
+
page_width = pdf.w - 2*pdf.l_margin
|
| 703 |
+
image_width = min(page_width - 20, 160) # Keep some margin
|
| 704 |
+
|
| 705 |
+
# Add the image to PDF
|
| 706 |
+
# The key fix is properly handling the image insertion
|
| 707 |
+
pdf.image(temp_img_path, x=(pdf.w - image_width)/2, w=image_width)
|
| 708 |
+
|
| 709 |
+
# Clean up temp file
|
| 710 |
+
os.unlink(temp_img_path)
|
| 711 |
+
except Exception as img_error:
|
| 712 |
+
# Log error but continue if image insertion fails
|
| 713 |
+
pdf.ln(5)
|
| 714 |
+
pdf.multi_cell(0, 10, f"[Image could not be included: {str(img_error)}]")
|
| 715 |
|
| 716 |
# Save PDF to memory
|
| 717 |
pdf_buffer = io.BytesIO()
|
|
|
|
| 732 |
# ======================
|
| 733 |
# 3) Download each section's image file to the ZIP
|
| 734 |
# ======================
|
|
|
|
| 735 |
for idx, section_obj in enumerate(sections):
|
| 736 |
image_url = section_obj.get("image_url", "")
|
| 737 |
if image_url and "firebasestorage.googleapis.com" in image_url:
|
| 738 |
+
try:
|
| 739 |
+
bucket = storage.bucket()
|
| 740 |
+
blob_path = urllib.parse.unquote(image_url.split('/o/')[-1].split('?')[0])
|
| 741 |
+
blob = bucket.blob(blob_path)
|
| 742 |
+
image_data = blob.download_as_bytes()
|
| 743 |
+
zipf.writestr(f"image_section_{idx + 1}.jpg", image_data)
|
| 744 |
+
except Exception as img_error:
|
| 745 |
+
# Log error but continue with the rest of the process
|
| 746 |
+
print(f"Error downloading image for section {idx + 1}: {str(img_error)}")
|
| 747 |
|
| 748 |
# ======================
|
| 749 |
# 4) Download each section's audio file to the ZIP
|
|
|
|
| 751 |
for idx, section_obj in enumerate(sections):
|
| 752 |
audio_url = section_obj.get("audio_url", "")
|
| 753 |
if audio_url and "firebasestorage.googleapis.com" in audio_url:
|
| 754 |
+
try:
|
| 755 |
+
bucket = storage.bucket()
|
| 756 |
+
blob_path = urllib.parse.unquote(audio_url.split('/o/')[-1].split('?')[0])
|
| 757 |
+
blob = bucket.blob(blob_path)
|
| 758 |
+
audio_data = blob.download_as_bytes()
|
| 759 |
+
zipf.writestr(f"audio_section_{idx + 1}.mp3", audio_data)
|
| 760 |
+
except Exception as audio_error:
|
| 761 |
+
# Log error but continue with the rest of the process
|
| 762 |
+
print(f"Error downloading audio for section {idx + 1}: {str(audio_error)}")
|
| 763 |
|
| 764 |
# ------------------------------------------------------------------
|
| 765 |
# Serve ZIP File
|
|
|
|
| 773 |
)
|
| 774 |
|
| 775 |
except Exception as e:
|
| 776 |
+
import traceback
|
| 777 |
+
traceback.print_exc()
|
| 778 |
return jsonify({'error': str(e)}), 500
|
| 779 |
|
| 780 |
# ---------- Credit Request Endpoints ----------
|