File size: 6,390 Bytes
1b63e6c
44922b3
 
1b63e6c
 
 
44922b3
 
 
 
 
 
 
 
 
1b63e6c
 
2ec8f56
 
 
1b63e6c
44922b3
 
1b63e6c
44922b3
 
 
 
 
1b63e6c
44922b3
 
1b63e6c
44922b3
 
 
1b63e6c
2ec8f56
 
 
 
 
 
 
 
 
 
1b63e6c
2ec8f56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2660e3
44922b3
2ec8f56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44922b3
1b63e6c
 
44922b3
2ec8f56
44922b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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()