Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,22 +72,28 @@ def create_temp_file(content, suffix=".txt"):
|
|
| 72 |
|
| 73 |
# Case 1: If there's no block marker `[]`, treat it as a single-line input
|
| 74 |
if not re.search(r"\[\w+\]", content):
|
| 75 |
-
|
|
|
|
| 76 |
else:
|
| 77 |
-
#
|
| 78 |
blocks = re.split(r"(?=\[\w+\])", content) # Keeps headers in the split parts
|
| 79 |
-
|
| 80 |
-
# Remove excess newlines within each block
|
| 81 |
cleaned_blocks = [block.strip() for block in blocks if block.strip()]
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
formatted_content = "\n\n".join(cleaned_blocks)
|
| 85 |
-
|
| 86 |
-
# Write to temp file
|
| 87 |
fd, path = tempfile.mkstemp(suffix=suffix)
|
| 88 |
-
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
| 89 |
-
f.write(formatted_content)
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return path
|
| 92 |
|
| 93 |
def get_last_mp3_file(output_dir):
|
|
|
|
| 72 |
|
| 73 |
# Case 1: If there's no block marker `[]`, treat it as a single-line input
|
| 74 |
if not re.search(r"\[\w+\]", content):
|
| 75 |
+
# Normalize single-line input to avoid extra newlines
|
| 76 |
+
formatted_content = " ".join(content.split())
|
| 77 |
else:
|
| 78 |
+
# Case 2: If there are block markers, ensure blocks are separated by two newlines
|
| 79 |
blocks = re.split(r"(?=\[\w+\])", content) # Keeps headers in the split parts
|
|
|
|
|
|
|
| 80 |
cleaned_blocks = [block.strip() for block in blocks if block.strip()]
|
| 81 |
+
formatted_content = "\n\n".join(cleaned_blocks) # Ensure exactly two newlines
|
| 82 |
|
| 83 |
+
# Create temp file
|
|
|
|
|
|
|
|
|
|
| 84 |
fd, path = tempfile.mkstemp(suffix=suffix)
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
try:
|
| 87 |
+
# Open file in write mode and write the formatted content
|
| 88 |
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
| 89 |
+
f.write(formatted_content)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Error writing file: {e}")
|
| 92 |
+
return None
|
| 93 |
+
|
| 94 |
+
# Ensure file permissions allow reading
|
| 95 |
+
os.chmod(path, 0o644) # Read & write permissions for owner, read for others
|
| 96 |
+
|
| 97 |
return path
|
| 98 |
|
| 99 |
def get_last_mp3_file(output_dir):
|