from fastapi import FastAPI from fastapi.responses import HTMLResponse from haystack import Pipeline from pydantic import BaseModel from typing import List from pathlib import Path from classes import CourseOutline tags_metadata = [ { "name": "course generation", "description": "Generate and regenerate course content.", } ] app = FastAPI( title="TrainingCourseGen", openapi_tags=tags_metadata, ) @app.get("/") async def root(): content = """ TrainingCourseGen ScriboAI Logo

Welcome to TrainingCourseGen!

This backend API supports ScriboAI, a tool for automating corporate training content. It includes three key endpoints:

""" return HTMLResponse(content=content, status_code=200) # ---------- Existing Script Generation Endpoints ---------- class ScriptRequest(BaseModel): topic: str time: str @app.post("/generate-outline", tags=["course generation"]) async def generate_script(req: ScriptRequest): with open("./pipelines/generate_outline.yml", "r") as file: content = file.read() pipe = Pipeline.loads(content) results = pipe.run( { "prompt_builder": { "topic": req.topic, "time": req.time, "schema": CourseOutline.model_json_schema(), }, } ) return {"response": results} class ScriptUpdate(BaseModel): script: CourseOutline notes: str = "" @app.post("/update-outline", tags=["course generation"]) async def update_script(req: ScriptUpdate): with open("./pipelines/update_outline.yml", "r") as file: content = file.read() pipe = Pipeline.loads(content) results = pipe.run( { "prompt_builder": { "script": req.script, "notes": req.notes, "schema": CourseOutline.model_json_schema(), }, } ) return {"response": results} # ---------- New Module Content Generation Endpoint ---------- class Module(BaseModel): name: str duration: str subtopics: List[str] class ModuleOutline(BaseModel): title: str modules: List[Module] @app.post("/generate-module-content", tags=["course generation"]) async def generate_module_content(req: ModuleOutline): with open("./pipelines/generate_module_content.yml", "r") as file: pipe = Pipeline.loads(file.read()) Path("./modules").mkdir(parents=True, exist_ok=True) def is_technical_topic(name, subtopics): keywords = ["API", "database", "code", "technical", "machine learning", "AI", "infrastructure", "engineering", "system", "security", "algorithm"] combined_text = f"{name} {' '.join(subtopics)}".lower() return any(k in combined_text for k in keywords) content_results = {} for module in req.modules: is_technical = is_technical_topic(module.name, module.subtopics) reading_speed = 150 if is_technical else 200 minutes = int(module.duration.split()[0]) word_count = int(minutes * reading_speed) formatted_subtopics = "\n".join([f"- {s}" for s in module.subtopics]) try: result = pipe.run({ "prompt_builder": { "name": module.name, "duration": module.duration, "word_count": word_count, "formatted_subtopics": formatted_subtopics, } }) markdown = result["llm"]["replies"][0] content_results[module.name] = markdown filename = module.name.replace(" ", "_").lower() with open(f"./modules/{filename}.md", "w", encoding="utf-8") as f: f.write(markdown) except Exception as e: content_results[module.name] = {"error": str(e)} return {"response": content_results}