tosin2013 commited on
Commit
ad9f9f3
1 Parent(s): c747902

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
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
- # Search common directories for TrueType fonts
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, "**/*.ttf"), recursive=True)
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
- # 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}")
75
  pdf.ln()
76
 
77
- pdf.set_font("SystemFont", size=12)
78
  pdf.multi_cell(0, 10, f"Answer: {answer}")
79
  pdf.ln()
80
 
81
- pdf.set_font("SystemFont", size=12)
82
  pdf.multi_cell(0, 10, f"Confidence Score: {score}")
83
  pdf.ln()
84
 
85
- pdf.set_font("SystemFont", size=12)
86
  pdf.multi_cell(0, 10, f"Score Explanation: {score_explanation}")
87
  pdf.ln()
88
 
89
- pdf.set_font("SystemFont", size=12)
90
  pdf.multi_cell(0, 10, "Highlighted Context:")
91
  pdf.ln()
92
- pdf.set_font("SystemFont", size=10)
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())
 
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())