File size: 1,422 Bytes
823bf5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import os
from youtube_transcript_api import YouTubeTranscriptApi
from dotenv import load_dotenv
import fitz  # PyMuPDF

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")


# ---------- PDF Summarization ----------
def extract_text_from_pdf(pdf_path):
    text = ""
    with fitz.open(pdf_path) as doc:
        for page in doc:
            text += page.get_text()
    return text


# ---------- YouTube Summarization ----------
def get_youtube_transcript(video_url):
    try:
        video_id = video_url.split("v=")[-1].split("&")[0]
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        full_text = " ".join([entry["text"] for entry in transcript])
        return full_text
    except Exception as e:
        return f"Error fetching transcript: {str(e)}"


# ---------- Summarization using GPT ----------
def summarize_text(text, engine="gpt-3.5-turbo"):
    try:
        response = openai.ChatCompletion.create(
            model=engine,
            messages=[
                {"role": "system", "content": "You are a helpful summarization assistant."},
                {"role": "user", "content": f"Summarize this:\n{text}"}
            ],
            temperature=0.5,
            max_tokens=500
        )
        summary = response['choices'][0]['message']['content']
        return summary
    except Exception as e:
        return f"Error during summarization: {str(e)}"