Ultralearning / app.py
DHEIVER's picture
Update app.py
2ec8f56 verified
raw
history blame
6.39 kB
import gradio as gr
import torch
from transformers import pipeline
class LearningPathGenerator:
def __init__(self):
self.device = 0 if torch.cuda.is_available() else -1
# Initialize models
self.transcriber = pipeline("automatic-speech-recognition",
model="openai/whisper-base",
device=self.device)
self.generator = pipeline("text-generation",
model="gpt2",
device=self.device)
def process_audio(self,
audio_path: str,
path_name: str,
difficulty: str = "intermediate",
include_resources: bool = True) -> tuple:
try:
# Transcribe audio
transcription = self.transcriber(audio_path)["text"]
# Generate learning path
prompt = f"""
Based on the following text, create a detailed learning path
for {difficulty} level:
{transcription}
Learning path:
"""
analysis = self.generator(prompt,
max_length=300,
num_return_sequences=1)[0]["generated_text"]
if include_resources:
resources = self._generate_resources()
analysis += "\n\n" + resources
return (
gr.Textbox.update(value=transcription, visible=True),
gr.Textbox.update(value=analysis, visible=True),
gr.Markdown.update(visible=True, value="βœ… Learning path generated successfully!"),
gr.Button.update(interactive=True)
)
except Exception as e:
return (
gr.Textbox.update(value=f"Error: {str(e)}", visible=True),
gr.Textbox.update(value="Could not generate analysis.", visible=True),
gr.Markdown.update(visible=True, value="❌ An error occurred"),
gr.Button.update(interactive=True)
)
def _generate_resources(self) -> str:
return """
πŸ“š Recommended Resources:
1. Books:
- "Essential Guide"
- "Advanced Techniques"
2. Online Courses:
- Coursera: "Topic Specialization"
- edX: "Advanced Course"
3. Practical Resources:
- Interactive tutorials
- Practice exercises
- Real-world projects
"""
def create_interface():
with gr.Blocks(theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="purple",
neutral_hue="gray"
)) as app:
gr.Markdown("""
# πŸŽ“ Learning Path Generator
Upload an audio file describing your learning goals and receive a personalized learning path!
""")
with gr.Row():
with gr.Column(scale=2):
# Input components
with gr.Group():
gr.Markdown("### πŸ“ Input")
audio_input = gr.Audio(
type="filepath",
label="Audio Input",
description="Record or upload an audio describing your goals",
sources=["microphone", "upload"],
)
with gr.Row():
path_name = gr.Textbox(
label="Path Name",
placeholder="Give your learning path a name",
scale=2
)
difficulty = gr.Dropdown(
choices=["beginner", "intermediate", "advanced"],
value="intermediate",
label="Difficulty Level",
scale=1
)
include_resources = gr.Checkbox(
label="Include Recommended Resources",
value=True,
info="Add curated learning resources to your path"
)
process_btn = gr.Button(
"πŸš€ Generate Learning Path",
variant="primary",
scale=2
)
# Output components
with gr.Column(scale=2):
with gr.Group():
gr.Markdown("### πŸ“Š Output")
status = gr.Markdown(visible=False)
with gr.Accordion("Audio Transcription", open=False):
transcription = gr.Textbox(
label="What we heard",
lines=4,
visible=False
)
analysis = gr.Textbox(
label="Your Learning Path",
lines=10,
visible=False
)
# Event handlers
process_btn.click(
fn=LearningPathGenerator().process_audio,
inputs=[audio_input, path_name, difficulty, include_resources],
outputs=[transcription, analysis, status, process_btn],
api_name="generate_path"
)
# Examples
gr.Examples(
examples=[
["path_audio.mp3", "Python Programming", "beginner", True],
["path_audio2.mp3", "Data Science", "intermediate", True],
],
inputs=[audio_input, path_name, difficulty, include_resources],
outputs=[transcription, analysis, status, process_btn],
cache_examples=True,
)
# Add additional info
gr.Markdown("""
### πŸ“Œ Tips
- Speak clearly and describe your learning goals in detail
- Mention any previous experience in the subject
- Include any specific areas you want to focus on
""")
return app
if __name__ == "__main__":
app = create_interface()
app.queue()
app.launch()