Spaces:
Sleeping
Sleeping
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` عند فتح التطبيق | |
async def serve_frontend(): | |
return FileResponse("frontend/index.html") | |
# 📌 نموذج للطلبات الخاصة بالإجابة على الأسئلة | |
class QARequest(BaseModel): | |
question: str | |
context: str | |
# 1️⃣ استخراج النصوص من المستندات | |
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️⃣ تلخيص المستندات | |
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️⃣ تحليل الصور وتوليد وصف لها | |
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️⃣ الإجابة على الأسئلة من النصوص | |
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️⃣ ترجمة المستندات | |
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️⃣ توليد أكواد التصور من أوامر نصية | |
async def generate_code(prompt: str = Form(...)): | |
code = code_generator(prompt) | |
return {"code": code} | |