Update utils/summarizer.py
Browse files- utils/summarizer.py +10 -41
utils/summarizer.py
CHANGED
|
@@ -1,46 +1,15 @@
|
|
| 1 |
-
import openai
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
import fitz # PyMuPDF
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
text = ""
|
| 14 |
-
with fitz.open(pdf_path) as doc:
|
| 15 |
-
for page in doc:
|
| 16 |
-
text += page.get_text()
|
| 17 |
-
return text
|
| 18 |
|
| 19 |
-
|
| 20 |
-
# ---------- YouTube Summarization ----------
|
| 21 |
-
def get_youtube_transcript(video_url):
|
| 22 |
-
try:
|
| 23 |
-
video_id = video_url.split("v=")[-1].split("&")[0]
|
| 24 |
-
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 25 |
-
full_text = " ".join([entry["text"] for entry in transcript])
|
| 26 |
-
return full_text
|
| 27 |
-
except Exception as e:
|
| 28 |
-
return f"Error fetching transcript: {str(e)}"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# ---------- Summarization using GPT ----------
|
| 32 |
-
def summarize_text(text, engine="gpt-3.5-turbo"):
|
| 33 |
-
try:
|
| 34 |
-
response = openai.ChatCompletion.create(
|
| 35 |
-
model=engine,
|
| 36 |
-
messages=[
|
| 37 |
-
{"role": "system", "content": "You are a helpful summarization assistant."},
|
| 38 |
-
{"role": "user", "content": f"Summarize this:\n{text}"}
|
| 39 |
-
],
|
| 40 |
-
temperature=0.5,
|
| 41 |
-
max_tokens=500
|
| 42 |
-
)
|
| 43 |
-
summary = response['choices'][0]['message']['content']
|
| 44 |
-
return summary
|
| 45 |
-
except Exception as e:
|
| 46 |
-
return f"Error during summarization: {str(e)}"
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
| 6 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 7 |
|
| 8 |
+
def summarize_text(text):
|
| 9 |
+
payload = {"inputs": text}
|
| 10 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 11 |
|
| 12 |
+
if response.status_code != 200:
|
| 13 |
+
return f"❌ Error: {response.status_code} - {response.json()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
return response.json()[0]["summary_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|