Pavaas commited on
Commit
b0e0069
·
verified ·
1 Parent(s): f3c32ac

Update config.py

Browse files
Files changed (1) hide show
  1. config.py +90 -47
config.py CHANGED
@@ -2,84 +2,127 @@ 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
 
 
 
 
 
 
2
  import pytesseract
3
  import easyocr
4
  import whisper
5
+ import tempfile
 
6
  import os
 
 
7
  import uuid
8
+ import genanki
9
+ import docx
10
+ import yt_dlp
11
+ import csv
12
+ from transformers import pipeline
13
+ from PIL import Image
14
 
15
+ # === Extract Text From Sources ===
16
 
17
+ def process_pdf(path):
 
18
  text = ""
19
+ doc = fitz.open(path)
20
  for page in doc:
21
+ t = page.get_text()
22
+ if t.strip():
23
+ text += t
24
+ else:
25
  pix = page.get_pixmap()
26
  img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
27
  text += pytesseract.image_to_string(img)
28
  return text
29
 
30
+ def process_image(path):
31
  reader = easyocr.Reader(['en'])
32
+ result = reader.readtext(path, detail=0)
33
  return "\n".join(result)
34
 
35
+ def process_audio(path):
36
  model = whisper.load_model("base")
37
+ result = model.transcribe(path)
38
  return result["text"]
39
 
40
+ def process_text(path):
41
+ if path.endswith(".txt"):
42
+ with open(path, "r", encoding="utf-8") as f:
43
+ return f.read()
44
+ elif path.endswith(".docx"):
45
+ doc = docx.Document(path)
46
+ return "\n".join([para.text for para in doc.paragraphs])
47
+ return ""
48
+
49
+ def process_youtube(url):
50
+ temp_dir = tempfile.gettempdir()
51
+ audio_path = os.path.join(temp_dir, f"{uuid.uuid4()}.mp3")
52
+ ydl_opts = {
53
+ 'format': 'bestaudio/best',
54
+ 'outtmpl': audio_path,
55
+ 'postprocessors': [{
56
+ 'key': 'FFmpegExtractAudio',
57
+ 'preferredcodec': 'mp3',
58
+ 'preferredquality': '192',
59
+ }],
60
+ 'quiet': True,
61
+ }
62
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
63
+ ydl.download([url])
64
+ return process_audio(audio_path)
65
 
66
+ # === Flashcard Generator ===
67
+
68
+ def generate_flashcards(text, model_name="t5-base", types=["Q&A"]):
69
+ generator = pipeline("text2text-generation", model=model_name, max_length=64)
70
+ chunks = [text[i:i + 400] for i in range(0, len(text), 400)]
71
+ cards = []
72
 
 
73
  for chunk in chunks:
74
+ if "Q&A" in types:
75
+ qa = generator(f"Generate a question and answer from:\n{chunk}")[0]['generated_text']
76
+ q, a = qa.split(":", 1) if ":" in qa else ("Question", qa)
77
+ cards.append({"question": q.strip(), "answer": a.strip(), "tag": "Q&A"})
78
+
79
+ if "Cloze" in types:
80
+ cloze = generator(f"Create a cloze deletion flashcard from:\n{chunk}")[0]['generated_text']
81
+ cards.append({"question": cloze.strip(), "answer": "[...]", "tag": "Cloze"})
82
+
83
+ if "MCQ" in types:
84
+ mcq = generator(f"Generate a multiple choice question from:\n{chunk}")[0]['generated_text']
85
+ cards.append({"question": mcq.strip(), "answer": "Choose best option", "tag": "MCQ"})
86
+
87
+ if "Reverse" in types:
88
+ qa = generator(f"Generate a question and answer from:\n{chunk}")[0]['generated_text']
89
+ q, a = qa.split(":", 1) if ":" in qa else ("Question", qa)
90
+ cards.append({"question": a.strip(), "answer": q.strip(), "tag": "Reverse"})
91
+
92
+ if len(cards) >= 20:
93
  break
 
94
 
95
+ return cards
96
+
97
+ # === Exporters ===
98
 
99
  def export_to_apkg(cards, deck_name):
100
  deck_id = int(str(uuid.uuid4().int)[:10])
101
  my_deck = genanki.Deck(deck_id, deck_name)
 
102
  model = genanki.Model(
103
  1607392319,
104
  'BatAnkiModel',
105
+ fields=[{'name': 'Question'}, {'name': 'Answer'}, {'name': 'Tag'}],
106
+ templates=[{
107
+ 'name': 'Card 1',
108
+ 'qfmt': '{{Question}}<br><i>Tag: {{Tag}}</i>',
109
+ 'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
110
+ }]
111
+ )
 
 
 
 
 
112
  for card in cards:
113
  my_deck.add_note(genanki.Note(
114
  model=model,
115
+ fields=[card['question'], card['answer'], card.get('tag', "")]
116
  ))
117
+ pkg_path = os.path.join(tempfile.gettempdir(), f"{deck_name}.apkg")
118
+ genanki.Package(my_deck).write_to_file(pkg_path)
119
+ return pkg_path
120
 
121
+ def export_to_csv(cards, deck_name):
122
+ path = os.path.join(tempfile.gettempdir(), f"{deck_name}.csv")
123
+ with open(path, "w", newline="", encoding="utf-8") as f:
124
+ writer = csv.writer(f)
125
+ writer.writerow(["Question", "Answer", "Tag"])
126
+ for card in cards:
127
+ writer.writerow([card["question"], card["answer"], card.get("tag", "")])
128
+ return path