akarshrajsingh7 commited on
Commit
b9648f5
·
1 Parent(s): f6554f2

Version 2 (Encoding fix)

Browse files
Files changed (3) hide show
  1. Logo.jpg +0 -0
  2. app.py +28 -10
  3. fast_reader.py +1 -1
Logo.jpg ADDED
app.py CHANGED
@@ -4,8 +4,9 @@ from concurrent.futures import ThreadPoolExecutor
4
  from base64 import b64encode
5
  from fpdf import FPDF
6
  import io, string, re, math
 
7
 
8
- #importing the class
9
  from fast_reader import Fastify_Reader
10
 
11
  def pdf_extract_text(pdf_docs):
@@ -19,9 +20,9 @@ def pdf_extract_text(pdf_docs):
19
  text += page.extract_text()
20
  return text
21
 
22
- def text_to_pdf_fastify(text):
23
-
24
  bold_text = Fastify_Reader(text).fastify()
 
25
  pdf = FPDF()
26
  pdf.add_page()
27
  pdf.set_font("Arial", size = 12)
@@ -29,16 +30,22 @@ def text_to_pdf_fastify(text):
29
  return bytes(pdf.output())
30
 
31
  def text_to_pdf(text):
 
32
  pdf = FPDF()
33
  pdf.add_page()
34
  pdf.set_font("Arial", size = 12)
35
  pdf.multi_cell(0, 10, txt = text, markdown=True)
36
  return bytes(pdf.output())
37
 
38
- st.set_page_config(layout="wide")
39
- st.header("Fastify Reader :books:")
40
- st.write("If the PDF viewer is not being rendered in your browser, try this link - https://akarshrajsingh7-fastify-reader.hf.space/?embed=true&embed_options=show_toolbar&embed_options=dark_theme#darktheme ")
41
- tab1, tab2= st.tabs(["PDF file", "Text File"])
 
 
 
 
 
42
 
43
  with tab1:
44
  st.header("PDF File")
@@ -47,21 +54,26 @@ with tab1:
47
  if st.button("Submit", key="pdf"):
48
  with st.spinner("Processing"):
49
  text = pdf_extract_text(uploaded_file)
50
-
51
  original_pdf = b64encode(text_to_pdf(text)).decode("utf-8")
52
  base64_pdf = b64encode(text_to_pdf_fastify(text)).decode("utf-8")
53
 
54
  original_display = f'<embed src="data:application/pdf;base64,{original_pdf}" width = "100%" height = 600 type="application/pdf">'
55
  pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width = "100%" height = 600 type="application/pdf">'
56
 
 
 
 
 
57
  col1, col2 = st.columns([1, 1], gap="small")
58
  with col1:
59
  with st.container(border = True):
 
60
  st.markdown(original_display, unsafe_allow_html=True)
61
  with col2:
62
  with st.container(border = True):
 
63
  st.markdown(pdf_display, unsafe_allow_html=True)
64
- st.download_button(label="Download as PDF", data=text_to_pdf_fastify(text), file_name='output.pdf', mime='application/pdf')
65
 
66
 
67
  with tab2:
@@ -78,11 +90,17 @@ with tab2:
78
  original_display = f'<embed src="data:application/pdf;base64,{original_pdf}" width = "100%" height = 600 type="application/pdf" download="original.pdf">'
79
  pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width = "100%" height = 600 type="application/pdf" download="Modified.pdf">'
80
 
 
 
 
 
81
  col1, col2 = st.columns([1, 1], gap="small")
82
  with col1:
83
  with st.container(border = True):
 
84
  st.markdown(original_display, unsafe_allow_html=True)
85
  with col2:
86
  with st.container(border = True):
 
87
  st.markdown(pdf_display, unsafe_allow_html=True)
88
- st.download_button(label="Download as PDF", data=text_to_pdf_fastify(text), file_name='output.pdf', mime='application/pdf')
 
4
  from base64 import b64encode
5
  from fpdf import FPDF
6
  import io, string, re, math
7
+ from io import StringIO
8
 
9
+ # Importing the Fastify Class
10
  from fast_reader import Fastify_Reader
11
 
12
  def pdf_extract_text(pdf_docs):
 
20
  text += page.extract_text()
21
  return text
22
 
23
+ def text_to_pdf_fastify(text):
 
24
  bold_text = Fastify_Reader(text).fastify()
25
+ bold_text = bold_text.encode('latin-1', 'ignore').decode('latin-1')
26
  pdf = FPDF()
