DHEIVER commited on
Commit
44922b3
·
verified ·
1 Parent(s): feadd0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -278
app.py CHANGED
@@ -1,297 +1,68 @@
1
- # config.py
2
- import os
3
- from dotenv import load_dotenv
4
-
5
- load_dotenv()
6
-
7
- class Config:
8
- DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'
9
- LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO')
10
- MODELS_CACHE_DIR = os.getenv('MODELS_CACHE_DIR', './models')
11
- HISTORY_FILE = os.getenv('HISTORY_FILE', 'learning_path_history.json')
12
- MAX_AUDIO_LENGTH = int(os.getenv('MAX_AUDIO_LENGTH', '600')) # seconds
13
- MAX_TEXT_LENGTH = int(os.getenv('MAX_TEXT_LENGTH', '1000'))
14
- SUPPORTED_AUDIO_FORMATS = ['.wav', '.mp3', '.ogg', '.flac']
15
-
16
- # Visualization settings
17
- MAX_TOPICS = int(os.getenv('MAX_TOPICS', '10'))
18
- MAX_SUBTOPICS = int(os.getenv('MAX_SUBTOPICS', '5'))
19
- FIGURE_DPI = int(os.getenv('FIGURE_DPI', '300'))
20
-
21
- # Model settings
22
- MODEL_TRANSCRIBER = os.getenv('MODEL_TRANSCRIBER', 'openai/whisper-base')
23
- MODEL_GENERATOR = os.getenv('MODEL_GENERATOR', 'gpt2')
24
-
25
- # Retry settings
26
- MAX_RETRIES = int(os.getenv('MAX_RETRIES', '3'))
27
- RETRY_DELAY = int(os.getenv('RETRY_DELAY', '1'))
28
-
29
- # utils.py
30
- import logging
31
- import json
32
- from typing import Dict, Any, Optional, List, Tuple
33
- import os
34
- from datetime import datetime
35
- from config import Config
36
-
37
- class Utils:
38
- @staticmethod
39
- def setup_logging() -> logging.Logger:
40
- logger = logging.getLogger("LearningPathGenerator")
41
- level = getattr(logging, Config.LOG_LEVEL)
42
- logger.setLevel(level)
43
-
44
- handler = logging.FileHandler("app.log")
45
- formatter = logging.Formatter(
46
- '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
47
- )
48
- handler.setFormatter(formatter)
49
- logger.addHandler(handler)
50
-
51
- return logger
52
-
53
- @staticmethod
54
- def save_json(data: Dict[str, Any], filename: str) -> bool:
55
- try:
56
- with open(filename, 'w', encoding='utf-8') as f:
57
- json.dump(data, f, ensure_ascii=False, indent=2)
58
- return True
59
- except Exception as e:
60
- logging.error(f"Error saving JSON: {str(e)}")
61
- return False
62
-
63
- @staticmethod
64
- def load_json(filename: str) -> Optional[Dict[str, Any]]:
65
- try:
66
- with open(filename, 'r', encoding='utf-8') as f:
67
- return json.load(f)
68
- except Exception as e:
69
- logging.error(f"Error loading JSON: {str(e)}")
70
- return None
71
-
72
- @staticmethod
73
- def extract_topics(analysis: str) -> Tuple[List[str], Dict[str, List[str]]]:
74
- # Simple topic extraction logic - could be enhanced
75
- topics = ["Main Topic", "Subtopic 1", "Subtopic 2"]
76
- subtopics = {
77
- "Main Topic": ["Detail 1", "Detail 2"],
78
- "Subtopic 1": ["Point 1", "Point 2"],
79
- "Subtopic 2": ["Item 1", "Item 2"]
80
- }
81
- return topics, subtopics
82
-
83
- # models.py
84
- from transformers import pipeline
85
- import torch
86
- from typing import Dict, Any
87
- import logging
88
- from config import Config
89
-
90
- class ModelManager:
91
- def __init__(self):
92
- self.logger = logging.getLogger("ModelManager")
93
- self.models: Dict[str, Any] = {}
94
- self._initialize_models()
95
-
96
- def _initialize_models(self):
97
- try:
98
- device = 0 if torch.cuda.is_available() else -1
99
-
100
- self.models["transcriber"] = pipeline(
101
- "automatic-speech-recognition",
102
- model=Config.MODEL_TRANSCRIBER,
103
- device=device
104
- )
105
-
106
- self.models["generator"] = pipeline(
107
- "text-generation",
108
- model=Config.MODEL_GENERATOR,
109
- device=device
110
- )
111
-
112
- except Exception as e:
113
- self.logger.error(f"Error initializing models: {str(e)}")
114
- raise
115
-
116
- def get_model(self, name: str) -> Any:
117
- return self.models.get(name)
118
-
119
- # main.py
120
  import gradio as gr
