Create config.py
Browse files
config.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import pytesseract
|
| 3 |
+
import easyocr
|
| 4 |
+
import whisper
|
| 5 |
+
import speech_recognition as sr
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import os
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import genanki
|
| 10 |
+
import uuid
|
| 11 |
+
import tempfile
|
| 12 |
+
import shutil
|
| 13 |
+
|
| 14 |
+
# === OCR and Text Extraction ===
|
| 15 |
+
|
| 16 |
+
def process_pdf(file_path):
|
| 17 |
+
doc = fitz.open(file_path)
|
| 18 |
+
text = ""
|
| 19 |
+
for page in doc:
|
| 20 |
+
text += page.get_text()
|
| 21 |
+
if not text.strip(): # fallback to OCR
|
| 22 |
+
pix = page.get_pixmap()
|
| 23 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 24 |
+
text += pytesseract.image_to_string(img)
|
| 25 |
+
return text
|
| 26 |
+
|
| 27 |
+
def process_image(file_path):
|
| 28 |
+
reader = easyocr.Reader(['en'])
|
| 29 |
+
result = reader.readtext(file_path, detail=0)
|
| 30 |
+
return "\n".join(result)
|
| 31 |
+
|
| 32 |
+
def process_audio(file_path):
|
| 33 |
+
model = whisper.load_model("base")
|
| 34 |
+
result = model.transcribe(file_path)
|
| 35 |
+
return result["text"]
|
| 36 |
+
|
| 37 |
+
# === AI Flashcard Generation ===
|
| 38 |
+
|
| 39 |
+
def generate_flashcards(text):
|
| 40 |
+
generator = pipeline("text2text-generation", model="t5-base", max_length=64)
|
| 41 |
+
flashcards = []
|
| 42 |
+
|
| 43 |
+
chunks = [text[i:i+400] for i in range(0, len(text), 400)]
|
| 44 |
+
for chunk in chunks:
|
| 45 |
+
prompt = f"Generate a flashcard question and answer from this medical text:\n{chunk}"
|
| 46 |
+
output = generator(prompt)[0]['generated_text']
|
| 47 |
+
if ":" in output:
|
| 48 |
+
q, a = output.split(":", 1)
|
| 49 |
+
else:
|
| 50 |
+
q, a = "Question", output
|
| 51 |
+
flashcards.append({"question": q.strip(), "answer": a.strip()})
|
| 52 |
+
if len(flashcards) >= 15:
|
| 53 |
+
break
|
| 54 |
+
return flashcards
|
| 55 |
+
|
| 56 |
+
# === Export to Anki (.apkg) ===
|
| 57 |
+
|
| 58 |
+
def export_to_apkg(cards, deck_name):
|
| 59 |
+
deck_id = int(str(uuid.uuid4().int)[:10])
|
| 60 |
+
my_deck = genanki.Deck(deck_id, deck_name)
|
| 61 |
+
|
| 62 |
+
model = genanki.Model(
|
| 63 |
+
1607392319,
|
| 64 |
+
'BatAnkiModel',
|
| 65 |
+
fields=[
|
| 66 |
+
{'name': 'Question'},
|
| 67 |
+
{'name': 'Answer'},
|
| 68 |
+
],
|
| 69 |
+
templates=[
|
| 70 |
+
{
|
| 71 |
+
'name': 'Card 1',
|
| 72 |
+
'qfmt': '{{Question}}',
|
| 73 |
+
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
|
| 74 |
+
},
|
| 75 |
+
])
|
| 76 |
+
|
| 77 |
+
for card in cards:
|
| 78 |
+
my_deck.add_note(genanki.Note(
|
| 79 |
+
model=model,
|
| 80 |
+
fields=[card['question'], card['answer']]
|
| 81 |
+
))
|
| 82 |
+
|
| 83 |
+
package_path = os.path.join(tempfile.gettempdir(), f"{deck_name}.apkg")
|
| 84 |
+
genanki.Package(my_deck).write_to_file(package_path)
|
| 85 |
+
return package_path
|