JHAhmed commited on
Commit
d4b1fea
·
verified ·
1 Parent(s): 1232cb0

created-app.py

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