Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,13 @@ from PIL import Image
|
|
| 8 |
import io
|
| 9 |
import base64
|
| 10 |
import google.generativeai as genai
|
| 11 |
-
from fpdf import FPDF
|
| 12 |
import markdown2
|
| 13 |
import re
|
| 14 |
import json
|
| 15 |
import os
|
| 16 |
from markdown_pdf import MarkdownPdf, Section
|
|
|
|
| 17 |
|
| 18 |
# Configure Gemini API
|
| 19 |
gemini_api_key = os.environ.get('GOOGLE_API_KEY')
|
|
@@ -92,58 +93,7 @@ def create_dashboard(data):
|
|
| 92 |
title='Top 10 Products by Quantity Sold')
|
| 93 |
st.plotly_chart(fig, use_container_width=True)
|
| 94 |
|
| 95 |
-
# ... [Keep the md_to_pdf, generate_pdf, StreamLitResponse, and generateResponse functions same as before but update generateResponse]
|
| 96 |
-
def md_to_pdf(md_text, pdf):
|
| 97 |
-
"""Renders basic Markdown to PDF using fpdf text functions (limited formatting)."""
|
| 98 |
-
md = markdown2.markdown(md_text) # Parse Markdown
|
| 99 |
-
lines = md.split('\n') # Split into lines
|
| 100 |
-
pdf.set_font("Arial", "", 12) # Set default font
|
| 101 |
|
| 102 |
-
for line in lines:
|
| 103 |
-
line = line.strip()
|
| 104 |
-
|
| 105 |
-
# Basic heading support (adjust as needed)
|
| 106 |
-
if line.startswith("# "):
|
| 107 |
-
pdf.set_font("Arial", "B", 18)
|
| 108 |
-
pdf.cell(0, 10, line[2:], ln=True)
|
| 109 |
-
elif line.startswith("## "):
|
| 110 |
-
pdf.set_font("Arial", "B", 16)
|
| 111 |
-
pdf.cell(0, 10, line[3:], ln=True)
|
| 112 |
-
elif line.startswith("### "):
|
| 113 |
-
pdf.set_font("Arial", "B", 14)
|
| 114 |
-
pdf.cell(0, 10, line[4:], ln=True)
|
| 115 |
-
|
| 116 |
-
# Basic bold text support (very limited)
|
| 117 |
-
elif "**" in line:
|
| 118 |
-
parts = line.split("**")
|
| 119 |
-
for i, part in enumerate(parts):
|
| 120 |
-
if i % 2 == 1: # Bold text
|
| 121 |
-
pdf.set_font("Arial", "B", 12)
|
| 122 |
-
pdf.cell(0, 10, part, ln=False) # Don't add newline for inline bold
|
| 123 |
-
else:
|
| 124 |
-
pdf.set_font("Arial", "", 12)
|
| 125 |
-
pdf.cell(0, 10, part, ln=False)
|
| 126 |
-
pdf.ln() # Newline after the whole line
|
| 127 |
-
|
| 128 |
-
# Add other basic formatting as needed...
|
| 129 |
-
|
| 130 |
-
else: # Normal text
|
| 131 |
-
pdf.set_font("Arial", "", 12)
|
| 132 |
-
pdf.multi_cell(0, 10, line) # multi_cell for wrapping
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
def generate_pdf(report_text):
|
| 136 |
-
"""Generates PDF from report text."""
|
| 137 |
-
pdf = FPDF()
|
| 138 |
-
pdf.add_page()
|
| 139 |
-
try:
|
| 140 |
-
pdf.add_font('Arial', '', 'arial.ttf', uni=True) # Add unicode support
|
| 141 |
-
except:
|
| 142 |
-
st.warning("Arial font not found. Unicode might not work.")
|
| 143 |
-
pdf.set_font("Arial", "", 12)
|
| 144 |
-
md_to_pdf(report_text, pdf)
|
| 145 |
-
pdf_bytes = pdf.output(dest="S").encode("latin1")
|
| 146 |
-
return pdf_bytes
|
| 147 |
|
| 148 |
# --- Chat Tab Functions ---
|
| 149 |
class StreamLitResponse(ResponseParser):
|
|
@@ -345,26 +295,34 @@ def main():
|
|
| 345 |
"""
|
| 346 |
response = model.generate_content(prompt)
|
| 347 |
report = response.text
|
|
|
|
| 348 |
|
| 349 |
-
|
| 350 |
|
| 351 |
# PDF Generation and display
|
| 352 |
try:
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
st.download_button(
|
| 358 |
label="Download Report as PDF",
|
| 359 |
-
data=
|
| 360 |
file_name="report.pdf",
|
| 361 |
mime="application/pdf"
|
| 362 |
-
|
| 363 |
-
st.
|
| 364 |
-
# Display the report below the download button
|
| 365 |
except Exception as e:
|
| 366 |
st.error(f"Error generating PDF: {e}")
|
| 367 |
-
st.
|
| 368 |
|
| 369 |
else:
|
| 370 |
st.error("No data available for reports")
|
|
|
|
| 8 |
import io
|
| 9 |
import base64
|
| 10 |
import google.generativeai as genai
|
| 11 |
+
#from fpdf import FPDF
|
| 12 |
import markdown2
|
| 13 |
import re
|
| 14 |
import json
|
| 15 |
import os
|
| 16 |
from markdown_pdf import MarkdownPdf, Section
|
| 17 |
+
import tempfile
|
| 18 |
|
| 19 |
# Configure Gemini API
|
| 20 |
gemini_api_key = os.environ.get('GOOGLE_API_KEY')
|
|
|
|
| 93 |
title='Top 10 Products by Quantity Sold')
|
| 94 |
st.plotly_chart(fig, use_container_width=True)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# --- Chat Tab Functions ---
|
| 99 |
class StreamLitResponse(ResponseParser):
|
|
|
|
| 295 |
"""
|
| 296 |
response = model.generate_content(prompt)
|
| 297 |
report = response.text
|
| 298 |
+
html_text = markdown2.markdown(markdown_text)
|
| 299 |
|
| 300 |
+
|
| 301 |
|
| 302 |
# PDF Generation and display
|
| 303 |
try:
|
| 304 |
+
# Create a temporary file to store the PDF
|
| 305 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 306 |
+
pdf = MarkdownPdf()
|
| 307 |
+
pdf.meta["title"] = 'Suburb Business Report'
|
| 308 |
+
pdf.add_section(Section(report, toc=False))
|
| 309 |
+
pdf.save(tmp_file.name) # Save the PDF to the temporary file
|
| 310 |
+
|
| 311 |
+
# Read the PDF bytes from the temporary file
|
| 312 |
+
with open(tmp_file.name, "rb") as f:
|
| 313 |
+
pdf_bytes = f.read()
|
| 314 |
+
|
| 315 |
+
# Provide the PDF for download
|
| 316 |
st.download_button(
|
| 317 |
label="Download Report as PDF",
|
| 318 |
+
data=pdf_bytes,
|
| 319 |
file_name="report.pdf",
|
| 320 |
mime="application/pdf"
|
| 321 |
+
)
|
| 322 |
+
st.write(html_text, unsafe_allow_html=True) # Display the report below the download button
|
|
|
|
| 323 |
except Exception as e:
|
| 324 |
st.error(f"Error generating PDF: {e}")
|
| 325 |
+
st.write(html_text, unsafe_allow_html=True)
|
| 326 |
|
| 327 |
else:
|
| 328 |
st.error("No data available for reports")
|