abhishekjoel commited on
Commit
515022a
Β·
verified Β·
1 Parent(s): d1ceb0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +224 -97
app.py CHANGED
@@ -1,112 +1,239 @@
1
- import streamlit as st
2
  import openai
3
- import io
 
 
 
4
  from fpdf import FPDF
5
- import os
 
6
 
7
- # Set API key from environment variable
 
 
 
8
  openai.api_key = os.getenv('OPENAI_API_KEY')
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def transcribe_audio(audio_file):
11
- """
12
- Transcribe audio using OpenAI's Whisper model.
13
-
14
- Args:
15
- audio_file: Audio file bytes.
16
-
17
- Returns:
18
- Transcription text.
19
- """
20
  try:
21
- audio_file_obj = io.BytesIO(audio_file.read())
22
- audio_file_obj.name = audio_file.name
23
- transcript = openai.Audio.transcribe(
24
- model="whisper-1",
25
- file=audio_file_obj
26
- )
27
- transcription = transcript.get('text', '')
28
- return transcription
29
- except openai.error.APIError as e:
30
- st.error(f"OpenAI API error: {e}")
 
 
 
 
 
 
 
 
31
  return None
32
 
33
- def extract_key_topics(transcription, lesson_plan):
34
- """
35
- Extract key topics from the transcription using OpenAI GPT-3.5.
36
-
37
- Args:
38
- transcription: Transcription text.
39
- lesson_plan: List of lesson plan topics.
40
-
41
- Returns:
42
- Key topics text.
 
 
43
  """
44
- prompt = f"Here is a transcription of a lecture: '{transcription}'.\n"
45
- prompt += "The following are key topics from the lesson plan:\n"
46
- for topic in lesson_plan:
47
- prompt += f"- {topic}\n"
48
- prompt += "Please find the sentences in the transcription that match these key topics and include timestamps if possible."
49
-
50
- try:
51
- response = openai.ChatCompletion.create(
52
- model="gpt-3.5-turbo",
53
- messages=[
54
- {"role": "system", "content": "You are a helpful assistant."},
55
- {"role": "user", "content": prompt}
56
- ]
57
- )
58
- key_topics = response['choices'][0]['message']['content']
59
- return key_topics
60
- except openai.error.APIError as e:
61
- st.error(f"OpenAI API error: {e}")
62
- return None
63
-
64
- def generate_pdf(notes):
65
- """
66
- Generate a PDF file from notes.
67
-
68
- Args:
69
- notes: List of notes.
70
-
71
- Returns:
72
- PDF bytes.
73
- """
74
- pdf = FPDF()
75
- pdf.add_page()
76
- pdf.set_font("Arial", size=12)
77
-
78
- for note in notes:
79
- pdf.cell(200, 10, txt=note, ln=True)
80
-
81
- return pdf.output(dest='S').encode('latin1')
82
-
83
  def main():
