Download routers/content_generation_route.py from aliroya32/ContiAI: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/spaces/aliroya32/ContiAI/resolve/main/routers/content_generation_route.py
- Command line
-
hf download hf://spaces/aliroya32/ContiAI/routers/content_generation_route.py
-
curl -L -o content_generation_route.py https://huggingface.co/spaces/aliroya32/ContiAI/resolve/main/routers/content_generation_route.py
3.87 kB
| import os | |
| from crewai import Crew, Process | |
| import json | |
| from typing import Dict, Any | |
| from fastapi import APIRouter, HTTPException, Depends | |
| from fastapi.responses import HTMLResponse | |
| from agents.content_phase import ( | |
| query_agent, | |
| query_task, | |
| course_writer_agent, | |
| course_writer_task, | |
| ) | |
| import app | |
| from core import generate_course_content | |
| from core.content_converter_service import convert_dict_to_html | |
| from modules import ( | |
| llm, | |
| inputs, | |
| ) | |
| from tools import scrape_course, SerperExhaustedError,CURRENT_SERPER_INDEX | |
| from schemas import ( | |
| CourseWithQueries, | |
| CourseContent, | |
| ) | |
| router = APIRouter(prefix="/content", tags=["Content Generation"]) | |
| # ------------------------------------ | |
| # === Agent 7:Training Generator === | |
| # ------------------------------------ | |
| def run_keywords(outlines: CourseContent): | |
| agent = query_agent() | |
| task = query_task(agent) | |
| keywords_crew = Crew( | |
| agents=[agent], | |
| tasks=[task], | |
| process=Process.sequential, | |
| ) | |
| result = keywords_crew.kickoff(inputs={"course": outlines.dict()}) | |
| print(result.json_dict) | |
| return {"message": "Keywords Generated Well ๐", "result": result} | |
| # @router.post("/scrape") | |
| # def scrape_endpoint(course:CourseWithQueries): | |
| # try: | |
| # # ูุญูู Pydantic model ูู dict ุจููุณ ุงูุดูู ุงููู ุงููุงููุดู ู ุณุชููุงูุง | |
| # course_dict = course.dict() | |
| # summary, raw = scrape_course(course_dict) | |
| # return { | |
| # "message": "Queries Searched and Scraped Well ๐", | |
| # "summary": summary, | |
| # "raw": raw, | |
| # } | |
| # return {"message": "Queries Searched and Scraped Well ๐", "summary": summary, "raw": raw} | |
| # except Exception as e: | |
| # # ูู ุงูุจุฑูุฏุงูุดู ุฎููู logging ู ู ุชุทูุนุด ุงูุฑุณุงูุฉ ุงูุฎุงู ููุนู ูู | |
| # raise HTTPException(status_code=500, detail=str(e)) | |
| def scrape_endpoint(course: CourseWithQueries): | |
| try: | |
| course_dict = course.dict() | |
| summary, raw = scrape_course(course_dict) | |
| return { | |
| "message": "Queries Searched and Scraped Well ๐", | |
| "serper_api_used_index": CURRENT_SERPER_INDEX, | |
| "summary": summary, | |
| "raw": raw, | |
| } | |
| except SerperExhaustedError as e: | |
| raise HTTPException(status_code=500, detail=f"{str(e)}") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}") | |
| def run_content_generation(course_data: Dict): | |
| # ูุญูู ูู dict ููุจุนุชูุง ููู service | |
| course_dict = course_data | |
| result = generate_course_content(course_dict) | |
| return {"message": "Content Generated Well ๐", "result": result} | |
| def convert_to_html(course_data: Dict[str, Any]): | |
| """ | |
| ุชุญููู ุจูุงูุงุช ุงูู ูุฑุฑ ุงูุฏุฑุงุณู ู ู Dictionary ุฅูู HTML | |
| Args: | |
| course_data: Dictionary ูุญุชูู ุนูู ุจูุงูุงุช ุงูู ูุฑุฑ ุงูุฏุฑุงุณู | |
| - course_name: ุงุณู ุงูู ูุฑุฑ | |
| - course_audience: ุงูุฌู ููุฑ ุงูู ุณุชูุฏู | |
| - results: ูุงุฆู ุฉ ุงููุญุฏุงุช | |
| Returns: | |
| HTML string ูุงู ู ุฌุงูุฒ ููุนุฑุถ | |
| """ | |
| try: | |
| # ุงูุชุญูู ู ู ูุฌูุฏ ุงูุจูุงูุงุช ุงูุฃุณุงุณูุฉ | |
| if not course_data: | |
| raise HTTPException(status_code=400, detail="ูุง ุชูุฌุฏ ุจูุงูุงุช ู ุฑุณูุฉ") | |
| # ุชุญููู Dictionary ุฅูู HTML | |
| html_content = convert_dict_to_html(course_data) | |
| return HTMLResponse(content=html_content) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"ุฎุทุฃ ูู ุงูุชุญููู: {str(e)}") | |