Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -54,46 +54,46 @@ def highlight_relevant_text(context, start, end):
|
|
54 |
return highlighted_text
|
55 |
|
56 |
def find_system_font():
|
57 |
-
#
|
58 |
font_dirs = ["/usr/share/fonts", "/usr/local/share/fonts"]
|
59 |
for font_dir in font_dirs:
|
60 |
-
ttf_files = glob.glob(os.path.join(font_dir, "
|
61 |
if ttf_files:
|
62 |
-
return ttf_files[0] # Return the first found font
|
63 |
-
raise FileNotFoundError("No TTF font file found in system font directories.")
|
64 |
|
65 |
def generate_pdf_report(question, answer, score, score_explanation, score_chart, highlighted_context):
|
66 |
pdf = FPDF()
|
67 |
pdf.add_page()
|
68 |
|
69 |
-
#
|
70 |
font_path = find_system_font()
|
71 |
-
pdf.add_font("
|
72 |
-
pdf.set_font("
|
73 |
|
74 |
pdf.multi_cell(0, 10, f"Question: {question}")
|
75 |
pdf.ln()
|
76 |
|
77 |
-
pdf.set_font("
|
78 |
pdf.multi_cell(0, 10, f"Answer: {answer}")
|
79 |
pdf.ln()
|
80 |
|
81 |
-
pdf.set_font("
|
82 |
pdf.multi_cell(0, 10, f"Confidence Score: {score}")
|
83 |
pdf.ln()
|
84 |
|
85 |
-
pdf.set_font("
|
86 |
pdf.multi_cell(0, 10, f"Score Explanation: {score_explanation}")
|
87 |
pdf.ln()
|
88 |
|
89 |
-
pdf.set_font("
|
90 |
pdf.multi_cell(0, 10, "Highlighted Context:")
|
91 |
pdf.ln()
|
92 |
-
pdf.set_font("
|
93 |
pdf.multi_cell(0, 10, highlighted_context)
|
94 |
pdf.ln()
|
95 |
|
96 |
-
#
|
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())
|
|
|
54 |
return highlighted_text
|
55 |
|
56 |
def find_system_font():
|
57 |
+
# Adjust this function to find a suitable font
|
58 |
font_dirs = ["/usr/share/fonts", "/usr/local/share/fonts"]
|
59 |
for font_dir in font_dirs:
|
60 |
+
ttf_files = glob.glob(os.path.join(font_dir, "**/NotoSans*.ttf"), recursive=True)
|
61 |
if ttf_files:
|
62 |
+
return ttf_files[0] # Return the first found NotoSans font
|
63 |
+
raise FileNotFoundError("No suitable TTF font file found in system font directories.")
|
64 |
|
65 |
def generate_pdf_report(question, answer, score, score_explanation, score_chart, highlighted_context):
|
66 |
pdf = FPDF()
|
67 |
pdf.add_page()
|
68 |
|
69 |
+
# Find and use a comprehensive Unicode font like NotoSans
|
70 |
font_path = find_system_font()
|
71 |
+
pdf.add_font("NotoSans", "", font_path)
|
72 |
+
pdf.set_font("NotoSans", size=12)
|
73 |
|
74 |
pdf.multi_cell(0, 10, f"Question: {question}")
|
75 |
pdf.ln()
|
76 |
|
77 |
+
pdf.set_font("NotoSans", size=12)
|
78 |
pdf.multi_cell(0, 10, f"Answer: {answer}")
|
79 |
pdf.ln()
|
80 |
|
81 |
+
pdf.set_font("NotoSans", size=12)
|
82 |
pdf.multi_cell(0, 10, f"Confidence Score: {score}")
|
83 |
pdf.ln()
|
84 |
|
85 |
+
pdf.set_font("NotoSans", size=12)
|
86 |
pdf.multi_cell(0, 10, f"Score Explanation: {score_explanation}")
|
87 |
pdf.ln()
|
88 |
|
89 |
+
pdf.set_font("NotoSans", size=12)
|
90 |
pdf.multi_cell(0, 10, "Highlighted Context:")
|
91 |
pdf.ln()
|
92 |
+
pdf.set_font("NotoSans", size=10)
|
93 |
pdf.multi_cell(0, 10, highlighted_context)
|
94 |
pdf.ln()
|
95 |
|
96 |
+
# Handle the image as 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())
|