abhishekjoel commited on
Commit
1c5b3b4
·
verified ·
1 Parent(s): a740371

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -59
app.py CHANGED
@@ -62,17 +62,15 @@ def get_youtube_transcript(url):
62
  # Fetch the actual transcript data
63
  transcript_data = transcript.fetch()
64
  # Combine transcript texts
65
- transcription_text = "\n".join(
66
- [f"[{entry['start']:.2f}-{entry['start']+entry['duration']:.2f}] {entry['text']}" for entry in transcript_data]
67
- )
68
  return transcription_text
69
  except Exception as e:
70
  st.error(f"Error fetching YouTube transcript: {str(e)}")
71
  return None
72
 
73
- # Function to generate summary for each chunk
74
- def generate_summary(text):
75
- prompt = f"Summarize the following text into 500-800 words, focusing on key points:\n\n{text}"
76
  response = openai.ChatCompletion.create(
77
  model='gpt-3.5-turbo',
78
  messages=[{'role': 'user', 'content': prompt}],
@@ -80,16 +78,6 @@ def generate_summary(text):
80
  )
81
  return response.choices[0].message.content.strip()
82
 
83
- # Function to chat with AI
84
- def chat_with_ai(question, context):
85
- prompt = f"Based on the following content:\n\n{context}\n\nAnswer the question:\n{question}"
86
- response = openai.ChatCompletion.create(
87
- model='gpt-3.5-turbo',
88
- messages=[{'role': 'user', 'content': prompt}],
89
- max_tokens=500,
90
- )
91
- return response.choices[0].message.content.strip()
92
-
93
  # Main app
94
  def main():
95
  st.set_page_config(layout="wide")
@@ -99,9 +87,9 @@ def main():
99
  st.sidebar.header("Upload your file:")
100
  input_type = st.sidebar.selectbox("Select Input Type", ["Audio File", "PDF Document", "YouTube URL"])
101
  st.sidebar.markdown("### Steps to Use the Tool:")
102
- st.sidebar.markdown("1. Upload an audio file (25MB max), PDF, or YouTube URL for summary.")
103
- st.sidebar.markdown("2. Click on 'Generate Notes' to get an AI-transcribed summary.")
104
- st.sidebar.markdown("3. Use 'Chat with AI' for further questions.")
105
 
106
  # File uploader or URL input based on selected type
107
  audio_input = pdf_input = youtube_input = None
@@ -113,19 +101,17 @@ def main():
113
  youtube_input = st.sidebar.text_input("Enter YouTube URL (must have subtitles enabled)", key="youtube")
114
 
115
  # Main section for processing and results
116
- if st.button("Generate Notes", key="generate_summary"):
117
  transcription_text = ""
118
  if input_type == "Audio File" and audio_input:
119
  transcription = transcribe_audio(audio_input)
120
  if transcription:
121
- transcription_text = "\n".join(
122
- [f"[{seg['start']:.2f}-{seg['end']:.2f}] {seg['text']}" for seg in transcription['segments']]
123
- )
124
  else:
125
  st.error("Transcription failed.")
126
  elif input_type == "PDF Document" and pdf_input:
127
  chunks = extract_text_from_pdf(pdf_input)
128
- summaries = [generate_summary(chunk) for chunk in chunks]
129
  transcription_text = "\n".join(summaries)
130
  elif input_type == "YouTube URL" and youtube_input:
131
  transcription_text = get_youtube_transcript(youtube_input)
@@ -135,43 +121,30 @@ def main():
135
  st.error("Please provide valid input.")
136
 
137
  if transcription_text:
138
- st.session_state['transcription'] = transcription_text
139
- summary = generate_summary(transcription_text)
140
- st.session_state['summary'] = summary
141
-
142
- # Display transcription and summary side by side
143
- if 'transcription' in st.session_state and 'summary' in st.session_state:
144
- st.markdown("---")
145
- col1, col2 = st.columns([1, 1])
146
 
147
- with col1:
148
- st.subheader("Transcription with Timestamps")
149
- st.text_area("", st.session_state['transcription'], height=300)
150
-
151
- with col2:
152
- st.subheader("Generated Notes")
153
- st.text_area("", st.session_state['summary'], height=300)
154
-
155
- # Chat with AI at the bottom
156
  st.markdown("---")
