File size: 8,907 Bytes
bedfa3f
 
 
acee15a
d5933bb
bedfa3f
d5933bb
b722113
 
bedfa3f
 
 
d5933bb
 
 
 
 
f62b780
d5933bb
 
 
 
 
 
 
bedfa3f
 
 
d5933bb
 
 
bedfa3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b722113
bedfa3f
b722113
f62b780
 
bedfa3f
 
 
d5933bb
f62b780
b722113
 
 
f62b780
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b722113
f62b780
 
b722113
 
 
bedfa3f
b722113
bedfa3f
b722113
d5933bb
b722113
d5933bb
b722113
d5933bb
bedfa3f
 
d5933bb
bedfa3f
 
d5933bb
bedfa3f
 
 
 
 
 
 
 
 
d5933bb
bedfa3f
 
b722113
bedfa3f
d5933bb
 
bedfa3f
f62b780
d5933bb
bedfa3f
 
f62b780
d5933bb
f62b780
bedfa3f
d5933bb
bedfa3f
 
d5933bb
bedfa3f
 
d5933bb
bedfa3f
 
 
 
 
 
 
 
 
d5933bb
bedfa3f
 
 
 
d5933bb
acee15a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5933bb
8fa31e3
 
 
 
 
 
bedfa3f
 
 
 
 
 
 
b722113
 
 
bedfa3f
 
 
 
 
 
f62b780
 
bedfa3f
 
f62b780
 
b722113
 
 
 
 
f62b780
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b722113
f62b780
 
 
b722113
 
 
8fa31e3
d5933bb
b722113
 
bedfa3f
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import logging
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import RequestValidationError
import openai
from pydub import AudioSegment
import tempfile

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("uvicorn.error")

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://studyscribe.framer.ai/"],  # Replace "*" with your frontend URL in production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

if not OPENAI_API_KEY:
    logger.error("OPENAI_API_KEY is not set.")
    raise Exception("OPENAI_API_KEY must be set as an environment variable.")

openai.api_key = OPENAI_API_KEY

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    logger.error(f"Validation error: {exc}")
    return JSONResponse(
        status_code=422,
        content={"detail": exc.errors(), "body": exc.body},
    )

@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
    logger.error(f"Unhandled exception: {exc}")
    return JSONResponse(status_code=500, content={"detail": "Internal Server Error"})

@app.middleware("http")
async def log_requests(request: Request, call_next):
    logger.info(f"Incoming request: {request.method} {request.url.path}")
    response = await call_next(request)
    return response

def transcribe_audio(audio_file_path):
    try:
        with open(audio_file_path, "rb") as audio_file:
            transcript = openai.Audio.transcribe("whisper-1", audio_file, response_format="verbose_json")
            return transcript
    except Exception as e:
        logger.error(f"Error in transcribe_audio: {e}")
        raise HTTPException(status_code=500, detail="Error during audio transcription.")

def split_audio_file(audio_file_path, max_chunk_size_mb=24):
    audio = AudioSegment.from_file(audio_file_path)
    duration_ms = len(audio)
    chunks = []
    start_ms = 0

    while start_ms < duration_ms:
        chunk_duration_ms = min(5 * 60 * 1000, duration_ms - start_ms)  # Start with 5 minutes
        chunk = audio[start_ms:start_ms + chunk_duration_ms]

        while True:
            with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_chunk_file:
                chunk.export(temp_chunk_file.name, format="wav")
                temp_chunk_file.flush()
                file_size_bytes = os.path.getsize(temp_chunk_file.name)
                file_size_mb = file_size_bytes / (1024 * 1024)
                temp_chunk_file.close()
                os.unlink(temp_chunk_file.name)

            if file_size_mb <= max_chunk_size_mb:
                # Chunk size is acceptable
                break
            else:
                # Reduce chunk duration
                if chunk_duration_ms <= 60 * 1000:
                    # Minimum chunk duration reached (1 minute), cannot reduce further
                    raise Exception("Cannot split audio into chunks small enough to meet the size limit.")
                chunk_duration_ms -= 60 * 1000  # Reduce by 1 minute
                chunk = audio[start_ms:start_ms + chunk_duration_ms]

        chunks.append(chunk)
        start_ms += chunk_duration_ms

    return chunks

