|
import gradio as gr |
|
from transformers import pipeline |
|
import json |
|
from difflib import SequenceMatcher |
|
|
|
|
|
recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-large-xlsr-53-arabic") |
|
|
|
|
|
with open('quran_verses.json', 'r', encoding='utf-8') as f: |
|
quran_verses = json.load(f)["verses"] |
|
|
|
|
|
try: |
|
with open('user_progress.json', 'r', encoding='utf-8') as f: |
|
user_progress = json.load(f) |
|
except FileNotFoundError: |
|
user_progress = {"memorized_verses": []} |
|
|
|
|
|
def calculate_similarity(a, b): |
|
return SequenceMatcher(None, a, b).ratio() |
|
|
|
|
|
def update_progress(verse): |
|
if verse not in user_progress["memorized_verses"]: |
|
user_progress["memorized_verses"].append(verse) |
|
with open('user_progress.json', 'w', encoding='utf-8') as f: |
|
json.dump(user_progress, f, indent=4) |
|
|
|
|
|
def calculate_progress(): |
|
total_verses = len(quran_verses) |
|
memorized_verses = len(user_progress["memorized_verses"]) |
|
return (memorized_verses / total_verses) * 100 |
|
|
|
|
|
def get_feedback(similarity): |
|
if similarity > 0.9: |
|
return "Excellent! Your recitation is almost perfect!" |
|
elif similarity > 0.75: |
|
return "Good job! You’re getting close, but there’s room for improvement." |
|
elif similarity > 0.5: |
|
return "Not bad, but practice some more to improve accuracy." |
|
else: |
|
return "Keep practicing, and try again!" |
|
|
|
|
|
def process_audio(audio): |
|
print(f"Audio Input: {audio}") |
|
|
|
|
|
if audio is None: |
|
return "No audio file provided. Please upload or record an audio." |
|
|
|
|
|
transcription = recognizer(audio)["text"] |
|
|
|
|
|
most_similar_verse = None |
|
highest_similarity = 0 |
|
for verse in quran_verses: |
|
similarity = calculate_similarity(transcription, verse["text"]) |
|
if similarity > highest_similarity: |
|
highest_similarity = similarity |
|
most_similar_verse = verse |
|
|
|
|
|
if most_similar_verse and highest_similarity > 0.8: |
|
update_progress(most_similar_verse) |
|
progress = calculate_progress() |
|
feedback = get_feedback(highest_similarity) |
|
return ( |
|
f"Transcription: {transcription}\n" |
|
f"Closest verse: {most_similar_verse['text']}\n" |
|
f"Similarity: {highest_similarity * 100:.2f}%\n" |
|
f"Feedback: {feedback}\n" |
|
f"Progress: {progress:.2f}%" |
|
) |
|
else: |
|
return ( |
|
f"Transcription: {transcription}\n" |
|
f"No matching verse found or similarity too low.\n" |
|
f"Progress: {calculate_progress():.2f}%" |
|
) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_audio, |
|
inputs=gr.Audio(type="filepath"), |
|
outputs=gr.Textbox(), |
|
title="Qur'an Memorization Helper", |
|
description="Speak a verse in Arabic, and we'll transcribe it, check your accuracy, and track your progress." |
|
) |
|
|
|
|
|
iface.launch() |