File size: 4,282 Bytes
01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea 01513b9 d4b1fea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | from openai import OpenAI
from pathlib import Path
import streamlit as st
import PyPDF2
import os
st.set_page_config(page_title="ReadPDF")
if "output_file_name" not in st.session_state:
st.session_state.output_file_name = ""
if "file_name" not in st.session_state:
st.session_state.file_name = ""
if "audio_processed" not in st.session_state:
st.session_state.audio_processed = False
def extract_text(pdf_path, from_page=10, to_page=10):
try:
with open(pdf_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader (file)
total_pages = len(pdf_reader.pages)
# Ensure from_page and to_page are within valid range
from_page = max(1, min(from_page, total_pages))
to_page = max(from_page, min(to_page, total_pages))
# Limit the number of pages to extract to a maximum of 10
to_page = min(from_page + 9, to_page)
st.session_state.output_file_name = f"{st.session_state.file_name[:-4]} P{from_page}-P{to_page}.mp3"
text = ""
for page_num in range(from_page - 1, to_page):
page = pdf_reader.pages[page_num]
text += page.extract_text()
return text
except Exception as e:
print(f"Error: {e}")
return 0
def process(voice):
with st.spinner("Uploading..."):
client = OpenAI(api_key=st.session_state.api_key)
extracted_text = extract_text(Path("Data", st.session_state.file_name), st.session_state.from_page, st.session_state.to_page)
if extracted_text == 0:
st.error('Page numbers out of bounds!', icon="🚨")
return "Error!"
print("THIS WILL NOT GET PRINTED!")
text_prompt = f"{extracted_text}\n\n Make corrections to this text, such as puncuation, typing errors, missing letters, and formatting."
text_response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a text correction assistant."},
{"role": "user", "content": text_prompt},
]
)
output = text_response.choices[0].message.content
response = client.audio.speech.create(
model="tts-1",
voice=voice.lower(),
input=output,
)
response.stream_to_file(st.session_state.output_file_name)
st.session_state.audio_processed = True
st.success('Processed!')
def upload_docs () :
path_check = os.path.isdir("Data")
if not path_check:
os.makedirs("Data")
with st.spinner("Uploading..."):
doc = st.session_state.uploaded_file
with open(Path("Data", doc.name),"wb") as f:
f.write(doc.getbuffer())
st.session_state.file_name = doc.name
st.success('Done!')
def create_upload () :
uploaded_doc = st.file_uploader("Upload Files", type=["pdf"], accept_multiple_files=False, key="uploaded_file",
help="Upload documents to embed!", on_change=upload_docs, label_visibility="visible")
return upload_docs
st.title("ReadPDF")
file_uploaded = not len(st.session_state.file_name) > 1
upload_button = create_upload()
st.text(f"Current Document: {st.session_state.file_name}")
st.text_input('API Key', key="api_key")
from_page = st.number_input("From Page: ", value=10, min_value=1, max_value=300, step=1, key="from_page")
to_page = st.number_input("To Page: ", value=10, min_value=1, max_value=300, step=1, key="to_page")
voice = st.selectbox(
"Choose TTS Voice",
("Alloy", "Echo", "Fable", "Onyx", "Nova", "Shimmer"),
key="voice"
)
st.markdown("[You can listen to the different voice options here!](https://platform.openai.com/docs/guides/text-to-speech/voice-options)")
st.button("Process", type="secondary", on_click=process, disabled=file_uploaded, kwargs={"voice": st.session_state.voice})
if (st.session_state.audio_processed):
with open(st.session_state.output_file_name, "rb") as file:
btn = st.download_button(
label="Download Audio",
data=file,
file_name=st.session_state.output_file_name,
mime="audio/mpeg"
) |