Spaces:
Running
Running
File size: 1,479 Bytes
07a0bf2 | 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 | 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)}") |