Update file_utils.py
Browse files- file_utils.py +72 -0
file_utils.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
import fitz # PyMuPDF
|
| 2 |
from pptx import Presentation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def extract_text_from_pdf(pdf_path):
|
|
@@ -45,3 +49,71 @@ def extract_text_from_file(file):
|
|
| 45 |
return extract_text_from_pptx(file.name)
|
| 46 |
else:
|
| 47 |
return "Unsupported file type."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import fitz # PyMuPDF
|
| 2 |
from pptx import Presentation
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
|
| 9 |
def extract_text_from_pdf(pdf_path):
|
|
|
|
| 49 |
return extract_text_from_pptx(file.name)
|
| 50 |
else:
|
| 51 |
return "Unsupported file type."
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def generate_summary(text):
|
| 55 |
+
prompt = f"""Please summarize the following study material in a concise and organized manner:
|
| 56 |
+
|
| 57 |
+
{text}
|
| 58 |
+
"""
|
| 59 |
+
response = client.chat.completions.create(
|
| 60 |
+
model="gpt-4",
|
| 61 |
+
messages=[
|
| 62 |
+
{"role": "system", "content": "You are a helpful study assistant."},
|
| 63 |
+
{"role": "user", "content": prompt}
|
| 64 |
+
]
|
| 65 |
+
)
|
| 66 |
+
return response.choices[0].message.content
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def generate_flashcards(text):
|
| 70 |
+
prompt = f"""Based on the content below, create a set of helpful flashcards.
|
| 71 |
+
Each flashcard should be formatted as:
|
| 72 |
+
Q: Question?
|
| 73 |
+
A: Answer.
|
| 74 |
+
|
| 75 |
+
{text}
|
| 76 |
+
"""
|
| 77 |
+
response = client.chat.completions.create(
|
| 78 |
+
model="gpt-4",
|
| 79 |
+
messages=[
|
| 80 |
+
{"role": "system", "content": "You are a flashcard generator assistant."},
|
| 81 |
+
{"role": "user", "content": prompt}
|
| 82 |
+
]
|
| 83 |
+
)
|
| 84 |
+
return response.choices[0].message.content
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def generate_quiz(text):
|
| 88 |
+
prompt = f"""Create a short quiz (3-5 questions) based on the content below.
|
| 89 |
+
Include a mix of multiple choice and short answer questions.
|
| 90 |
+
|
| 91 |
+
{text}
|
| 92 |
+
"""
|
| 93 |
+
response = client.chat.completions.create(
|
| 94 |
+
model="gpt-4",
|
| 95 |
+
messages=[
|
| 96 |
+
{"role": "system", "content": "You are a quiz generator assistant."},
|
| 97 |
+
{"role": "user", "content": prompt}
|
| 98 |
+
]
|
| 99 |
+
)
|
| 100 |
+
return response.choices[0].message.content
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def answer_question(text, question):
|
| 104 |
+
prompt = f"""You are an AI tutor. Answer the following question based only on the content below.
|
| 105 |
+
|
| 106 |
+
Content:
|
| 107 |
+
{text}
|
| 108 |
+
|
| 109 |
+
Question:
|
| 110 |
+
{question}
|
| 111 |
+
"""
|
| 112 |
+
response = client.chat.completions.create(
|
| 113 |
+
model="gpt-4",
|
| 114 |
+
messages=[
|
| 115 |
+
{"role": "system", "content": "You are a helpful and accurate tutor that stays grounded in provided content."},
|
| 116 |
+
{"role": "user", "content": prompt}
|
| 117 |
+
]
|
| 118 |
+
)
|
| 119 |
+
return response.choices[0].message.content
|