84
- st.title("AI Teaching Assistant - Lecture Notes Generation")
85
- st.sidebar.header("Upload Audio")
86
-
87
- col1, col2 = st.columns(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  with col1:
89
- audio_file = st.file_uploader("Upload Lecture Audio", type=["wav", "mp3"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  with col2:
91
- if audio_file:
92
- st.audio(audio_file, format='audio/wav')
93
-
94
- if audio_file:
95
- transcription = transcribe_audio(audio_file)
96
- st.success("Transcription complete!")
97
- st.write("### Transcription:")
98
- st.write(transcription)
99
-
100
- lesson_plan = st.text_input("Enter lesson plan topics (comma-separated):")
101
- lesson_plan = [topic.strip() for topic in lesson_plan.split(",")]
102
- key_topics = extract_key_topics(transcription, lesson_plan)
103
- st.write("### Generated Key Topics:")
104
- st.write(key_topics)
105
-
106
- pdf_data = generate_pdf(key_topics.splitlines())
107
- st.download_button("Download PDF", data=pdf_data, file_name="lecture_notes.pdf", mime='application/pdf')
108
-
109
- st.sidebar.markdown("[Home](https://studyscribe.framer.ai)", unsafe_allow_html=True)
110
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  if __name__ == "__main__":
112
  main()
 
1
+ import os
2
  import openai
3
+ import streamlit as st
4
+ from PyPDF2 import PdfReader
5
+ from youtube_transcript_api import YouTubeTranscriptApi
6
+ from pydub import AudioSegment
7
  from fpdf import FPDF
8
+ from dotenv import load_dotenv
9
+ import random
10
 
11
+ # Load environment variables from .env file
12
+ load_dotenv()
13
+
14
+ # Set OpenAI API key
15
  openai.api_key = os.getenv('OPENAI_API_KEY')
16
 
17
+ # Function to summarize text using GPT
18
+ def summarize_text(text, model="gpt-3.5-turbo"):
19
+ response = openai.ChatCompletion.create(
20
+ model=model,
21
+ messages=[
22
+ {"role": "user", "content": text}
23
+ ],
24
+ max_tokens=150,
25
+ )
26
+ return response['choices'][0]['message']['content']
27
+
28
+ # Function to transcribe audio using OpenAI Whisper
29
  def transcribe_audio(audio_file):
 
 
 
 
 
 
 
 
 
30
  try:
31
+ # Read the uploaded audio file directly as bytes
32
+ audio_bytes = audio_file.read()
33
+
34
+ # Save the audio bytes temporarily
35
+ with open("temp_audio_file.wav", "wb") as temp_file:
36
+ temp_file.write(audio_bytes)
37
+
38
+ # Convert to mp3 for Whisper API
39
+ audio = AudioSegment.from_wav("temp_audio_file.wav")
40
+ audio.export("temp_audio_file.mp3", format="mp3")
41
+
42
+ with open("temp_audio_file.mp3", "rb") as f:
43
+ # Attempt to transcribe audio
44
+ transcript_response = openai.Audio.transcribe("whisper-1", f)
45
+
46
+ return transcript_response['text']
47
+ except Exception as e:
48
+ st.error(f"Error during transcription: {str(e)}")
49
  return None
50
 
51
+ # New function to generate quiz questions
52
+ def generate_quiz_questions(summarized_text, num_questions=10):
53
+ prompt = f"""Based on the following summarized text, generate {num_questions} multiple-choice questions with 4 options each. Format each question as follows:
54
+ Q: [Question]
55
+ A) [Option A]
56
+ B) [Option B]
57
+ C) [Option C]
58
+ D) [Option D]
59
+ Correct: [Correct option letter]
60
+
61
+ Summarized text:
62
+ {summarized_text}
63
  """
64
+
65
+ response = openai.ChatCompletion.create(
66
+ model="gpt-3.5-turbo",
67
+ messages=[
68
+ {"role": "system", "content": "You are a helpful assistant that generates quiz questions based on summarized text."},
69
+ {"role": "user", "content": prompt}
70
+ ],
71
+ max_tokens=1000,
72
+ )
73
+
74
+ return response['choices'][0]['message']['content']
75
+
76
+ # Function to parse quiz questions
77
+ def parse_quiz_questions(quiz_text):
78
+ questions = []
79
+ for q in quiz_text.split('Q:')[1:]:
80
+ question = {}
81
+ lines = q.strip().split('\n')
82
+ question['question'] = lines[0].strip()
83
+ question['options'] = [line.strip()[3:] for line in lines[1:5]]
84
+ question['correct'] = lines[5].split(':')[1].strip()
85
+ questions.append(question)
86
+ return questions
87
+
88
+ # Main app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  def main():
90
+ st.set_page_config(page_title="AI Physics Summarization Assistant", layout="wide")
91
+
92
+ # Sidebar for navigation
93
+ page = st.sidebar.selectbox("Choose a page", ["Summarization", "Quiz"])
94
+
95
+ if page == "Summarization":
96
+ summarization_page()
97
+ elif page == "Quiz":
98
+ quiz_page()
99
+
100
+ def summarization_page():
101
+ st.title("πŸ§ͺ AI Physics Summarization Assistant")
102
+
103
+ # Layout: Create two columns
104
+ col1, col2 = st.columns([1, 2]) # Left column for inputs, right column for summaries and chat
105
+
106
+ # Input options in the left column
107
  with col1:
108
+ st.header("πŸ” Input Options")
109
+
110
+ # 1. Audio Summarization
111
+ st.subheader("Upload Audio File for Summarization")
112
+ audio_file = st.file_uploader("Choose an audio file...", type=["wav", "mp3", "m4a"])
113
+
114
+ if audio_file is not None:
115
+ st.audio(audio_file) # Display audio player for uploaded file
116
+ if st.button("πŸ”Š Summarize Audio", key="audio_summary"):
117
+ audio_text = transcribe_audio(audio_file)
118
+ if audio_text:
119
+ summarized_text = summarize_text(audio_text)
120
+ st.session_state.summarized_text = summarized_text # Save summarized text to session state
121
+
122
+ st.markdown("<br>", unsafe_allow_html=True) # Add spacing between sections
123
+
124
+ # 2. PDF Summarization
125
+ st.subheader("Upload PDF for Summarization")
126
+ pdf_file = st.file_uploader("Choose a PDF file...", type=["pdf"])
127
+
128
+ if pdf_file is not None:
129
+ pdf_reader = PdfReader(pdf_file)
130
+ text = ""
131
+ for page in pdf_reader.pages:
132
+ text += page.extract_text()
133
+
134
+ st.text_area("Extracted PDF Text", text[:1000]) # Show first 1000 chars
135
+
136
+ if st.button("πŸ“„ Summarize PDF", key="pdf_summary"):
137
+ summarized_text = summarize_text(text)
138
+ st.session_state.summarized_text = summarized_text # Save summarized text to session state
139
+
140
+ st.markdown("<br>", unsafe_allow_html=True) # Add spacing between sections
141
+
142
+ # 3. YouTube Summarization
143
+ st.subheader("Paste YouTube URL for Summarization")
144
+ youtube_url = st.text_input("Enter YouTube URL")
145
+
146
+ if youtube_url:
147
+ try:
148
+ video_id = youtube_url.split("v=")[1]
149
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
150
+ transcript_text = " ".join([entry['text'] for entry in transcript])
151
+
152
+ st.write("Extracted YouTube Transcript:")
153
+ st.text_area("Transcript", transcript_text[:1000]) # Display first 1000 chars
154
+
155
+ if st.button("πŸ“Ί Summarize YouTube", key="youtube_summary"):
156
+ summarized_text = summarize_text(transcript_text)
157
+ st.session_state.summarized_text = summarized_text # Save summarized text to session state
158
+
159
+ except Exception as e:
160
+ st.write(f"Error: {str(e)}")
161
+
162
+ # 4. Save Summarized Content as PDF
163
+ st.markdown("<br>", unsafe_allow_html=True) # Add spacing
164
+ st.header("πŸ’Ύ Save Summarized Content as PDF")
165
+ if st.button("Save as PDF") and 'summarized_text' in st.session_state:
166
+ pdf = FPDF()
167
+ pdf.add_page()
168
+ pdf.set_font("Arial", size=12)
169
+ pdf.multi_cell(0, 10, st.session_state.summarized_text)
170
+ pdf_output = "summarized_text.pdf"
171
+ pdf.output(pdf_output)
172
+ st.success(f"Saved as {pdf_output}")
173
+ elif 'summarized_text' not in st.session_state:
174
+ st.warning("No summarized content to save.")
175
+
176
+ # Summarized output and chat in the right column
177
  with col2:
178
+ st.header("πŸ“œ Summarized Output")
179
+ if 'summarized_text' in st.session_state:
180
+ st.text_area("Summarized Content", st.session_state.summarized_text, height=300) # Show the summarized text
181
+ else:
182
+ st.write("No summarized content available yet.")
183
+
184
+ # Chatbox for interacting with the summarized context
185
+ st.header("πŸ’¬ Chat with AI")
186
+ user_query = st.text_input("Ask your question about the summarized content:")
187
+
188
+ if user_query and 'summarized_text' in st.session_state:
189
+ combined_input = f"{st.session_state.summarized_text}\n\nUser: {user_query}"
190
+ response = summarize_text(combined_input)
191
+ st.write("AI Response:")
192
+ st.write(response)
193
+
194
+ def quiz_page():
195
+ st.title("πŸ“ Quiz on Summarized Content")
196
+
197
+ if 'summarized_text' not in st.session_state:
198
+ st.warning("Please summarize content first before taking the quiz.")
199
+ return
200
+
201
+ if 'quiz_questions' not in st.session_state:
202
+ with st.spinner("Generating quiz questions..."):
203
+ quiz_text = generate_quiz_questions(st.session_state.summarized_text)
204
+ st.session_state.quiz_questions = parse_quiz_questions(quiz_text)
205
+
206
+ if 'current_question' not in st.session_state:
207
+ st.session_state.current_question = 0
208
+ st.session_state.score = 0
209
+
210
+ if st.session_state.current_question < len(st.session_state.quiz_questions):
211
+ question = st.session_state.quiz_questions[st.session_state.current_question]
212
+ st.write(f"Question {st.session_state.current_question + 1} of {len(st.session_state.quiz_questions)}")
213
+ st.write(question['question'])
214
+
215
+ # Randomize the order of options
216
+ options = list(zip(['A', 'B', 'C', 'D'], question['options']))
217
+ random.shuffle(options)
218
+
219
+ answer = st.radio("Choose your answer:", options, format_func=lambda x: f"{x[0]}) {x[1]}")
220
+
221
+ if st.button("Submit Answer"):
222
+ if answer[0] == question['correct']:
223
+ st.success("Correct!")
224
+ st.session_state.score += 1
225
+ else:
226
+ st.error(f"Incorrect. The correct answer was {question['correct']}")
227
+ st.session_state.current_question += 1
228
+ st.rerun() # Updated from st.experimental_rerun()
229
+ else:
230
+ st.success(f"Quiz completed! Your score: {st.session_state.score}/{len(st.session_state.quiz_questions)}")
231
+ if st.button("Restart Quiz"):
232
+ del st.session_state['quiz_questions']
233
+ del st.session_state['current_question']
234
+ del st.session_state['score']
235
+ st.rerun() # Updated from st.experimental_rerun()
236
+
237
+ # Run the app
238
  if __name__ == "__main__":
239
  main()