Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
# app.py
|
| 2 |
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
from pydantic import BaseModel, HttpUrl, Field
|
| 4 |
-
from typing import List, Dict, Optional, Any
|
| 5 |
|
| 6 |
from helpers.text_blocks import extract_text_from_url
|
| 7 |
-
from helpers.output_clipper import clip_by_ranges
|
| 8 |
from parser.assembler import parse_law_from_texts
|
| 9 |
from supabase_utils import save_law_to_supabase
|
| 10 |
from helpers.indexer import build_indexed_response
|
| 11 |
-
from helpers.indexer_simple import build_indexed
|
| 12 |
from helpers.blocks_all import extract_from_url
|
| 13 |
|
| 14 |
|
|
@@ -18,112 +16,33 @@ app = FastAPI(
|
|
| 18 |
version="2.1.0"
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
class IndexedURLRequest(BaseModel):
|
| 22 |
url: HttpUrl
|
| 23 |
save_to_supabase: bool = False
|
| 24 |
timeout: int = Field(10, ge=1, le=60)
|
| 25 |
-
title_index: int = Field(
|
| 26 |
preamble_start: Optional[int] = Field(None, ge=0)
|
| 27 |
-
preamble_end: int = Field(
|
| 28 |
body_start: Optional[int] = Field(None, ge=0)
|
| 29 |
body_end: Optional[int] = Field(None, ge=0)
|
| 30 |
return_parsed: bool = Field(False, description="إرجاع النتيجة محلّلة (parsed) بدلاً من raw texts")
|
| 31 |
-
|
| 32 |
-
# -----------------------------
|
| 33 |
-
# نماذج البيانات
|
| 34 |
-
# -----------------------------
|
| 35 |
-
class URLRequest(BaseModel):
|
| 36 |
-
url: HttpUrl
|
| 37 |
-
return_parsed: bool = False
|
| 38 |
-
save_to_supabase: bool = False
|
| 39 |
-
timeout: int = Field(10, ge=1, le=60)
|
| 40 |
-
ranges: Optional[List[List[int]]] = None
|
| 41 |
-
|
| 42 |
-
class TextResponse(BaseModel):
|
| 43 |
-
text: str
|
| 44 |
-
|
| 45 |
-
class LegalDocumentResponse(BaseModel):
|
| 46 |
-
raw_texts: Optional[List[TextResponse]] = None
|
| 47 |
-
parsed_document: Optional[Dict[str, Any]] = None
|
| 48 |
-
|
| 49 |
-
# -----------------------------
|
| 50 |
-
# التحقق من صحة النطاقات
|
| 51 |
-
# -----------------------------
|
| 52 |
-
def validate_ranges(ranges: List[List[int]]) -> List[Tuple[int, int]]:
|
| 53 |
-
validated_ranges = []
|
| 54 |
-
for i, r in enumerate(ranges):
|
| 55 |
-
if len(r) != 2:
|
| 56 |
-
raise HTTPException(status_code=400, detail=f"النطاق رقم {i+1} غير صحيح")
|
| 57 |
-
start, end = r
|
| 58 |
-
if not all(isinstance(x, int) for x in (start, end)):
|
| 59 |
-
raise HTTPException(status_code=400, detail=f"النطاق رقم {i+1} يجب أن يحتوي على أرقام صحيحة")
|
| 60 |
-
if start < 0 or end <= start:
|
| 61 |
-
raise HTTPException(status_code=400, detail=f"النطاق رقم {i+1} غير صحيح. النهاية يجب أن تكون أكبر من البداية")
|
| 62 |
-
validated_ranges.append((start, end))
|
| 63 |
-
return validated_ranges
|
| 64 |
-
|
| 65 |
-
# -----------------------------
|
| 66 |
-
# دالة معالجة مشتركة
|
| 67 |
-
# -----------------------------
|
| 68 |
-
async def process_texts(
|
| 69 |
-
url: str,
|
| 70 |
-
timeout: int,
|
| 71 |
-
return_parsed: bool,
|
| 72 |
-
save_to_db: bool,
|
| 73 |
ranges: Optional[List[List[int]]] = None
|
| 74 |
-
) -> LegalDocumentResponse:
|
| 75 |
-
# 1) استخراج جميع النصوص
|
| 76 |
-
all_texts = await extract_text_from_url(url, timeout)
|
| 77 |
-
|
| 78 |
-
# 2) تطبيق النطاقات إذا وجدت
|
| 79 |
-
if ranges:
|
| 80 |
-
all_texts = clip_by_ranges(all_texts, validate_ranges(ranges))
|
| 81 |
-
|
| 82 |
-
# 3) تحليل القانون إذا طلب
|
| 83 |
-
if return_parsed:
|
| 84 |
-
parsed_document = parse_law_from_texts(all_texts)
|
| 85 |
-
parsed_document["saved_to_db"] = False
|
| 86 |
-
if save_to_db:
|
| 87 |
-
save_law_to_supabase(parsed_document["law"])
|
| 88 |
-
parsed_document["saved_to_db"] = True
|
| 89 |
-
return LegalDocumentResponse(parsed_document=parsed_document)
|
| 90 |
-
|
| 91 |
-
# 4) النصوص الخام
|
| 92 |
-
return LegalDocumentResponse(raw_texts=all_texts)
|
| 93 |
-
|
| 94 |
-
# -----------------------------
|
| 95 |
-
# نقطة النهاية POST
|
| 96 |
-
# -----------------------------
|
| 97 |
-
@app.post("/extract", response_model=LegalDocumentResponse)
|
| 98 |
-
async def extract_text_post(request: URLRequest):
|
| 99 |
-
try:
|
| 100 |
-
return await process_texts(
|
| 101 |
-
url=str(request.url),
|
| 102 |
-
timeout=request.timeout,
|
| 103 |
-
return_parsed=request.return_parsed,
|
| 104 |
-
save_to_db=request.save_to_supabase,
|
| 105 |
-
ranges=request.ranges
|
| 106 |
-
)
|
| 107 |
-
except Exception as e:
|
| 108 |
-
raise HTTPException(status_code=500, detail=f"خطأ في معالجة المحتوى: {str(e)}")
|
| 109 |
|
| 110 |
|
| 111 |
-
|
| 112 |
-
# نقطة النهاية الجديدة
|
| 113 |
-
# ============================
|
| 114 |
-
@app.post("/extract/indexed")
|
| 115 |
async def extract_indexed(request: IndexedURLRequest):
|
| 116 |
try:
|
| 117 |
# 1) استخراج النصوص الخام من الرابط
|
| 118 |
raw_texts = await extract_text_from_url(str(request.url), request.timeout)
|
| 119 |
|
| 120 |
-
# 2) بناء قائمة مفهرسة
|
| 121 |
datalist = build_indexed_response(
|
| 122 |
texts=raw_texts,
|
| 123 |
title_index=request.title_index,
|
| 124 |
-
preamble_start=request.preamble_start,
|
| 125 |
preamble_end=request.preamble_end,
|
| 126 |
-
body_start=request.body_start,
|
| 127 |
body_end=request.body_end
|
| 128 |
)
|
| 129 |
|
|
@@ -133,7 +52,6 @@ async def extract_indexed(request: IndexedURLRequest):
|
|
| 133 |
|
| 134 |
# 4) حفظ إلى Supabase إذا طُلب
|
| 135 |
if request.save_to_supabase:
|
| 136 |
-
# نفترض أن parse_law_from_texts تعيد dict يحتوي على مفتاح "law"
|
| 137 |
save_law_to_supabase(parsed["law"])
|
| 138 |
|
| 139 |
return parsed
|
|
@@ -144,8 +62,9 @@ async def extract_indexed(request: IndexedURLRequest):
|
|
| 144 |
except Exception as e:
|
| 145 |
raise HTTPException(status_code=500, detail=f"خطأ في معالجة المحتوى: {str(e)}")
|
| 146 |
|
|
|
|
| 147 |
# ============================
|
| 148 |
-
# نقطة
|
| 149 |
# ============================
|
| 150 |
@app.get("/extract_link")
|
| 151 |
async def extract_link_get(
|
|
@@ -153,11 +72,8 @@ async def extract_link_get(
|
|
| 153 |
timeout: int = Query(10, ge=1, le=60, description="مهلة الطلب بالثواني")
|
| 154 |
):
|
| 155 |
try:
|
| 156 |
-
# 1) استخراج النصوص الخام من الرابط
|
| 157 |
raw_texts = await extract_from_url(str(url), timeout)
|
| 158 |
-
#full = build_indexed(texts=raw_texts)
|
| 159 |
-
# 2) بناء القائمة المفهرسة
|
| 160 |
return raw_texts
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
-
raise HTTPException(status_code=500, detail=f"خطأ في معالجة المحتوى: {str(e)}")
|
|
|
|
| 1 |
# app.py
|
| 2 |
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
from pydantic import BaseModel, HttpUrl, Field
|
| 4 |
+
from typing import List, Dict, Optional, Any
|
| 5 |
|
| 6 |
from helpers.text_blocks import extract_text_from_url
|
|
|
|
| 7 |
from parser.assembler import parse_law_from_texts
|
| 8 |
from supabase_utils import save_law_to_supabase
|
| 9 |
from helpers.indexer import build_indexed_response
|
|
|
|
| 10 |
from helpers.blocks_all import extract_from_url
|
| 11 |
|
| 12 |
|
|
|
|
| 16 |
version="2.1.0"
|
| 17 |
)
|
| 18 |
|
| 19 |
+
|
| 20 |
class IndexedURLRequest(BaseModel):
|
| 21 |
url: HttpUrl
|
| 22 |
save_to_supabase: bool = False
|
| 23 |
timeout: int = Field(10, ge=1, le=60)
|
| 24 |
+
title_index: Optional[int] = Field(None, ge=0)
|
| 25 |
preamble_start: Optional[int] = Field(None, ge=0)
|
| 26 |
+
preamble_end: Optional[int] = Field(None, ge=0)
|
| 27 |
body_start: Optional[int] = Field(None, ge=0)
|
| 28 |
body_end: Optional[int] = Field(None, ge=0)
|
| 29 |
return_parsed: bool = Field(False, description="إرجاع النتيجة محلّلة (parsed) بدلاً من raw texts")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
ranges: Optional[List[List[int]]] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
+
@app.post("/extract")
|
|
|
|
|
|
|
|
|
|
| 34 |
async def extract_indexed(request: IndexedURLRequest):
|
| 35 |
try:
|
| 36 |
# 1) استخراج النصوص الخام من الرابط
|
| 37 |
raw_texts = await extract_text_from_url(str(request.url), request.timeout)
|
| 38 |
|
| 39 |
+
# 2) بناء قائمة مفهرسة بالخيارات المرسلة (قد تكون None)
|
| 40 |
datalist = build_indexed_response(
|
| 41 |
texts=raw_texts,
|
| 42 |
title_index=request.title_index,
|
| 43 |
+
preamble_start=request.preamble_start,
|
| 44 |
preamble_end=request.preamble_end,
|
| 45 |
+
body_start=request.body_start,
|
| 46 |
body_end=request.body_end
|
| 47 |
)
|
| 48 |
|
|
|
|
| 52 |
|
| 53 |
# 4) حفظ إلى Supabase إذا طُلب
|
| 54 |
if request.save_to_supabase:
|
|
|
|
| 55 |
save_law_to_supabase(parsed["law"])
|
| 56 |
|
| 57 |
return parsed
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
raise HTTPException(status_code=500, detail=f"خطأ في معالجة المحتوى: {str(e)}")
|
| 64 |
|
| 65 |
+
|
| 66 |
# ============================
|
| 67 |
+
# نقطة GET بسيطة لإرجاع النصوص بدون فهرسة
|
| 68 |
# ============================
|
| 69 |
@app.get("/extract_link")
|
| 70 |
async def extract_link_get(
|
|
|
|
| 72 |
timeout: int = Query(10, ge=1, le=60, description="مهلة الطلب بالثواني")
|
| 73 |
):
|
| 74 |
try:
|
|
|
|
| 75 |
raw_texts = await extract_from_url(str(url), timeout)
|
|
|
|
|
|
|
| 76 |
return raw_texts
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
raise HTTPException(status_code=500, detail=f"خطأ في معالجة المحتوى: {str(e)}")
|