Spaces:
Sleeping
Sleeping
Update learning_platform.py
Browse files- learning_platform.py +61 -38
learning_platform.py
CHANGED
|
@@ -39,16 +39,17 @@ class LLMService:
|
|
| 39 |
self.model = "gpt-4o"
|
| 40 |
|
| 41 |
async def generate(self, prompt: str, temperature: float = 1.0) -> str:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
class ContentGenerator:
|
| 54 |
def __init__(self, llm_service: LLMService):
|
|
@@ -107,33 +108,21 @@ class LearningPlatform:
|
|
| 107 |
self.llm_service = LLMService(api_key)
|
| 108 |
self.content_generator = ContentGenerator(self.llm_service)
|
| 109 |
|
| 110 |
-
async def create_module(self, module: LearningModule, topic: str):
|
| 111 |
-
try:
|
| 112 |
-
sections_prompt = f"List 3-4 section titles for module '{module.title}' in {topic}. Return JSON array of strings."
|
| 113 |
-
section_titles = json.loads(await self.llm_service.generate(sections_prompt))
|
| 114 |
-
|
| 115 |
-
for title in section_titles:
|
| 116 |
-
section = await self.content_generator.generate_section_content(topic, title)
|
| 117 |
-
module.sections.append(section)
|
| 118 |
-
|
| 119 |
-
module.is_complete = True
|
| 120 |
-
return module
|
| 121 |
-
except Exception as e:
|
| 122 |
-
st.error(f"Module generation error: {str(e)}")
|
| 123 |
-
raise
|
| 124 |
-
|
| 125 |
-
async def generate_remaining_modules(self, path: LearningPath, topic: str):
|
| 126 |
-
for module in path.modules[1:]:
|
| 127 |
-
await self.create_module(module, topic)
|
| 128 |
-
path.is_generating = False
|
| 129 |
-
|
| 130 |
async def create_course(self, topic: str, difficulty: str) -> LearningPath:
|
| 131 |
try:
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
|
|
|
|
|
|
| 137 |
path = LearningPath(
|
| 138 |
topic=topic,
|
| 139 |
description=f"Course on {topic}",
|
|
@@ -142,14 +131,48 @@ class LearningPlatform:
|
|
| 142 |
difficulty_level=difficulty
|
| 143 |
)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
asyncio.create_task(self.generate_remaining_modules(path, topic))
|
| 150 |
|
| 151 |
return path
|
| 152 |
|
| 153 |
except Exception as e:
|
| 154 |
-
st.error(f"Course creation
|
| 155 |
-
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
self.model = "gpt-4o"
|
| 40 |
|
| 41 |
async def generate(self, prompt: str, temperature: float = 1.0) -> str:
|
| 42 |
+
try:
|
| 43 |
+
st.write("🤖 Calling OpenAI API...")
|
| 44 |
+
response = await self.client.chat.completions.create(
|
| 45 |
+
model=self.model,
|
| 46 |
+
messages=[{"role": "system", "content": prompt}],
|
| 47 |
+
temperature=temperature
|
| 48 |
+
)
|
| 49 |
+
return response.choices[0].message.content
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"OpenAI API error: {str(e)}")
|
| 52 |
+
raise
|
| 53 |
|
| 54 |
class ContentGenerator:
|
| 55 |
def __init__(self, llm_service: LLMService):
|
|
|
|
| 108 |
self.llm_service = LLMService(api_key)
|
| 109 |
self.content_generator = ContentGenerator(self.llm_service)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
async def create_course(self, topic: str, difficulty: str) -> LearningPath:
|
| 112 |
try:
|
| 113 |
+
st.write("🔍 Generating course outline...")
|
| 114 |
+
titles_prompt = f"List 3 module titles for {topic} course. Return as JSON array of strings."
|
| 115 |
+
titles_task = asyncio.create_task(self.llm_service.generate(titles_prompt))
|
| 116 |
|
| 117 |
+
try:
|
| 118 |
+
titles_response = await asyncio.wait_for(titles_task, timeout=30)
|
| 119 |
+
titles = json.loads(titles_response)
|
| 120 |
+
except asyncio.TimeoutError:
|
| 121 |
+
st.error("Timeout generating course outline")
|
| 122 |
+
raise
|
| 123 |
|
| 124 |
+
st.write("📚 Creating course structure...")
|
| 125 |
+
modules = [LearningModule(title=title) for title in titles]
|
| 126 |
path = LearningPath(
|
| 127 |
topic=topic,
|
| 128 |
description=f"Course on {topic}",
|
|
|
|
| 131 |
difficulty_level=difficulty
|
| 132 |
)
|
| 133 |
|
| 134 |
+
st.write("🎯 Generating first module...")
|
| 135 |
+
try:
|
| 136 |
+
await asyncio.wait_for(
|
| 137 |
+
self.create_module(path.modules[0], topic),
|
| 138 |
+
timeout=60
|
| 139 |
+
)
|
| 140 |
+
except asyncio.TimeoutError:
|
| 141 |
+
st.error("Timeout generating first module")
|
| 142 |
+
raise
|
| 143 |
|
| 144 |
+
st.write("✨ Starting background generation...")
|
| 145 |
asyncio.create_task(self.generate_remaining_modules(path, topic))
|
| 146 |
|
| 147 |
return path
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
+
st.error(f"Course creation failed: {str(e)}")
|
| 151 |
+
raise
|
| 152 |
+
|
| 153 |
+
async def create_module(self, module: LearningModule, topic: str, timeout: int = 30):
|
| 154 |
+
try:
|
| 155 |
+
st.write(f"Generating sections for {module.title}...")
|
| 156 |
+
sections_prompt = f"List 3 section titles for module '{module.title}' in {topic}. Return JSON array."
|
| 157 |
+
|
| 158 |
+
section_titles = json.loads(await asyncio.wait_for(
|
| 159 |
+
self.llm_service.generate(sections_prompt),
|
| 160 |
+
timeout=timeout
|
| 161 |
+
))
|
| 162 |
+
|
| 163 |
+
for title in section_titles:
|
| 164 |
+
st.write(f"Creating section: {title}")
|
| 165 |
+
section = await self.content_generator.generate_section_content(topic, title)
|
| 166 |
+
module.sections.append(section)
|
| 167 |
+
|
| 168 |
+
module.is_complete = True
|
| 169 |
+
return module
|
| 170 |
+
except Exception as e:
|
| 171 |
+
st.error(f"Module generation failed: {str(e)}")
|
| 172 |
+
raise
|
| 173 |
+
|
| 174 |
+
async def generate_remaining_modules(self, path: LearningPath, topic: str):
|
| 175 |
+
for i, module in enumerate(path.modules[1:], 1):
|
| 176 |
+
st.write(f"Generating module {i+1} of {len(path.modules)}...")
|
| 177 |
+
await self.create_module(module, topic)
|
| 178 |
+
path.is_generating = False
|