Update app.py
Browse files
app.py
CHANGED
@@ -152,71 +152,77 @@ if prompt := st.chat_input("How can I help you today?"):
|
|
152 |
message_placeholder.markdown(full_response + "▌")
|
153 |
message_placeholder.markdown(full_response)
|
154 |
|
155 |
-
def generate_pdf():
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
def generate_audio():
|
182 |
-
with open('/home/user/app/audio/audio.mp3','wb') as sound_file:
|
183 |
-
tts = gTTS(result['result'], lang='en', tld='co.in')
|
184 |
-
tts.write_to_fp(sound_file)
|
185 |
-
sound = AudioSegment.from_mp3("/home/user/app/audio/audio.mp3")
|
186 |
-
sound.export("/home/user/app/audio/audio.wav", format="wav")
|
187 |
-
|
188 |
-
st.session_state['reference'] = '/home/user/app/pdf2image/default_output.png'
|
189 |
-
st.session_state['audio'] = ''
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
if "reference" not in st.session_state:
|
195 |
-
st.session_state.reference = False
|
196 |
-
if "audio" not in st.session_state:
|
197 |
-
st.session_state.audio = False
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
#choice = st.radio("References and TTS",["Reference" , 'TTS'])
|
202 |
-
on_ref = st.toggle('Reference')
|
203 |
-
if on_ref:
|
204 |
-
with st.sidebar:
|
205 |
-
generate_pdf()
|
206 |
-
st.session_state['reference'] = '/home/user/app/pdf2image/output.png'
|
207 |
-
st.image(st.session_state['reference'])
|
208 |
-
|
209 |
-
on_TTS = st.toggle('TTS')
|
210 |
-
if on_TTS:
|
211 |
-
with st.sidebar:
|
212 |
with open('/home/user/app/audio/audio.mp3','wb') as sound_file:
|
213 |
-
tts = gTTS(result['result'], lang='en', tld
|
214 |
tts.write_to_fp(sound_file)
|
215 |
sound = AudioSegment.from_mp3("/home/user/app/audio/audio.mp3")
|
216 |
sound.export("/home/user/app/audio/audio.wav", format="wav")
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
message_placeholder.markdown(full_response + "▌")
|
153 |
message_placeholder.markdown(full_response)
|
154 |
|
155 |
+
def generate_pdf():
|
156 |
+
page_number = int(result['source_documents'][0].metadata['page'])
|
157 |
+
doc = fitz.open(str(result['source_documents'][0].metadata['source']))
|
158 |
+
text = str(result['source_documents'][0].page_content)
|
159 |
+
if text != '':
|
160 |
+
for page in doc:
|
161 |
+
text_instances = page.search_for(text)
|
162 |
+
for inst in text_instances:
|
163 |
+
highlight = page.add_highlight_annot(inst)
|
164 |
+
highlight.update()
|
165 |
+
doc.save("/home/user/app/pdf2image/output.pdf", garbage=4, deflate=True, clean=True)
|
166 |
+
|
167 |
+
def pdf_page_to_image(pdf_file, page_number, output_image):
|
168 |
+
pdf_document = fitz.open(pdf_file)
|
169 |
+
page = pdf_document[page_number]
|
170 |
+
dpi = 300 # You can adjust this as needed
|
171 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(dpi / 100, dpi / 100))
|
172 |
+
pix.save(output_image, "png")
|
173 |
+
pdf_document.close()
|
174 |
+
pdf_page_to_image('/home/user/app/pdf2image/output.pdf', page_number, '/home/user/app/pdf2image/output.png')
|
175 |
+
#image = Image.open('/home/user/app/pdf2image/output.png')
|
176 |
+
#message_placeholder.image(image)
|
177 |
+
#st.session_state.reference = True
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
def generate_audio():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
with open('/home/user/app/audio/audio.mp3','wb') as sound_file:
|
183 |
+
tts = gTTS(result['result'], lang='en', tld='co.in')
|
184 |
tts.write_to_fp(sound_file)
|
185 |
sound = AudioSegment.from_mp3("/home/user/app/audio/audio.mp3")
|
186 |
sound.export("/home/user/app/audio/audio.wav", format="wav")
|
187 |
+
|
188 |
+
st.session_state['reference'] = '/home/user/app/pdf2image/default_output.png'
|
189 |
+
st.session_state['audio'] = ''
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
if "reference" not in st.session_state:
|
195 |
+
st.session_state.reference = False
|
196 |
+
if "audio" not in st.session_state:
|
197 |
+
st.session_state.audio = False
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
#choice = st.radio("References and TTS",["Reference" , 'TTS'])
|
202 |
+
on_ref = st.toggle('Reference', key='onref')
|
203 |
+
st.session_state.onref = False
|
204 |
+
|
205 |
+
if on_ref:
|
206 |
+
st.session_state.onref = True
|
207 |
+
with st.sidebar:
|
208 |
+
generate_pdf()
|
209 |
+
st.session_state['reference'] = '/home/user/app/pdf2image/output.png'
|
210 |
+
st.image(st.session_state['reference'])
|
211 |
+
|
212 |
+
on_TTS = st.toggle('TTS', key='ontts')
|
213 |
+
st.session_state.ontts = False
|
214 |
+
|
215 |
+
if on_TTS:
|
216 |
+
st.session_state.ontts = True
|
217 |
+
with st.sidebar:
|
218 |
+
with open('/home/user/app/audio/audio.mp3','wb') as sound_file:
|
219 |
+
tts = gTTS(result['result'], lang='en', tld = 'co.in')
|
220 |
+
tts.write_to_fp(sound_file)
|
221 |
+
sound = AudioSegment.from_mp3("/home/user/app/audio/audio.mp3")
|
222 |
+
sound.export("/home/user/app/audio/audio.wav", format="wav")
|
223 |
+
st.session_state['audio'] = '/home/user/app/audio/audio.wav'
|
224 |
+
st.audio(st.session_state['audio'])
|
225 |
+
|
226 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
227 |
+
|
228 |
+
|