157
- st.subheader("Chat with AI")
158
- question = st.text_input("Ask a question about the content:")
159
- if st.button("Save Notes to PDF"):
160
- pdf = FPDF()
161
- pdf.add_page()
162
- pdf.set_auto_page_break(auto=True, margin=15)
163
- pdf.set_font("Arial", size=12)
164
- for line in st.session_state['summary'].split('\n'):
165
- pdf.multi_cell(0, 10, line)
166
- pdf_output = io.BytesIO()
167
- pdf.output(pdf_output)
168
- pdf_output.seek(0)
169
- st.download_button(
170
- label="Download Notes as PDF",
171
- data=pdf_output,
172
- file_name="notes.pdf",
173
- mime="application/pdf"
174
- )
 
175
 
176
  if __name__ == "__main__":
177
  main()
 
62
  # Fetch the actual transcript data
63
  transcript_data = transcript.fetch()
64
  # Combine transcript texts
65
+ transcription_text = "\n".join([entry['text'] for entry in transcript_data])
 
 
66
  return transcription_text
67
  except Exception as e:
68
  st.error(f"Error fetching YouTube transcript: {str(e)}")
69
  return None
70
 
71
+ # Function to generate notes for each chunk
72
+ def generate_notes(text):
73
+ prompt = f"Create comprehensive notes in bullet points from the following text:\n\n{text}"
74
  response = openai.ChatCompletion.create(
75
  model='gpt-3.5-turbo',
76
  messages=[{'role': 'user', 'content': prompt}],
 
78
  )
79
  return response.choices[0].message.content.strip()
80
 
 
 
 
 
 
 
 
 
 
 
81
  # Main app
82
  def main():
83
  st.set_page_config(layout="wide")
 
87
  st.sidebar.header("Upload your file:")
88
  input_type = st.sidebar.selectbox("Select Input Type", ["Audio File", "PDF Document", "YouTube URL"])
89
  st.sidebar.markdown("### Steps to Use the Tool:")
90
+ st.sidebar.markdown("1. Upload an audio file (25MB max), PDF, or YouTube URL for notes.")
91
+ st.sidebar.markdown("2. Click on 'Generate Notes' to get AI-generated notes.")
92
+ st.sidebar.markdown("3. Use 'Download Notes as PDF' to save them locally.")
93
 
94
  # File uploader or URL input based on selected type
95
  audio_input = pdf_input = youtube_input = None
 
101
  youtube_input = st.sidebar.text_input("Enter YouTube URL (must have subtitles enabled)", key="youtube")
102
 
103
  # Main section for processing and results
104
+ if st.button("Generate Notes", key="generate_notes"):
105
  transcription_text = ""
106
  if input_type == "Audio File" and audio_input:
107
  transcription = transcribe_audio(audio_input)
108
  if transcription:
109
+ transcription_text = "\n".join([seg['text'] for seg in transcription['segments']])
 
 
110
  else:
111
  st.error("Transcription failed.")
112
  elif input_type == "PDF Document" and pdf_input:
113
  chunks = extract_text_from_pdf(pdf_input)
114
+ summaries = [generate_notes(chunk) for chunk in chunks]
115
  transcription_text = "\n".join(summaries)
116
  elif input_type == "YouTube URL" and youtube_input:
117
  transcription_text = get_youtube_transcript(youtube_input)
 
121
  st.error("Please provide valid input.")
122
 
123
  if transcription_text:
124
+ st.session_state['summary'] = transcription_text
 
 
 
 
 
 
 
125
 
126
+ # Display generated notes
127
+ if 'summary' in st.session_state:
 
 
 
 
 
 
 
128
  st.markdown("---")
129
+ st.subheader("Generated Notes")
130
+ st.text_area("", st.session_state['summary'], height=400)
131
+
132
+ # Button to save notes as PDF
133
+ pdf = FPDF()
134
+ pdf.add_page()
135
+ pdf.set_auto_page_break(auto=True, margin=15)
136
+ pdf.set_font("Arial", size=12)
137
+ for line in st.session_state['summary'].split('\n'):
138
+ pdf.multi_cell(0, 10, line)
139
+ pdf_output = io.BytesIO()
140
+ pdf.output(pdf_output)
141
+ pdf_output.seek(0)
142
+ st.download_button(
143
+ label="Download Notes as PDF",
144
+ data=pdf_output,
145
+ file_name="notes.pdf",
146
+ mime="application/pdf"
147
+ )
148
 
149
  if __name__ == "__main__":
150
  main()