Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import base64
|
|
8 |
import torch
|
9 |
from fpdf import FPDF
|
10 |
import os
|
|
|
11 |
import glob
|
12 |
|
13 |
# Preload models
|
@@ -65,9 +66,9 @@ def generate_pdf_report(question, answer, score, score_explanation, score_chart,
|
|
65 |
pdf = FPDF()
|
66 |
pdf.add_page()
|
67 |
|
68 |
-
#
|
69 |
font_path = find_system_font()
|
70 |
-
pdf.add_font("SystemFont", "", font_path)
|
71 |
pdf.set_font("SystemFont", size=12)
|
72 |
|
73 |
pdf.multi_cell(0, 10, f"Question: {question}")
|
@@ -92,15 +93,22 @@ def generate_pdf_report(question, answer, score, score_explanation, score_chart,
|
|
92 |
pdf.multi_cell(0, 10, highlighted_context)
|
93 |
pdf.ln()
|
94 |
|
95 |
-
#
|
96 |
score_chart_image = io.BytesIO(base64.b64decode(score_chart))
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Save PDF to memory
|
100 |
pdf_output = io.BytesIO()
|
101 |
pdf.output(pdf_output)
|
102 |
pdf_output.seek(0)
|
103 |
|
|
|
|
|
|
|
104 |
return pdf_output
|
105 |
|
106 |
def answer_question(model_name, file, question, status):
|
|
|
8 |
import torch
|
9 |
from fpdf import FPDF
|
10 |
import os
|
11 |
+
import tempfile
|
12 |
import glob
|
13 |
|
14 |
# Preload models
|
|
|
66 |
pdf = FPDF()
|
67 |
pdf.add_page()
|
68 |
|
69 |
+
# Add the Unicode font (e.g., DejaVuSans)
|
70 |
font_path = find_system_font()
|
71 |
+
pdf.add_font("SystemFont", "", font_path)
|
72 |
pdf.set_font("SystemFont", size=12)
|
73 |
|
74 |
pdf.multi_cell(0, 10, f"Question: {question}")
|
|
|
93 |
pdf.multi_cell(0, 10, highlighted_context)
|
94 |
pdf.ln()
|
95 |
|
96 |
+
# Decode and save the score chart image to a temporary file
|
97 |
score_chart_image = io.BytesIO(base64.b64decode(score_chart))
|
98 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmpfile:
|
99 |
+
tmpfile.write(score_chart_image.read())
|
100 |
+
tmpfile.flush()
|
101 |
+
tmpfile.close()
|
102 |
+
pdf.image(tmpfile.name, x=10, y=pdf.get_y(), w=100)
|
103 |
|
104 |
# Save PDF to memory
|
105 |
pdf_output = io.BytesIO()
|
106 |
pdf.output(pdf_output)
|
107 |
pdf_output.seek(0)
|
108 |
|
109 |
+
# Clean up temporary file
|
110 |
+
os.remove(tmpfile.name)
|
111 |
+
|
112 |
return pdf_output
|
113 |
|
114 |
def answer_question(model_name, file, question, status):
|