121
- from typing import Dict, Any
122
- import logging
123
- from config import Config
124
- from utils import Utils
125
- from models import ModelManager
126
- from visualization import Visualizer
127
- from datetime import datetime
128
 
129
  class LearningPathGenerator:
130
  def __init__(self):
131
- self.logger = Utils.setup_logging()
132
- self.model_manager = ModelManager()
133
- self.history_file = Config.HISTORY_FILE
134
-
135
- if not os.path.exists(self.history_file):
136
- Utils.save_json([], self.history_file)
137
-
 
 
138
  def process_audio(self,
139
  audio_path: str,
140
- path_name: str = "",
141
- difficulty: str = "intermediate",
142
- include_resources: bool = True) -> Dict[str, Any]:
143
  try:
144
- transcriber = self.model_manager.get_model("transcriber")
145
- transcription = transcriber(audio_path)["text"]
146
 
147
- generator = self.model_manager.get_model("generator")
148
- analysis = self._generate_analysis(generator, transcription, difficulty, include_resources)
 
 
 
149
 
150
- topics, subtopics = Utils.extract_topics(analysis)
151
- mind_map = Visualizer.create_mind_map(topics, subtopics)
152
 
153
- if path_name:
154
- self._save_to_history(transcription, analysis, path_name)
 
155
 
156
  return {
157
  "transcription": transcription,
158
- "analysis": analysis,
159
- "mind_map": mind_map
160
  }
161
-
162
  except Exception as e:
163
- self.logger.error(f"Processing error: {str(e)}")
164
- return self._error_response(str(e))
165
-
166
- def _generate_analysis(self,
167
- generator: Any,
168
- text: str,
169
- difficulty: str,
170
- include_resources: bool) -> str:
171
- prompt = f"""
172
- Based on the following text, create a detailed learning path
173
- for {difficulty} level:
174
- {text[:Config.MAX_TEXT_LENGTH]}
175
-
176
- Learning path:
177
- """
178
-
179
- response = generator(
180
- prompt,
181
- max_length=300,
182
- num_return_sequences=1
183
- )[0]["generated_text"]
184
-
185
- if include_resources:
186
- response += self._generate_resources()
187
-
188
- return response
189
-
190
- def _generate_resources(self) -> str:
191
- return """
192
- Recommended Resources:
193
-
194
- 1. Books:
195
- - "Essential Guide"
196
- - "Advanced Techniques"
197
-
198
- 2. Online Courses:
199
- - Coursera: "Topic Specialization"
200
- - edX: "Advanced Course"
201
-
202
- 3. Practical Resources:
203
- - Interactive tutorials
204
- - Practice exercises
205
- - Real-world projects
206
- """
207
-
208
- def _save_to_history(self, transcription: str, analysis: str, path_name: str):
209
- history = Utils.load_json(self.history_file) or []
210
- history.append({
211
- "date": datetime.now().isoformat(),
212
- "name": path_name,
213
- "transcription": transcription,
214
- "analysis": analysis
215
- })
216
- Utils.save_json(history, self.history_file)
217
-
218
- def _error_response(self, error_msg: str) -> Dict[str, Any]:
219
- return {
220
- "transcription": f"Error: {error_msg}",
221
- "analysis": "Could not generate analysis due to an error.",
222
- "mind_map": None
223
- }
224
 
