File size: 3,384 Bytes
86bb974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import gradio as gr
from transformers import pipeline
import json
from difflib import SequenceMatcher

# Load pre-trained speech-to-text model
recognizer = pipeline("automatic-speech-recognition")

# Load Qur'an verses from JSON file
with open('quran_verses.json', 'r', encoding='utf-8') as f:
    quran_verses = json.load(f)["verses"]

# Load user progress
try:
    with open('user_progress.json', 'r', encoding='utf-8') as f:
        user_progress = json.load(f)
except FileNotFoundError:
    user_progress = {"memorized_verses": []}

# Function to calculate the similarity between two texts
def calculate_similarity(a, b):
    return SequenceMatcher(None, a, b).ratio()

# Function to update user progress
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)

# Function to calculate progress percentage
def calculate_progress():
    total_verses = len(quran_verses)
    memorized_verses = len(user_progress["memorized_verses"])
    return (memorized_verses / total_verses) * 100

# Function to provide detailed feedback based on similarity score
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!"

# Function to process audio and match it with the closest Qur'an verse
def process_audio(audio):
    transcription = recognizer(audio)["text"]
    
    # Find the most similar verse
    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

    # Update progress if the match is good enough
    if most_similar_verse and highest_similarity > 0.8:  # Threshold of 80% similarity
        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}%"
        ), progress / 100  # Return progress as a decimal for the progress bar
    else:
        return (
            f"Transcription: {transcription}\n"
            f"No matching verse found or similarity too low.\n"
            f"Progress: {calculate_progress():.2f}%"
        ), calculate_progress() / 100  # Return progress as a decimal for the progress bar

# Interface
iface = gr.Interface(
    fn=process_audio, 
    inputs=gr.Audio(source="microphone", type="filepath"), 
    outputs=[gr.Textbox(), gr.Progress(label="Memorization Progress")],
    title="Qur'an Memorization Helper",
    description="Speak a verse, and we'll transcribe it, check your accuracy, and track your progress."
)

# Launch the app
iface.launch()