Oranblock commited on
Commit
86bb974
1 Parent(s): 06c0996

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import json
4
+ from difflib import SequenceMatcher
5
+
6
+ # Load pre-trained speech-to-text model
7
+ recognizer = pipeline("automatic-speech-recognition")
8
+
9
+ # Load Qur'an verses from JSON file
10
+ with open('quran_verses.json', 'r', encoding='utf-8') as f:
11
+ quran_verses = json.load(f)["verses"]
12
+
13
+ # Load user progress
14
+ try:
15
+ with open('user_progress.json', 'r', encoding='utf-8') as f:
16
+ user_progress = json.load(f)
17
+ except FileNotFoundError:
18
+ user_progress = {"memorized_verses": []}
19
+
20
+ # Function to calculate the similarity between two texts
21
+ def calculate_similarity(a, b):
22
+ return SequenceMatcher(None, a, b).ratio()
23
+
24
+ # Function to update user progress
25
+ def update_progress(verse):
26
+ if verse not in user_progress["memorized_verses"]:
27
+ user_progress["memorized_verses"].append(verse)
28
+ with open('user_progress.json', 'w', encoding='utf-8') as f:
29
+ json.dump(user_progress, f, indent=4)
30
+
31
+ # Function to calculate progress percentage
32
+ def calculate_progress():
33
+ total_verses = len(quran_verses)
34
+ memorized_verses = len(user_progress["memorized_verses"])
35
+ return (memorized_verses / total_verses) * 100
36
+
37
+ # Function to provide detailed feedback based on similarity score
38
+ def get_feedback(similarity):
39
+ if similarity > 0.9:
40
+ return "Excellent! Your recitation is almost perfect!"
41
+ elif similarity > 0.75:
42
+ return "Good job! You’re getting close, but there’s room for improvement."
43
+ elif similarity > 0.5:
44
+ return "Not bad, but practice some more to improve accuracy."
45
+ else:
46
+ return "Keep practicing, and try again!"
47
+
48
+ # Function to process audio and match it with the closest Qur'an verse
49
+ def process_audio(audio):
50
+ transcription = recognizer(audio)["text"]
51
+
52
+ # Find the most similar verse
53
+ most_similar_verse = None
54
+ highest_similarity = 0
55
+ for verse in quran_verses:
56
+ similarity = calculate_similarity(transcription, verse["text"])
57
+ if similarity > highest_similarity:
58
+ highest_similarity = similarity
59
+ most_similar_verse = verse
60
+
61
+ # Update progress if the match is good enough
62
+ if most_similar_verse and highest_similarity > 0.8: # Threshold of 80% similarity
63
+ update_progress(most_similar_verse)
64
+ progress = calculate_progress()
65
+ feedback = get_feedback(highest_similarity)
66
+ return (
67
+ f"Transcription: {transcription}\n"
68
+ f"Closest verse: {most_similar_verse['text']}\n"
69
+ f"Similarity: {highest_similarity * 100:.2f}%\n"
70
+ f"Feedback: {feedback}\n"
71
+ f"Progress: {progress:.2f}%"
72
+ ), progress / 100 # Return progress as a decimal for the progress bar
73
+ else:
74
+ return (
75
+ f"Transcription: {transcription}\n"
76
+ f"No matching verse found or similarity too low.\n"
77
+ f"Progress: {calculate_progress():.2f}%"
78
+ ), calculate_progress() / 100 # Return progress as a decimal for the progress bar
79
+
80
+ # Interface
81
+ iface = gr.Interface(
82
+ fn=process_audio,
83
+ inputs=gr.Audio(source="microphone", type="filepath"),
84
+ outputs=[gr.Textbox(), gr.Progress(label="Memorization Progress")],
85
+ title="Qur'an Memorization Helper",
86
+ description="Speak a verse, and we'll transcribe it, check your accuracy, and track your progress."
87
+ )
88
+
89
+ # Launch the app
90
+ iface.launch()