martinezjandrew's picture
Update app.py
dfc6e99 verified
Raw History Blame Contribute Delete
5.83 kB
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}