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 |
|
| 12 |
# Preload models
|
| 13 |
models = {
|
|
@@ -51,33 +52,43 @@ def highlight_relevant_text(context, start, end):
|
|
| 51 |
)
|
| 52 |
return highlighted_text
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def generate_pdf_report(question, answer, score, score_explanation, score_chart, highlighted_context):
|
| 55 |
pdf = FPDF()
|
| 56 |
pdf.add_page()
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
pdf.
|
|
|
|
| 61 |
|
| 62 |
pdf.multi_cell(0, 10, f"Question: {question}")
|
| 63 |
pdf.ln()
|
| 64 |
|
| 65 |
-
pdf.set_font("
|
| 66 |
pdf.multi_cell(0, 10, f"Answer: {answer}")
|
| 67 |
pdf.ln()
|
| 68 |
|
| 69 |
-
pdf.set_font("
|
| 70 |
pdf.multi_cell(0, 10, f"Confidence Score: {score}")
|
| 71 |
pdf.ln()
|
| 72 |
|
| 73 |
-
pdf.set_font("
|
| 74 |
pdf.multi_cell(0, 10, f"Score Explanation: {score_explanation}")
|
| 75 |
pdf.ln()
|
| 76 |
|
| 77 |
-
pdf.set_font("
|
| 78 |
pdf.multi_cell(0, 10, "Highlighted Context:")
|
| 79 |
pdf.ln()
|
| 80 |
-
pdf.set_font("
|
| 81 |
pdf.multi_cell(0, 10, highlighted_context)
|
| 82 |
pdf.ln()
|
| 83 |
|
|
|
|
| 8 |
import torch
|
| 9 |
from fpdf import FPDF
|
| 10 |
import os
|
| 11 |
+
import glob
|
| 12 |
|
| 13 |
# Preload models
|
| 14 |
models = {
|
|
|
|
| 52 |
)
|
| 53 |
return highlighted_text
|
| 54 |
|
| 55 |
+
def find_system_font():
|
| 56 |
+
# Search common directories for TrueType fonts
|
| 57 |
+
font_dirs = ["/usr/share/fonts", "/usr/local/share/fonts"]
|
| 58 |
+
for font_dir in font_dirs:
|
| 59 |
+
ttf_files = glob.glob(os.path.join(font_dir, "**/*.ttf"), recursive=True)
|
| 60 |
+
if ttf_files:
|
| 61 |
+
return ttf_files[0] # Return the first found font
|
| 62 |
+
raise FileNotFoundError("No TTF font file found in system font directories.")
|
| 63 |
+
|
| 64 |
def generate_pdf_report(question, answer, score, score_explanation, score_chart, highlighted_context):
|
| 65 |
pdf = FPDF()
|
| 66 |
pdf.add_page()
|
| 67 |
|
| 68 |
+
# Find a system font
|
| 69 |
+
font_path = find_system_font()
|
| 70 |
+
pdf.add_font("SystemFont", "", font_path) # Use the found system font
|
| 71 |
+
pdf.set_font("SystemFont", size=12)
|
| 72 |
|
| 73 |
pdf.multi_cell(0, 10, f"Question: {question}")
|
| 74 |
pdf.ln()
|
| 75 |
|
| 76 |
+
pdf.set_font("SystemFont", size=12)
|
| 77 |
pdf.multi_cell(0, 10, f"Answer: {answer}")
|
| 78 |
pdf.ln()
|
| 79 |
|
| 80 |
+
pdf.set_font("SystemFont", size=12)
|
| 81 |
pdf.multi_cell(0, 10, f"Confidence Score: {score}")
|
| 82 |
pdf.ln()
|
| 83 |
|
| 84 |
+
pdf.set_font("SystemFont", size=12)
|
| 85 |
pdf.multi_cell(0, 10, f"Score Explanation: {score_explanation}")
|
| 86 |
pdf.ln()
|
| 87 |
|
| 88 |
+
pdf.set_font("SystemFont", size=12)
|
| 89 |
pdf.multi_cell(0, 10, "Highlighted Context:")
|
| 90 |
pdf.ln()
|
| 91 |
+
pdf.set_font("SystemFont", size=10)
|
| 92 |
pdf.multi_cell(0, 10, highlighted_context)
|
| 93 |
pdf.ln()
|
| 94 |
|