File size: 5,827 Bytes
71b8ef4
0c4e23b
71b8ef4
 
4983616
 
f02d14f
71b8ef4
eb58818
 
 
 
 
 
4983616
eb58818
 
 
 
71b8ef4
 
 
0c4e23b
 
 
 
72b272f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c4e23b
 
72b272f
0c4e23b
72b272f
 
 
 
 
 
 
 
 
 
 
dfc6e99
72b272f
 
 
3b671a6
eb58818
0c4e23b
71b8ef4
 
4983616
 
8010521
71b8ef4
 
 
eb58818
8010521
a817a92
71b8ef4
 
 
 
8010521
 
f02d14f
 
 
 
 
8010521
 
 
 
 
 
 
d925aa5
8010521
 
3b671a6
8010521
3b671a6
8010521
 
 
 
 
 
 
 
 
69d3b98
8010521
 
 
71b8ef4
 
4983616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
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 = """
    <html>
        <head>
            <title>TrainingCourseGen</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    background-color: #f9f9f9;
                    color: #333;
                    text-align: center;
                    padding: 50px;
                }
                img {
                    width: 80px;
                    margin-bottom: 20px;
                }
                h1 {
                    color: #2c3e50;
                }
                a {
                    color: #1a73e8;
                    text-decoration: none;
                }
                a:hover {
                    text-decoration: underline;
                }
                .links {
                    margin-top: 30px;
                }
            </style>
        </head>
        <body>
            <img src="https://github.com/Zoqi-Andrew-Senior-Project/ScriboAI-Consumer/blob/main/consumer_frontend/public/minilogo.png?raw=true" alt="ScriboAI Logo" />
            <h1>Welcome to TrainingCourseGen!</h1>
            <p>
                This backend API supports ScriboAI, a tool for automating corporate training content.
                It includes three key endpoints:
            </p>
            <ul style="text-align: left; display: inline-block;">
                <li><strong>/generate_outline</strong> - Create a course outline from a topic</li>
                <li><strong>/update_outline</strong> - Modify the generated outline</li>
                <li><strong>/generate_pages</strong> - Generate full training pages from the outline</li>
            </ul>
            <div class="links">
                <p><a href="https://github.com/Zoqi-Andrew-Senior-Project/ScriboAI-Consumer" target="_blank" rel="noopener noreferrer">๐Ÿ“‚ GitHub - ScriboAI Consumer App</a></p>
                <p><a href="https://zoqiandrewseniorproject-trainingcoursegen.hf.space/docs" target="_blank" rel="noopener noreferrer">๐Ÿ“„ Hugging Face Space - API Docs</a></p>
                <p><a href="/docs">๐Ÿ“ Local API Docs</a></p>
            </div>
        </body>
    </html>
    """
    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}