Spaces:
Sleeping

File size: 1,390 Bytes
696c82e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eebc7e3
696c82e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer
from PIL import Image
import io

app = FastAPI(title="Beauty AI Embedding Server")

# السماح للباك إند الخاص بك بالاتصال
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

print("⏳ جاري تحميل نموذج CLIP...")
model = SentenceTransformer('sentence-transformers/clip-ViT-B-32-multilingual-v1')
print("✅ تم تحميل النموذج بنجاح!")

class TextRequest(BaseModel):
    text: str

@app.get("/")
def home():
    return {"message": "سيرفر الذكاء الاصطناعي يعمل بنجاح 🚀"}

@app.post("/api/embed-text")
def embed_text(req: TextRequest):
    try:
        vector = model.encode(req.text).tolist()
        return {"embedding": vector}
    except Exception as e:
        return {"error": str(e)}

@app.post("/api/embed-image")
async def embed_image(file: UploadFile = File(...)):
    try:
        contents = await file.read()
        image = Image.open(io.BytesIO(contents)).convert("RGB")
        vector = model.encode(image).tolist()
        return {"embedding": vector}
    except Exception as e:
        return {"error": str(e)}