Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -107,49 +107,96 @@ Do not name the company if name is not there and return just the report and noth
|
|
| 107 |
|
| 108 |
|
| 109 |
def create_pdf_report(report_text):
|
| 110 |
-
|
| 111 |
"""
|
| 112 |
-
Create a PDF from markdown text using the md-to-pdf API.
|
| 113 |
|
| 114 |
Args:
|
| 115 |
report_text (str): Markdown formatted report text
|
| 116 |
|
| 117 |
Returns:
|
| 118 |
BytesIO: PDF file in memory buffer
|
| 119 |
-
"""
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
api_url = "https://md-to-pdf.fly.dev"
|
|
|
|
|
|
|
| 122 |
css = """
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
}
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
payload = {
|
| 141 |
-
'markdown':
|
| 142 |
'engine': 'weasyprint',
|
| 143 |
'css': css
|
| 144 |
}
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
return BytesIO(response.content)
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def main():
|
| 155 |
st.title("Quantitlytix AI")
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
def create_pdf_report(report_text):
|
|
|
|
| 110 |
"""
|
| 111 |
+
Create a PDF from markdown text using the md-to-pdf API with enhanced formatting.
|
| 112 |
|
| 113 |
Args:
|
| 114 |
report_text (str): Markdown formatted report text
|
| 115 |
|
| 116 |
Returns:
|
| 117 |
BytesIO: PDF file in memory buffer
|
|
|
|
| 118 |
|
| 119 |
+
Raises:
|
| 120 |
+
Exception: If PDF generation fails
|
| 121 |
+
"""
|
| 122 |
api_url = "https://md-to-pdf.fly.dev"
|
| 123 |
+
|
| 124 |
+
# Enhanced CSS for better table formatting
|
| 125 |
css = """
|
| 126 |
+
@page {
|
| 127 |
+
margin: 20mm;
|
| 128 |
+
size: A4;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
body {
|
| 132 |
+
font-family: 'Helvetica', sans-serif;
|
| 133 |
+
line-height: 1.6;
|
| 134 |
+
color: #333;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
h1, h2 {
|
| 138 |
+
color: #191970; /* MidnightBlue */
|
| 139 |
+
border-bottom: 2px solid #eee;
|
| 140 |
+
padding-bottom: 0.3em;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
table {
|
| 144 |
+
width: 100%;
|
| 145 |
+
border-collapse: collapse;
|
| 146 |
+
margin: 1em 0;
|
| 147 |
+
page-break-inside: avoid;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
th {
|
| 151 |
+
background-color: #f8f9fa;
|
| 152 |
+
font-weight: bold;
|
| 153 |
+
text-align: left;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
th, td {
|
| 157 |
+
padding: 0.75em;
|
| 158 |
+
border: 1px solid #ddd;
|
| 159 |
+
word-wrap: break-word;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
tr:nth-child(even) {
|
| 163 |
+
background-color: #f9f9f9;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.amount {
|
| 167 |
+
text-align: right;
|
| 168 |
+
white-space: nowrap;
|
| 169 |
+
}
|
| 170 |
+
"""
|
| 171 |
+
|
| 172 |
+
# Pre-process markdown to clean up table formatting
|
| 173 |
+
processed_md = "\n".join([line.rstrip() for line in report_text.split("\n")])
|
| 174 |
+
|
| 175 |
payload = {
|
| 176 |
+
'markdown': processed_md,
|
| 177 |
'engine': 'weasyprint',
|
| 178 |
'css': css
|
| 179 |
}
|
| 180 |
|
| 181 |
+
try:
|
| 182 |
+
response = requests.post(
|
| 183 |
+
api_url,
|
| 184 |
+
data=payload,
|
| 185 |
+
timeout=30 # Add timeout for API call
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
response.raise_for_status() # Raise exception for 4xx/5xx errors
|
| 189 |
+
|
| 190 |
+
# Validate PDF content
|
| 191 |
+
if not response.content.startswith(b'%PDF-'):
|
| 192 |
+
raise ValueError("Invalid PDF content received from API")
|
| 193 |
+
|
| 194 |
return BytesIO(response.content)
|
| 195 |
+
|
| 196 |
+
except requests.exceptions.RequestException as e:
|
| 197 |
+
raise Exception(f"API request failed: {str(e)}") from e
|
| 198 |
+
except ValueError as e:
|
| 199 |
+
raise Exception(f"Invalid PDF response: {str(e)}") from e
|
| 200 |
|
| 201 |
def main():
|
| 202 |
st.title("Quantitlytix AI")
|