Upload 5 files
Browse files- utils/flashcards.py +12 -0
- utils/memory_manager.py +21 -0
- utils/ocr.py +7 -0
- utils/pdf_parser.py +8 -0
- utils/quiz_generator.py +12 -0
utils/flashcards.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
|
| 6 |
+
def generate_flashcards(text, num_cards=5):
|
| 7 |
+
prompt = f"Generate {num_cards} flashcards in Q&A format from:\n{text}"
|
| 8 |
+
response = openai.ChatCompletion.create(
|
| 9 |
+
model="gpt-4",
|
| 10 |
+
messages=[{"role":"user","content":prompt}]
|
| 11 |
+
)
|
| 12 |
+
return response.choices[0].message["content"]
|
utils/memory_manager.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
import chromadb
|
| 3 |
+
|
| 4 |
+
# Initialize embedding model
|
| 5 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 6 |
+
|
| 7 |
+
# Initialize ChromaDB
|
| 8 |
+
client = chromadb.Client()
|
| 9 |
+
collection = client.create_collection("memory")
|
| 10 |
+
|
| 11 |
+
def save_to_memory(text, metadata=None):
|
| 12 |
+
embedding = embedding_model.encode(text).tolist()
|
| 13 |
+
collection.add(documents=[text], embeddings=[embedding], metadatas=[metadata or {}])
|
| 14 |
+
|
| 15 |
+
def retrieve_memory(query, n_results=3):
|
| 16 |
+
embedding = embedding_model.encode(query).tolist()
|
| 17 |
+
results = collection.query(query_embeddings=[embedding], n_results=n_results)
|
| 18 |
+
return results
|
| 19 |
+
|
| 20 |
+
def delete_memory(ids):
|
| 21 |
+
collection.delete(ids=ids)
|
utils/ocr.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytesseract
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
def extract_text_from_image(image_path):
|
| 5 |
+
img = cv2.imread(image_path)
|
| 6 |
+
text = pytesseract.image_to_string(img)
|
| 7 |
+
return text.strip()
|
utils/pdf_parser.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
|
| 3 |
+
def extract_text_from_pdf(file_path):
|
| 4 |
+
text = ""
|
| 5 |
+
with fitz.open(file_path) as pdf:
|
| 6 |
+
for page in pdf:
|
| 7 |
+
text += page.get_text()
|
| 8 |
+
return text.strip()
|
utils/quiz_generator.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
|
| 6 |
+
def generate_quiz(text, num_questions=5):
|
| 7 |
+
prompt = f"Generate {num_questions} multiple choice questions from this content:\n{text}"
|
| 8 |
+
response = openai.ChatCompletion.create(
|
| 9 |
+
model="gpt-4",
|
| 10 |
+
messages=[{"role":"user","content":prompt}]
|
| 11 |
+
)
|
| 12 |
+
return response.choices[0].message["content"]
|