Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,24 @@ except KeyError:
|
|
| 15 |
logging.error("GROQ_API_KEY not found in environment variables")
|
| 16 |
raise EnvironmentError("Please set the GROQ_API_KEY environment variable")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def generate_tutor_output(subject: str, difficulty: str, student_input: str) -> str:
|
| 19 |
prompt = f"""
|
| 20 |
You are an expert tutor in {subject} at the {difficulty} level. The student has provided the following input: "{student_input}"
|
|
@@ -79,9 +97,11 @@ def create_interface() -> gr.Blocks:
|
|
| 79 |
label="Your Input",
|
| 80 |
info="Enter the topic you want to explore"
|
| 81 |
)
|
|
|
|
| 82 |
submit_button = gr.Button("📚 Generate Lesson", variant="primary")
|
| 83 |
|
| 84 |
with gr.Column(scale=3):
|
|
|
|
| 85 |
lesson_output = gr.Markdown(label="Lesson")
|
| 86 |
example_output = gr.Markdown(label="Example")
|
| 87 |
real_world_output = gr.Markdown(label="Real-World Application")
|
|
@@ -90,7 +110,7 @@ def create_interface() -> gr.Blocks:
|
|
| 90 |
### How to Use
|
| 91 |
1. Select a subject from the dropdown.
|
| 92 |
2. Choose your difficulty level.
|
| 93 |
-
3. Enter the topic or question you'd like to explore.
|
| 94 |
4. Click 'Generate Lesson' to receive a personalized lesson, example, and real-world application.
|
| 95 |
5. Review the AI-generated content to enhance your learning.
|
| 96 |
6. Feel free to ask follow-up questions or explore new topics!
|
|
@@ -98,10 +118,21 @@ def create_interface() -> gr.Blocks:
|
|
| 98 |
Remember: This is an AI-powered educational tool. Always verify important information with authoritative sources.
|
| 99 |
""")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
submit_button.click(
|
| 102 |
-
fn=
|
| 103 |
-
inputs=[subject, difficulty, student_input],
|
| 104 |
-
outputs=[lesson_output, example_output, real_world_output]
|
| 105 |
)
|
| 106 |
|
| 107 |
return demo
|
|
|
|
| 15 |
logging.error("GROQ_API_KEY not found in environment variables")
|
| 16 |
raise EnvironmentError("Please set the GROQ_API_KEY environment variable")
|
| 17 |
|
| 18 |
+
def transcribe_audio(audio_path):
|
| 19 |
+
try:
|
| 20 |
+
# Open and read the audio file
|
| 21 |
+
with open(audio_path, "rb") as audio_file:
|
| 22 |
+
audio_data = audio_file.read()
|
| 23 |
+
|
| 24 |
+
# Transcribe the audio using Distil-Whisper
|
| 25 |
+
transcription = client.audio.transcriptions.create(
|
| 26 |
+
file=(os.path.basename(audio_path), audio_data),
|
| 27 |
+
model="distil-whisper-large-v3-en",
|
| 28 |
+
response_format="verbose_json",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
return transcription.text
|
| 32 |
+
except Exception as e:
|
| 33 |
+
logging.error(f"Error in transcription: {str(e)}")
|
| 34 |
+
return f"Error in transcription: {str(e)}"
|
| 35 |
+
|
| 36 |
def generate_tutor_output(subject: str, difficulty: str, student_input: str) -> str:
|
| 37 |
prompt = f"""
|
| 38 |
You are an expert tutor in {subject} at the {difficulty} level. The student has provided the following input: "{student_input}"
|
|
|
|
| 97 |
label="Your Input",
|
| 98 |
info="Enter the topic you want to explore"
|
| 99 |
)
|
| 100 |
+
audio_input = gr.Audio(type="filepath", label="Or speak your question")
|
| 101 |
submit_button = gr.Button("📚 Generate Lesson", variant="primary")
|
| 102 |
|
| 103 |
with gr.Column(scale=3):
|
| 104 |
+
transcription_output = gr.Textbox(label="Transcribed Audio (if provided)")
|
| 105 |
lesson_output = gr.Markdown(label="Lesson")
|
| 106 |
example_output = gr.Markdown(label="Example")
|
| 107 |
real_world_output = gr.Markdown(label="Real-World Application")
|
|
|
|
| 110 |
### How to Use
|
| 111 |
1. Select a subject from the dropdown.
|
| 112 |
2. Choose your difficulty level.
|
| 113 |
+
3. Enter the topic or question you'd like to explore, or use the microphone to speak your question.
|
| 114 |
4. Click 'Generate Lesson' to receive a personalized lesson, example, and real-world application.
|
| 115 |
5. Review the AI-generated content to enhance your learning.
|
| 116 |
6. Feel free to ask follow-up questions or explore new topics!
|
|
|
|
| 118 |
Remember: This is an AI-powered educational tool. Always verify important information with authoritative sources.
|
| 119 |
""")
|
| 120 |
|
| 121 |
+
def process_input(subject, difficulty, text_input, audio_input):
|
| 122 |
+
if audio_input:
|
| 123 |
+
transcribed_text = transcribe_audio(audio_input)
|
| 124 |
+
student_input = transcribed_text
|
| 125 |
+
else:
|
| 126 |
+
student_input = text_input
|
| 127 |
+
transcribed_text = ""
|
| 128 |
+
|
| 129 |
+
lesson, example, real_world = process_output(generate_tutor_output(subject, difficulty, student_input))
|
| 130 |
+
return transcribed_text, lesson, example, real_world
|
| 131 |
+
|
| 132 |
submit_button.click(
|
| 133 |
+
fn=process_input,
|
| 134 |
+
inputs=[subject, difficulty, student_input, audio_input],
|
| 135 |
+
outputs=[transcription_output, lesson_output, example_output, real_world_output]
|
| 136 |
)
|
| 137 |
|
| 138 |
return demo
|