-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_utils.py
More file actions
57 lines (46 loc) · 1.62 KB
/
Copy pathai_utils.py
File metadata and controls
57 lines (46 loc) · 1.62 KB
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
# ai_utils.py
import os
from dotenv import load_dotenv
from openai import OpenAI
# Load environment variables from .env
load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
if not GEMINI_API_KEY:
raise ValueError("GEMINI_API_KEY is missing in .env file")
# Gemini OpenAI-compatible endpoint
client = OpenAI(
api_key=GEMINI_API_KEY,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)
MODEL_NAME = "gemini-2.0-flash-001" # You can upgrade to Gemini 3 later safely
def summarize_pdf_text(pdf_text: str) -> str:
"""
Send the extracted PDF text to Gemini and get a clean study summary.
"""
# Safety guard: avoid sending empty text
if not pdf_text or pdf_text.isspace():
return "No readable text found in the PDF."
system_prompt = (
"You are a helpful study assistant. "
"Summarize the given PDF content into clear, exam-focused notes. "
"Use short paragraphs and bullet points where helpful. "
"Ignore blank pages, headers, and footers."
)
# Truncate huge PDFs so the model doesn't overflow
max_chars = 12000
trimmed_text = pdf_text[:max_chars]
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": (
"Here is the PDF content. Summarize it into clean, helpful study notes:\n\n"
+ trimmed_text
),
},
],
temperature=0.3,
)
return response.choices[0].message.content.strip()