225
- def create_interface(self):
226
- with gr.Blocks(theme=gr.themes.Soft()) as app:
227
- gr.Markdown("""
228
- # 🎓 Learning Path Generator
229
-
230
- Upload an audio file describing your learning goals
231
- and receive a personalized learning path with resources!
232
- """)
233
-
234
- with gr.Tab("Generate Path"):
235
- with gr.Row():
236
- with gr.Column(scale=2):
237
- audio_input = gr.Audio(
238
- type="filepath",
239
- label="Audio Upload",
240
- description="Record or upload an audio describing your goals"
241
- )
242
-
243
- with gr.Row():
244
- path_name = gr.Textbox(
245
- label="Path Name",
246
- placeholder="Give your learning path a name (optional)"
247
- )
248
- difficulty = gr.Dropdown(
249
- choices=["beginner", "intermediate", "advanced"],
250
- value="intermediate",
251
- label="Difficulty Level"
252
- )
253
-
254
- include_resources = gr.Checkbox(
255
- label="Include Recommended Resources",
256
- value=True
257
- )
258
-
259
- process_btn = gr.Button(
260
- "Generate Learning Path",
261
- variant="primary"
262
- )
263
-
264
- text_output = gr.Textbox(
265
- label="Audio Transcription",
266
- lines=4
267
- )
268
-
269
- analysis_output = gr.Textbox(
270
- label="Analysis and Learning Path",
271
- lines=10
272
- )
273
-
274
- mind_map_output = gr.Image(
275
- label="Learning Path Mind Map"
276
- )
277
-
278
- process_btn.click(
279
- fn=self.process_audio,
280
- inputs=[audio_input, path_name, difficulty, include_resources],
281
- outputs={
282
- "transcription": text_output,
283
- "analysis": analysis_output,
284
- "mind_map": mind_map_output
285
- }
286
  )
287
-
288
- return app
 
 
 
 
 
 
 
289
 
290
  if __name__ == "__main__":
291
- try:
292
- generator = LearningPathGenerator()
293
- app = generator.create_interface()
294
- app.launch(debug=Config.DEBUG)
295
- except Exception as e:
296
- logging.error(f"Application error: {str(e)}")
297
- raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
 
 
 
 
 
4
 
5
  class LearningPathGenerator:
6
  def __init__(self):
7
+ self.device = 0 if torch.cuda.is_available() else -1
8
+ # Initialize models
9
+ self.transcriber = pipeline("automatic-speech-recognition",
10
+ model="openai/whisper-base",
11
+ device=self.device)
12
+ self.generator = pipeline("text-generation",
13
+ model="gpt2",
14
+ device=self.device)
15
+
16
  def process_audio(self,
17
  audio_path: str,
18
+ difficulty: str = "intermediate") -> dict:
 
 
19
  try:
20
+ # Transcribe audio
21
+ transcription = self.transcriber(audio_path)["text"]
22
 
23
+ # Generate learning path
24
+ prompt = f"""
25
+ Based on the following text, create a detailed learning path
26
+ for {difficulty} level:
27
+ {transcription}
28
 
29
+ Learning path:
30
+ """
31
 
32
+ analysis = self.generator(prompt,
33
+ max_length=300,
34
+ num_return_sequences=1)[0]["generated_text"]
35
 
36
  return {
37
  "transcription": transcription,
38
+ "analysis": analysis
 
39
  }
 
40
  except Exception as e:
41
+ return {
42
+ "transcription": f"Error: {str(e)}",
43
+ "analysis": "Could not generate analysis."
44
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ def create_interface():
47
+ app = gr.Interface(
48
+ fn=LearningPathGenerator().process_audio,
49
+ inputs=[
50
+ gr.Audio(type="filepath", label="Upload Audio"),
51
+ gr.Dropdown(
52
+ choices=["beginner", "intermediate", "advanced"],
53
+ value="intermediate",
54
+ label="Difficulty Level"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  )
56
+ ],
57
+ outputs=[
58
+ gr.Textbox(label="Audio Transcription"),
59
+ gr.Textbox(label="Learning Path")
60
+ ],
61
+ title="🎓 Learning Path Generator",
62
+ description="Upload an audio file describing your learning goals and receive a personalized learning path!"
63
+ )
64
+ return app
65
 
66
  if __name__ == "__main__":
67
+ app = create_interface()
68
+ app.launch()