-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
176 lines (143 loc) · 5.81 KB
/
app.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import openai
import os
import toml
from pytube import YouTube
import streamlit as st
from PIL import Image
import re
from gtts import gTTS
#------------Set up OpenAI API credentials------------
# with open("/secrets/secrets.toml", "r") as f:
# config = toml.load(f)
openai.api_key = st.secrets["OPENAI_KEY"]
#------------Model------------
def video_to_audio(video_URL:str, destination:str)-> None:
'''
Downloads the audio of the input URL and saves it into a .mp3 audio file
Args:
video_URL(str): URL of a youtube video
destination(str): path to temporarily store the extracted audio file
Returns:
None
'''
video = YouTube(video_URL)
# Convert video to Audio
audio = video.streams.filter(only_audio=True).first()
output = audio.download(output_path = destination)
_, ext = os.path.splitext(output)
new_file = "Target_audio" + '.mp3'
# Change the name of the file
os.rename(output, new_file)
def audio_to_text()-> None:
'''
Converts Target_audio.mp3 into text using whisper-1 model
Args:
None
Returns:
None
'''
audio_file= open("Target_audio.mp3", "rb")
transcript = openai.Audio.translate("whisper-1", audio_file)
return transcript['text']
def markdown_to_voice(text:str)-> None:
'''
Converts markdown into plain text format and saves it in voice_file.mp3
Args:
text(str): text in the format of markdown
Returns:
None
'''
output_file = "notes_voice.mp3"
#Convert markdown to plain text
cleaned_text = text.replace('#', ' ').replace('-', ' ').replace('.', ' ')
speech = gTTS(text = cleaned_text)
speech.save(output_file)
def generate_notes(text:str)-> str:
'''
Generates notes from input text
Args:
text(str): Generated text from audio file
Returns:
reply(str): Notes formatted in Markdown format
'''
prompt = """You are a teacher helping teach students with learning disabilities such as Dyslexia and ADHD.
The answer provided should always include these 5 sections listed below and should also contain any of the OPTIONAL SECTIONS listed below if applicable:
1) Title: containing an apt title for the text,
2) Summary: a brief summary containing a creative, meaningful and intuitive explanation of the text,
3) Key Takeaways: containing important points to remember from the text,
4) Mnemonics: use mnemonic devices (acronyms, acrostics, association, chunking, method of loci, songs or rhymes) to remember any important facts in the text,
5) Quiz Yourself!: a short quiz with questions on the key takeaways from the text along with the answers.
The answer provided must also include any of these optional sections below if conditions are true:
OPTIONAL SECTIONS:
- Formulae: containing any important formulae mentioned in the text,
- Code: containing any snippets of code mentioned in the text,
- Trivia: include an any important dates or information about important entities such persons, organizations, etc mentioned in text,
- Jargons: provide short explanations for any jargons present in the text,
The answer generated must always be appropiately formatted using the Markdown with each section being a subheading.
The language used to answer should be simple, compassionate and easily visualizable.
"""
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": text}
]
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=1.2,
top_p=0,
n=1,
stream=False,
presence_penalty=0,
frequency_penalty=0)
reply = chat.choices[0].message.content
return(reply)
def display_sidebar(text:str)-> None:
'''
Display the Markdown notes in the streamlit app
Args:
text(str): text in Markdown format
Returns:
None
'''
# Use regular expressions to find level 2 headers
pattern = r'^##\s+.+$'
headers = re.findall(pattern, text, re.MULTILINE)
# Add the headers to the sidebar
st.sidebar.markdown('## Table of Contents')
for header in headers:
text_without_hashes = header.replace('##', '').strip()
st.sidebar.markdown(f'- [{text_without_hashes}](#{text_without_hashes.lower().replace(" ", "-")})')
# Display the full text with headers
st.markdown(text, unsafe_allow_html=True)
#------------Streamlit app------------
def app():
st.set_page_config(page_title="Noteazy.AI")
st.title("Noteezy.AI 📑")
with st.sidebar:
# Add the logo image to the sidebar
image = Image.open("assets/images/Noteazyai-no-bg.png")
st.image(image)
# Add the header to the sidebar
st.header("Understanding complex topics made simple!")
st.write("_For students with special needs._")
# Get user input
video_URL = st.text_input("Paste the video URL here.")
# Generate Notes
if st.button("Generate Notes"):
if video_URL:
with st.spinner('Simplifying the content 📖....'):
destination = "."
video_to_audio(video_URL, destination)
text = audio_to_text()
os.remove('Target_audio.mp3')
output = generate_notes(text)
st.video(video_URL)
st.write("Listen to the notes in voice")
markdown_to_voice(output)
st.audio('notes_voice.mp3')
display_sidebar(output)
else:
st.warning("Please enter some text to summarize.")
# Run the Streamlit app
if __name__ == '__main__':
app()