import os from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from nlp import process_full_document_pipeline, build_knowledge_graph_json app = FastAPI(title="Text2Tale - Get Knowledge Graph From Text", version="1.0") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class GraphRequest(BaseModel): text: str @app.post("/generate_graph") async def generate_graph_api(request: GraphRequest): try: camel_path = os.path.dirname(os.path.abspath(__file__)) extracted_relations = process_full_document_pipeline( full_text=request.text, camel_parser_path=camel_path ) if not extracted_relations: return { "message": "لم يتم استخراج أي علاقات من النص.", "triplets": [], "graph": {"nodes": [], "edges": []} } graph_data = build_knowledge_graph_json(extracted_relations) return { "message": "تم بناء الغراف المعرفي بنجاح", "triplets": extracted_relations, "graph": graph_data } except Exception as e: # التقاط أي خطأ وإرجاعه بشكل مقروء raise HTTPException(status_code=500, detail=f"حدث خطأ : {str(e)}")