27
  pdf.add_page()
28
  pdf.set_font("Arial", size = 12)
 
30
  return bytes(pdf.output())
31
 
32
  def text_to_pdf(text):
33
+ text = text.encode('latin-1', 'ignore').decode('latin-1')
34
  pdf = FPDF()
35
  pdf.add_page()
36
  pdf.set_font("Arial", size = 12)
37
  pdf.multi_cell(0, 10, txt = text, markdown=True)
38
  return bytes(pdf.output())
39
 
40
+ st.set_page_config(page_title="Fastify Reader",
41
+ page_icon=":books:",
42
+ layout="wide")
43
+
44
+ with st.sidebar:
45
+ st.image("Logo.jpg")
46
+ st.markdown("<div style='text-align: center;'>How fast can you read really?</div>", unsafe_allow_html=True)
47
+
48
+ tab1, tab2= st.tabs(["PDF file", "Input Text"])
49
 
50
  with tab1:
51
  st.header("PDF File")
 
54
  if st.button("Submit", key="pdf"):
55
  with st.spinner("Processing"):
56
  text = pdf_extract_text(uploaded_file)
57
+
58
  original_pdf = b64encode(text_to_pdf(text)).decode("utf-8")
59
  base64_pdf = b64encode(text_to_pdf_fastify(text)).decode("utf-8")
60
 
61
  original_display = f'<embed src="data:application/pdf;base64,{original_pdf}" width = "100%" height = 600 type="application/pdf">'
62
  pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width = "100%" height = 600 type="application/pdf">'
63
 
64
+ col1, col2, col3 = st.columns(3)
65
+ with col3:
66
+ st.download_button(label="Download Fastified PDF", data=text_to_pdf_fastify(text), file_name='output.pdf', mime='application/pdf')
67
+
68
  col1, col2 = st.columns([1, 1], gap="small")
69
  with col1:
70
  with st.container(border = True):
71
+ st.markdown("<div style='text-align: center;'><strong>Original PDF viewer</strong></div>", unsafe_allow_html=True)
72
  st.markdown(original_display, unsafe_allow_html=True)
73
  with col2:
74
  with st.container(border = True):
75
+ st.markdown("<div style='text-align: center;'><strong>Fastified PDF viewer</strong></div>", unsafe_allow_html=True)
76
  st.markdown(pdf_display, unsafe_allow_html=True)
 
77
 
78
 
79
  with tab2:
 
90
  original_display = f'<embed src="data:application/pdf;base64,{original_pdf}" width = "100%" height = 600 type="application/pdf" download="original.pdf">'
91
  pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width = "100%" height = 600 type="application/pdf" download="Modified.pdf">'
92
 
93
+ col1, col2, col3 = st.columns(3)
94
+ with col3:
95
+ st.download_button(label="Download Fastified PDF", data=text_to_pdf_fastify(text), file_name='output.pdf', mime='application/pdf')
96
+
97
  col1, col2 = st.columns([1, 1], gap="small")
98
  with col1:
99
  with st.container(border = True):
100
+ st.markdown("<div style='text-align: center;'><strong>Original PDF viewer</strong></div>", unsafe_allow_html=True)
101
  st.markdown(original_display, unsafe_allow_html=True)
102
  with col2:
103
  with st.container(border = True):
104
+ st.markdown("<div style='text-align: center;'><strong>Fastified PDF viewer</strong></div>", unsafe_allow_html=True)
105
  st.markdown(pdf_display, unsafe_allow_html=True)
106
+
fast_reader.py CHANGED
@@ -16,7 +16,7 @@ class Fastify_Reader:
16
 
17
  def fastify_word(self, word: str):
18
  if '-' in word:
19
- part_1, part_2 = word.split('-')
20
  part_1 = f"**{part_1[:self._get_offset(part_1)]}**{part_1[self._get_offset(part_1):]}"
21
  part_2 = f"**{part_2[:self._get_offset(part_2)]}**{part_2[self._get_offset(part_2):]}"
22
  fastify_word = f"{part_1}-{part_2}"
 
16
 
17
  def fastify_word(self, word: str):
18
  if '-' in word:
19
+ part_1, part_2 = word.rsplit('-', 1)
20
  part_1 = f"**{part_1[:self._get_offset(part_1)]}**{part_1[self._get_offset(part_1):]}"
21
  part_2 = f"**{part_2[:self._get_offset(part_2)]}**{part_2[self._get_offset(part_2):]}"
22
  fastify_word = f"{part_1}-{part_2}"