File size: 3,184 Bytes
5a56bb7
 
 
213f85f
36fe359
5a56bb7
 
8e9e651
36fe359
5a56bb7
 
8e9e651
 
 
 
 
 
5a56bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e9e651
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
import fitz  # PyMuPDF
from pptx import Presentation
from openai import OpenAI
import os

# === GPT-4 Client Setup ===
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
MAX_TOKENS = 7000  # Safety buffer below GPT-4's 8192 limit

# === Centralized LLM Caller ===
def call_llm(prompt, system_message="You are a helpful assistant.", temperature=0.7):
    # Rough token approximation: 1 token ≈ 4 characters (English average)
    approx_tokens = len(prompt) // 4
    if approx_tokens > MAX_TOKENS:
        prompt = prompt[:MAX_TOKENS * 4]  # truncate by character length
        prompt += "\n[NOTE: Truncated due to length limit.]"

    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": system_message},
            {"role": "user", "content": prompt}
        ],
        temperature=temperature
    )
    return response.choices[0].message.content

# === File Parsers ===
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


def extract_text_from_txt(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        return f.read()


def extract_text_from_md(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        return f.read()


def extract_text_from_pptx(file_path):
    text = ""
    prs = Presentation(file_path)
    for slide in prs.slides:
        for shape in slide.shapes:
            if hasattr(shape, "text"):
                text += shape.text + "\n"
    return text


def extract_text_from_file(file):
    if file is None:
        return ""

    name = file.name.lower()
    if name.endswith(".pdf"):
        return extract_text_from_pdf(file.name)
    elif name.endswith(".txt"):
        return extract_text_from_txt(file.name)
    elif name.endswith(".md"):
        return extract_text_from_md(file.name)
    elif name.endswith(".pptx"):
        return extract_text_from_pptx(file.name)
    else:
        return "Unsupported file type."


# === GPT-4 Powered Content Generators ===
def generate_summary(text):
    prompt = f"""Please summarize the following study material in a concise and organized manner:

{text}
"""
    return call_llm(prompt, system_message="You are a helpful study assistant.")


def generate_flashcards(text):
    prompt = f"""Based on the content below, create a set of helpful flashcards.
Each flashcard should be formatted as:
Q: Question?
A: Answer.

{text}
"""
    return call_llm(prompt, system_message="You are a flashcard generator assistant.")


def generate_quiz(text):
    prompt = f"""Create a short quiz (3-5 questions) based on the content below.
Include a mix of multiple choice and short answer questions.

{text}
"""
    return call_llm(prompt, system_message="You are a quiz generator assistant.")


def answer_question(text, question):
    prompt = f"""You are an AI tutor. Answer the following question based only on the content below.

Content:
{text}

Question:
{question}
"""
    return call_llm(prompt, system_message="You are a helpful and accurate tutor that stays grounded in provided content.")