def summarize_text(text, lesson_plan):
    try:
        system_prompt = "You are an assistant that summarizes text based on a lesson plan."
        user_prompt = f"""
        Text to summarize:

        "{text}"

        Based on the lesson plan below, summarize the key points discussed:

        Lesson Plan:
        {lesson_plan}

        Provide a concise summary with key takeaways.
        """

        response = openai.ChatCompletion.create(
            model='gpt-3.5-turbo',
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt}
            ],
            max_tokens=300,
            temperature=0.5,
        )

        return response['choices'][0]['message']['content'].strip()
    except Exception as e:
        logger.error(f"Error in summarize_text: {e}")
        raise HTTPException(status_code=500, detail="Error during summarization.")

def generate_lecture_notes(summaries, lesson_plan):
    try:
        summaries_text = "\n".join([f"At {item['timestamp']}: {item['summary']}" for item in summaries])

        system_prompt = "You are an assistant that generates detailed lecture notes based on summaries and a lesson plan."
        user_prompt = f"""
        Using the summarized text segments below and the lesson plan, create detailed lecture notes.

        Summarized Segments:
        {summaries_text}

        Lesson Plan:
        {lesson_plan}

        Provide comprehensive lecture notes in a structured format.
        """

        response = openai.ChatCompletion.create(
            model='gpt-3.5-turbo',
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt}
            ],
            max_tokens=2000,
            temperature=0.5,
        )

        return response['choices'][0]['message']['content'].strip()
    except Exception as e:
        logger.error(f"Error in generate_lecture_notes: {e}")
        raise HTTPException(status_code=500, detail="Error during lecture notes generation.")

@app.get("/", response_class=HTMLResponse)
def read_root():
    html_content = """
    <html>
    <head>
        <title>Lecture Notes Generator</title>
    </head>
    <body>
        <h1>Lecture Notes Generator</h1>
        <p>This is the backend API for the Lecture Notes Generator. Please use the /process endpoint to submit data.</p>
    </body>
    </html>
    """
    return HTMLResponse(content=html_content)

@app.post("/process")
async def process_files(
    audio_file: UploadFile = File(None),
    lecture_link: str = Form(None),
    lesson_plan: str = Form(...)
):
    try:
        if not audio_file and not lecture_link:
            raise HTTPException(status_code=400, detail="Please provide an audio file or a lecture link.")

        if not lesson_plan:
            raise HTTPException(status_code=400, detail="Lesson plan is required.")

        if audio_file:
            with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
                tmp_file.write(await audio_file.read())
                tmp_file_path = tmp_file.name
        elif lecture_link:
            logger.error("Processing lecture links is not implemented yet.")
            raise HTTPException(status_code=501, detail="Processing lecture links is not implemented yet.")
        else:
            raise HTTPException(status_code=400, detail="No valid audio input provided.")

        # Use the updated split_audio_file function
        audio_chunks = split_audio_file(tmp_file_path, max_chunk_size_mb=24)

        summarized_texts = []
        current_chunk_start_time = 0

        for index, chunk in enumerate(audio_chunks):
            with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as chunk_file:
                chunk.export(chunk_file.name, format="wav")
                chunk_file_path = chunk_file.name

            # Transcribe chunk
            transcript = transcribe_audio(chunk_file_path)
            segments = transcript.get('segments', [])

            for segment in segments:
                # Adjust the segment timestamps to account for the chunk's position in the full audio
                segment_start = segment['start'] + current_chunk_start_time
                segment_end = segment['end'] + current_chunk_start_time
                segment_text = segment['text']

                # Summarize the segment
                summary = summarize_text(segment_text, lesson_plan)

                summarized_texts.append({
                    'timestamp': f"{segment_start:.2f} - {segment_end:.2f}",
                    'summary': summary
                })

            # Update the chunk start time
            chunk_duration = len(chunk) / 1000.0  # duration in seconds
            current_chunk_start_time += chunk_duration

            os.unlink(chunk_file_path)

        lecture_notes = generate_lecture_notes(summarized_texts, lesson_plan)

        os.unlink(tmp_file_path)

        return JSONResponse(content={
            'summarized_texts': summarized_texts,
            'lecture_notes': lecture_notes
        })
    except HTTPException as e:
        logger.error(f"HTTPException in /process endpoint: {e.detail}")
        raise e
    except Exception as e:
        logger.error(f"Unhandled exception in /process endpoint: {e}")
        raise HTTPException(status_code=500, detail="Internal Server Error")