Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Online learning has taken the front seat in the post-pandemic age. With the adve

EduAid is one such project currently available in the form of a browser extension.

## System Requirements

- **ffmpeg** or **libav** installed and available on your system `PATH`. The backend's `extract_text_from_audio()` method (in `backend/Generator/main.py`) uses `pydub` to convert MP3 to WAV, and `pydub` depends on these tools for audio transcoding.

## Installation and Setup

### 1. Clone the Repository
Expand Down
69 changes: 61 additions & 8 deletions backend/Generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import os
import fitz
import mammoth
import speech_recognition as sr
from pydub import AudioSegment
import uuid

class MCQGenerator:

Expand Down Expand Up @@ -367,20 +370,70 @@ def extract_text_from_docx(self, file_path):
result = mammoth.extract_raw_text(docx_file)
return result.value

def extract_text_from_audio(self, file_path):
wav_path = file_path
request_id = uuid.uuid4().hex
if file_path.endswith('.mp3'):
audio = AudioSegment.from_file(file_path, format='mp3')
wav_path = os.path.join(self.upload_folder, f"{request_id}.wav")
audio.export(wav_path, format='wav')

r = sr.Recognizer()
audio = AudioSegment.from_wav(wav_path)

chunk_length_ms = 60 * 1000
chunks = [
audio[i: i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)
]

full_text = []
chunk_files = []

try:
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
for i, chunk in enumerate(chunks):
chunk_filename = os.path.join(self.upload_folder, f"{request_id}_chunk_{i}.wav")
chunk_files.append(chunk_filename)
chunk.export(chunk_filename, format='wav')

with sr.AudioFile(chunk_filename) as source:
audio_data = r.record(source)
try:
text = r.recognize_google(audio_data)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
full_text.append(text)
except sr.UnknownValueError:
full_text.append("[Unintelligible]")
except sr.RequestError as e:
raise RuntimeError("Could not request results") from e
Comment thread
coderabbitai[bot] marked this conversation as resolved.
finally:
for chunk_file in chunk_files:
if os.path.exists(chunk_file):
os.remove(chunk_file)
if wav_path != file_path and os.path.exists(wav_path):
os.remove(wav_path)

return "\n".join(full_text)

def process_file(self, file):
file_path = os.path.join(self.upload_folder, file.filename)
file.save(file_path)
content = ""

if file.filename.endswith('.txt'):
with open(file_path, 'r') as f:
content = f.read()
elif file.filename.endswith('.pdf'):
content = self.extract_text_from_pdf(file_path)
elif file.filename.endswith('.docx'):
content = self.extract_text_from_docx(file_path)
try:
if file.filename.endswith('.txt'):
with open(file_path, 'r') as f:
content = f.read()
elif file.filename.endswith('.pdf'):
content = self.extract_text_from_pdf(file_path)
elif file.filename.endswith('.docx'):
content = self.extract_text_from_docx(file_path)
elif file.filename.endswith('.wav') or file.filename.endswith('.mp3'):
content = self.extract_text_from_audio(file_path)
else:
raise ValueError('Unsupported file format')
finally:
if os.path.exists(file_path):
os.remove(file_path)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

os.remove(file_path)
return content

class QuestionGenerator:
Expand Down
2 changes: 1 addition & 1 deletion eduaid_web/src/pages/Text_Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const Text_Input = () => {
{/* File Upload Section */}
<div className="w-full max-w-2xl mx-auto border-[3px] rounded-2xl text-center px-6 py-6 border-dotted border-[#3E5063] mt-6">
<img className="mx-auto mb-2" height={32} width={32} src={cloud} alt="cloud" />
<p className="text-white text-lg">Choose a file (PDF, MP3 supported)</p>
<p className="text-white text-lg">Choose a file (PDF, Audio supported)</p>

<input type="file" ref={fileInputRef} onChange={handleFileUpload} style={{ display: "none" }} />
<button
Expand Down
2 changes: 1 addition & 1 deletion extension/src/pages/text_input/TextInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function Second() {
<img className="mx-auto" height={24} width={24} src={cloud} alt="cloud" />
<div className="text-center text-white text-sm">Choose a file</div>
<div className="text-center text-white text-sm">
PDF, MP3 supported
PDF, Audio supported
</div>
<div>
<input
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ tokenizers
mammoth
mediawikiapi
PyMuPDF
textblob
textblob==0.19.0
SpeechRecognition==3.14.5
pydub==0.25.1
llama-cpp-python