Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -368,48 +368,55 @@ def save_to_google_storage(bucket_name, file_path, destination_blob_name, expira
|
|
368 |
return signed_url
|
369 |
|
370 |
# Function to generate and save a document
|
371 |
-
def generate_document(task_description, md_content, user_name
|
372 |
-
logger.debug("
|
|
|
373 |
# Hash the task description to generate a unique filename
|
374 |
task_hash = generate_task_hash(task_description)
|
375 |
-
|
376 |
# Truncate the hash if needed (64 characters is sufficient for uniqueness)
|
377 |
max_hash_length = 64 # Adjust if needed
|
378 |
truncated_hash = task_hash[:max_hash_length]
|
379 |
-
|
380 |
# Generate PDF file locally
|
381 |
local_filename = f"{truncated_hash}.pdf" # Use the truncated hash as the local file name
|
382 |
pdf = FPDF()
|
383 |
pdf.add_page()
|
384 |
-
|
385 |
-
|
386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
# Process dictionary and render content
|
389 |
for key, value in md_content.items():
|
390 |
# Add key as a header
|
391 |
-
pdf.set_font(
|
392 |
pdf.multi_cell(0, 10, f"# {key}")
|
393 |
-
|
394 |
# Add value
|
395 |
-
pdf.set_font(
|
396 |
if isinstance(value, list): # Handle lists
|
397 |
for item in value:
|
398 |
pdf.multi_cell(0, 10, f"- {item}")
|
399 |
else: # Handle single strings
|
400 |
pdf.multi_cell(0, 10, value)
|
401 |
|
|
|
402 |
pdf.output(local_filename)
|
403 |
-
|
404 |
# Organize files into user-specific folders
|
405 |
destination_blob_name = f"{user_name}/{truncated_hash}.pdf"
|
406 |
|
407 |
# Upload to Google Cloud Storage and get the public URL
|
408 |
public_url = save_to_google_storage(bucket_name, local_filename, destination_blob_name)
|
409 |
-
logger.debug("
|
410 |
return public_url
|
411 |
|
412 |
-
|
413 |
# In[10]:
|
414 |
|
415 |
|
|
|
368 |
return signed_url
|
369 |
|
370 |
# Function to generate and save a document
|
371 |
+
def generate_document(task_description, md_content, user_name='jayw', bucket_name='curify'):
|
372 |
+
logger.debug("Starting to generate document")
|
373 |
+
|
374 |
# Hash the task description to generate a unique filename
|
375 |
task_hash = generate_task_hash(task_description)
|
376 |
+
|
377 |
# Truncate the hash if needed (64 characters is sufficient for uniqueness)
|
378 |
max_hash_length = 64 # Adjust if needed
|
379 |
truncated_hash = task_hash[:max_hash_length]
|
380 |
+
|
381 |
# Generate PDF file locally
|
382 |
local_filename = f"{truncated_hash}.pdf" # Use the truncated hash as the local file name
|
383 |
pdf = FPDF()
|
384 |
pdf.add_page()
|
385 |
+
|
386 |
+
# Use a unified font that supports Unicode (e.g., NotoSansCJK)
|
387 |
+
font_path = 'NotoSansCJK-Regular.ttc'
|
388 |
+
try:
|
389 |
+
pdf.add_font('NotoSansCJK', '', font_path, uni=True)
|
390 |
+
pdf.set_font('NotoSansCJK', size=12)
|
391 |
+
except Exception as e:
|
392 |
+
logger.error(f"Error loading font from {font_path}: {e}")
|
393 |
+
raise RuntimeError("Failed to load the unified font. Ensure the font file is accessible.")
|
394 |
|
395 |
# Process dictionary and render content
|
396 |
for key, value in md_content.items():
|
397 |
# Add key as a header
|
398 |
+
pdf.set_font('NotoSansCJK', style='B', size=12) # Bold font for headers
|
399 |
pdf.multi_cell(0, 10, f"# {key}")
|
400 |
+
|
401 |
# Add value
|
402 |
+
pdf.set_font('NotoSansCJK', size=12) # Regular font for content
|
403 |
if isinstance(value, list): # Handle lists
|
404 |
for item in value:
|
405 |
pdf.multi_cell(0, 10, f"- {item}")
|
406 |
else: # Handle single strings
|
407 |
pdf.multi_cell(0, 10, value)
|
408 |
|
409 |
+
# Output the PDF to a local file
|
410 |
pdf.output(local_filename)
|
411 |
+
|
412 |
# Organize files into user-specific folders
|
413 |
destination_blob_name = f"{user_name}/{truncated_hash}.pdf"
|
414 |
|
415 |
# Upload to Google Cloud Storage and get the public URL
|
416 |
public_url = save_to_google_storage(bucket_name, local_filename, destination_blob_name)
|
417 |
+
logger.debug("Finished generating document")
|
418 |
return public_url
|
419 |
|
|
|
420 |
# In[10]:
|
421 |
|
422 |
|