File size: 3,457 Bytes
1631029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from fastapi import FastAPI, UploadFile, File, Form
from pydantic import BaseModel
import shutil
import os

from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

# ✅ تعريف التطبيق أولًا قبل استخدامه
app = FastAPI()

# ✅ تحديد مجلد الرفع
UPLOAD_DIR = "/tmp/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)

# ✅ استيراد الدوال بعد تعريف `app`
from backend.models import summarizer, image_captioning, qa_pipeline, translator, code_generator
from backend.utils import extract_text_from_pdf, extract_text_from_document

# ✅ خدمة الملفات الثابتة (Frontend)
app.mount("/static", StaticFiles(directory="frontend"), name="static")

# ✅ عرض `index.html` عند فتح التطبيق
@app.get("/")
async def serve_frontend():
    return FileResponse("frontend/index.html")

# 📌 نموذج للطلبات الخاصة بالإجابة على الأسئلة
class QARequest(BaseModel):
    question: str
    context: str

# 1️⃣ استخراج النصوص من المستندات
@app.post("/extract_text")
async def extract_text(file: UploadFile = File(...)):
    file_path = f"{UPLOAD_DIR}/{file.filename}"
    with open(file_path, "wb") as f:
        shutil.copyfileobj(file.file, f)

    text = extract_text_from_document(file_path)
    os.remove(file_path)
    return {"text": text}

# 2️⃣ تلخيص المستندات
@app.post("/summarize")
async def summarize_text(text: str = Form(...)):
    summary = summarizer(text)
    if isinstance(summary, list) and len(summary) > 0 and "summary_text" in summary[0]:
        return {"summary": summary[0]["summary_text"]}
    else:
        return {"error": "Summarization failed. No valid response."}

# 3️⃣ تحليل الصور وتوليد وصف لها
@app.post("/image_caption")
async def caption_image(file: UploadFile = File(...)):
    file_path = f"{UPLOAD_DIR}/{file.filename}"
    with open(file_path, "wb") as f:
        shutil.copyfileobj(file.file, f)

    with open(file_path, "rb") as img:
        image_bytes = img.read()

    caption = image_captioning(image_bytes)
    os.remove(file_path)

    if isinstance(caption, list) and len(caption) > 0 and "generated_text" in caption[0]:
        return {"caption": caption[0]["generated_text"]}
    else:
        return {"error": "Image captioning failed. No valid response received."}

# 4️⃣ الإجابة على الأسئلة من النصوص
@app.post("/qa/")
async def answer_question(request: QARequest):
    answer = qa_pipeline(request.question, request.context)
    if "answer" in answer:
        return {"answer": answer["answer"]}
    else:
        return {"error": "No valid answer found."}

# 5️⃣ ترجمة المستندات
@app.post("/translate")
async def translate_text(
    text: str = Form(...),
    source_lang: str = Form(...),
    target_lang: str = Form(...)
):
    translation = translator(text, source_lang, target_lang)
    if isinstance(translation, list) and len(translation) > 0 and "translation_text" in translation[0]:
        return {"translation": translation[0]["translation_text"]}
    else:
        return {"error": "Translation failed. No valid response."}


# 6️⃣ توليد أكواد التصور من أوامر نصية
@app.post("/generate_code")
async def generate_code(prompt: str = Form(...)):
    code = code_generator(prompt)
    return {"code": code}