Ultralearning / app.py
DHEIVER's picture
Update app.py
44922b3 verified
raw
history blame
2.32 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,
difficulty: str = "intermediate") -> dict:
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"]
return {
"transcription": transcription,
"analysis": analysis
}
except Exception as e:
return {
"transcription": f"Error: {str(e)}",
"analysis": "Could not generate analysis."
}
def create_interface():
app = gr.Interface(
fn=LearningPathGenerator().process_audio,
inputs=[
gr.Audio(type="filepath", label="Upload Audio"),
gr.Dropdown(
choices=["beginner", "intermediate", "advanced"],
value="intermediate",
label="Difficulty Level"
)
],
outputs=[
gr.Textbox(label="Audio Transcription"),
gr.Textbox(label="Learning Path")
],
title="πŸŽ“ Learning Path Generator",
description="Upload an audio file describing your learning goals and receive a personalized learning path!"
)
return app
if __name__ == "__main__":
app = create_interface()
app.launch()