Quran / app.py
Oranblock's picture
Create app.py
86bb974 verified
raw
history blame
3.38 kB
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()