sultan-hassan commited on
Commit
fa20980
·
verified ·
1 Parent(s): 5da153c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import elevenlabs
4
+ from speech_recognition import Recognizer, AudioFile
5
+ import io
6
+ import google.generativeai as genai
7
+
8
+ # --- API Keys (Set as environment variables for security!) ---
9
+ genai.configure(api_key=os.environ.get("GOOGLE_API_KEY")) # Gemini API Key
10
+ elevenlabs_api_key = os.environ.get("ELEVENLABS_API_KEY")
11
+
12
+ elevenlabs.set_api_key(elevenlabs_api_key)
13
+
14
+ # --- ElevenLabs Voice ---
15
+ voice = "Bella" # Choose a voice from ElevenLabs
16
+
17
+ # --- Language Tutor Parameters ---
18
+ target_language = "Arabic"
19
+ difficulty = 1 # 1 = Easy, 2 = Medium, 3 = Hard
20
+
21
+ # --- Gemini Model ---
22
+ model = genai.GenerativeModel('gemini-pro') # Or 'gemini-pro-vision' if you need image input
23
+
24
+ # --- Functions ---
25
+
26
+ def generate_question(difficulty):
27
+ """Generates a Arabic question based on difficulty using Gemini."""
28
+ prompt = f"Generate a simple {target_language} question for a language learner at difficulty level {difficulty}. Just the question, no extra text."
29
+ response = model.generate_content(prompt)
30
+ return response.text.strip()
31
+
32
+ def evaluate_answer(question, answer):
33
+ """Evaluates the user's answer using Gemini."""
34
+ prompt = f"You are a Arabic language tutor. Evaluate the following answer to the question: '{question}'. Answer: '{answer}'. Provide feedback on grammar, vocabulary, and fluency. Keep the feedback concise (under 50 words). Also, give a score from 1-5 (1 being very poor, 5 being excellent)."
35
+ response = model.generate_content(prompt)
36
+ return response.text.strip()
37
+
38
+ def text_to_speech(text, voice):
39
+ """Converts text to speech using ElevenLabs."""
40
+ audio = elevenlabs.generate(text=text, voice=voice, model="eleven_monolingual_v1")
41
+ return audio
42
+
43
+ def transcribe_audio(audio_file):
44
+ """Transcribes audio using SpeechRecognition."""
45
+ r = Recognizer()
46
+ with AudioFile(audio_file) as source:
47
+ audio = r.record(source)
48
+ try:
49
+ text = r.recognize_google(audio, language=target_language) # You might need to adjust the language code
50
+ return text
51
+ except Exception as e:
52
+ return f"Error transcribing audio: {e}"
53
+
54
+ def run_tutor(audio_file):
55
+ """Main function to run the tutor."""
56
+ question = generate_question(difficulty)
57
+ question_audio = text_to_speech(question, voice)
58
+
59
+ # Transcribe the user's answer
60
+ user_answer = transcribe_audio(audio_file)
61
+
62
+ # Evaluate the answer
63
+ feedback = evaluate_answer(question, user_answer)
64
+ feedback_audio = text_to_speech(feedback, voice)
65
+
66
+ return question_audio, feedback_audio, question, user_answer, feedback
67
+
68
+ # --- Gradio Interface ---
69
+ with gr.Blocks() as demo:
70
+ gr.Markdown("# Adaptive Language Tutor (Spanish)")
71
+ with gr.Row():
72
+ question_audio_output = gr.Audio(label="Question")
73
+ feedback_audio_output = gr.Audio(label="Feedback")
74
+ with gr.Row():
75
+ question_text_output = gr.Textbox(label="Question Text")
76
+ answer_text_output = gr.Textbox(label="Your Answer")
77
+ feedback_text_output = gr.Textbox(label="Feedback")
78
+ mic_input = gr.Audio(source="microphone", type="filepath", label="Speak Your Answer")
79
+
80
+ mic_input.change(
81
+ fn=run_tutor,
82
+ inputs=mic_input,
83
+ outputs=[question_audio_output, feedback_audio_output, question_text_output, answer_text_output, feedback_text_output]
84
+ )
85
+
86
+ demo.launch()