medodeyaa commited on
Commit
e578f95
·
verified ·
1 Parent(s): 53dface

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Depends, HTTPException, Security
2
+ from fastapi.security.api_key import APIKeyHeader
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from pydantic import BaseModel
5
+ from transformers import pipeline
6
+ import os
7
+
8
+ # سحب الباسوورد من الإعدادات
9
+ SECRET_API_KEY = os.environ.get("API_KEY")
10
+ api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
11
+
12
+ app = FastAPI(title="Dentor NLP API")
13
+
14
+ # السماح للموقع بتبادل البيانات
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=["*"],
18
+ allow_credentials=True,
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
+ )
22
+
23
+ def get_api_key(api_key_header: str = Security(api_key_header)):
24
+ if api_key_header == SECRET_API_KEY:
25
+ return api_key_header
26
+ raise HTTPException(status_code=403, detail="Access Denied: Invalid API Key")
27
+
28
+ # تحميل الموديلات (من الفولدرات اللي لسه رافعينها)
29
+ print("Loading BioBART Summarizer...")
30
+ summarizer = pipeline("summarization", model="./biobart", tokenizer="./biobart")
31
+
32
+ print("Loading Helsinki Translator...")
33
+ translator = pipeline("translation", model="./helsinki", tokenizer="./helsinki")
34
+
35
+ print("All Models Loaded!")
36
+
37
+ class TextRequest(BaseModel):
38
+ text: str
39
+
40
+ # مسار التلخيص
41
+ @app.post("/summarize")
42
+ async def summarize_text(request: TextRequest, api_key: str = Depends(get_api_key)):
43
+ try:
44
+ summary = summarizer(request.text, max_length=150, min_length=30, do_sample=False)
45
+ return {"status": "success", "summary": summary[0]['summary_text']}
46
+ except Exception as e:
47
+ raise HTTPException(status_code=500, detail=str(e))
48
+
49
+ # مسار الترجمة
50
+ @app.post("/translate")
51
+ async def translate_text(request: TextRequest, api_key: str = Depends(get_api_key)):
52
+ try:
53
+ translation = translator(request.text)
54
+ return {"status": "success", "translation": translation[0]['translation_text']}
55
+ except Exception as e:
56
+ raise HTTPException(status_code=500, detail=str(e))