Soltane777 commited on
Commit
949b7b5
·
verified ·
1 Parent(s): 19ba462

Update backend/main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +69 -69
backend/main.py CHANGED
@@ -1,69 +1,69 @@
1
- from fastapi import FastAPI, UploadFile, File, Form
2
- from pydantic import BaseModel
3
- import shutil
4
- import os
5
-
6
- from backend.models import summarizer, image_captioning, qa_pipeline, translator, code_generator
7
- from backend.utils import extract_text_from_pdf, extract_text_from_document
8
-
9
- app = FastAPI()
10
-
11
- UPLOAD_DIR = "uploads"
12
- os.makedirs(UPLOAD_DIR, exist_ok=True)
13
-
14
- class QARequest(BaseModel):
15
- question: str
16
- context: str
17
-
18
- @app.get("/")
19
- async def home():
20
- return {"message": "AI Web App is Running!"}
21
-
22
- # 1️⃣ استخراج النصوص من المستندات
23
- @app.post("/extract_text/")
24
- async def extract_text(file: UploadFile = File(...)):
25
- file_path = f"{UPLOAD_DIR}/{file.filename}"
26
- with open(file_path, "wb") as f:
27
- shutil.copyfileobj(file.file, f)
28
-
29
- text = extract_text_from_document(file_path)
30
- os.remove(file_path)
31
- return {"text": text}
32
-
33
- # 2️⃣ تلخيص المستندات
34
- @app.post("/summarize/")
35
- async def summarize_text(text: str = Form(...)):
36
- summary = summarizer(text)
37
- return {"summary": summary[0]['summary_text']}
38
-
39
- # 3️⃣ تحليل الصور وتوليد وصف لها
40
- @app.post("/image_caption/")
41
- async def caption_image(file: UploadFile = File(...)):
42
- file_path = f"{UPLOAD_DIR}/{file.filename}"
43
- with open(file_path, "wb") as f:
44
- shutil.copyfileobj(file.file, f)
45
-
46
- with open(file_path, "rb") as img:
47
- image_bytes = img.read()
48
-
49
- caption = image_captioning(image_bytes)
50
- os.remove(file_path)
51
- return {"caption": caption[0]['generated_text']}
52
-
53
- # 4️⃣ الإجابة على الأسئلة من النصوص
54
- @app.post("/qa/")
55
- async def answer_question(request: QARequest):
56
- answer = qa_pipeline(request.question, request.context)
57
- return {"answer": answer["answer"]}
58
-
59
- # 5️⃣ ترجمة المستندات
60
- @app.post("/translate/")
61
- async def translate_text(text: str = Form(...)):
62
- translation = translator(text)
63
- return {"translation": translation[0]['translation_text']}
64
-
65
- # 6️⃣ توليد أكواد التصور من أوامر نصية
66
- @app.post("/generate_code/")
67
- async def generate_code(prompt: str = Form(...)):
68
- code = code_generator(prompt)
69
- return {"code": code}
 
1
+ from fastapi import FastAPI, UploadFile, File, Form
2
+ from pydantic import BaseModel
3
+ import shutil
4
+ import os
5
+
6
+ from backend.models import summarizer, image_captioning, qa_pipeline, translator, code_generator
7
+ from backend.utils import extract_text_from_pdf, extract_text_from_document
8
+
9
+ app = FastAPI()
10
+
11
+ UPLOAD_DIR = "/tmp/uploads"
12
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
13
+
14
+ class QARequest(BaseModel):
15
+ question: str
16
+ context: str
17
+
18
+ @app.get("/")
19
+ async def home():
20
+ return {"message": "AI Web App is Running!"}
21
+
22
+ # 1️⃣ استخراج النصوص من المستندات
23
+ @app.post("/extract_text/")
24
+ async def extract_text(file: UploadFile = File(...)):
25
+ file_path = f"{UPLOAD_DIR}/{file.filename}"
26
+ with open(file_path, "wb") as f:
27
+ shutil.copyfileobj(file.file, f)
28
+
29
+ text = extract_text_from_document(file_path)
30
+ os.remove(file_path)
31
+ return {"text": text}
32
+
33
+ # 2️⃣ تلخيص المستندات
34
+ @app.post("/summarize/")
35
+ async def summarize_text(text: str = Form(...)):
36
+ summary = summarizer(text)
37
+ return {"summary": summary[0]['summary_text']}
38
+
39
+ # 3️⃣ تحليل الصور وتوليد وصف لها
40
+ @app.post("/image_caption/")
41
+ async def caption_image(file: UploadFile = File(...)):
42
+ file_path = f"{UPLOAD_DIR}/{file.filename}"
43
+ with open(file_path, "wb") as f:
44
+ shutil.copyfileobj(file.file, f)
45
+
46
+ with open(file_path, "rb") as img:
47
+ image_bytes = img.read()
48
+
49
+ caption = image_captioning(image_bytes)
50
+ os.remove(file_path)
51
+ return {"caption": caption[0]['generated_text']}
52
+
53
+ # 4️⃣ الإجابة على الأسئلة من النصوص
54
+ @app.post("/qa/")
55
+ async def answer_question(request: QARequest):
56
+ answer = qa_pipeline(request.question, request.context)
57
+ return {"answer": answer["answer"]}
58
+
59
+ # 5️⃣ ترجمة المستندات
60
+ @app.post("/translate/")
61
+ async def translate_text(text: str = Form(...)):
62
+ translation = translator(text)
63
+ return {"translation": translation[0]['translation_text']}
64
+
65
+ # 6️⃣ توليد أكواد التصور من أوامر نصية
66
+ @app.post("/generate_code/")
67
+ async def generate_code(prompt: str = Form(...)):
68
+ code = code_generator(prompt)
69
+ return {"code": code}