Spaces:
Sleeping
Sleeping
Download main.py from aiwebhook/clip_web_store: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/aiwebhook/clip_web_store/resolve/main/main.py
- Command line
-
hf download hf://spaces/aiwebhook/clip_web_store/main.py
-
curl -L -o main.py https://huggingface.co/spaces/aiwebhook/clip_web_store/resolve/main/main.py
1.39 kB
| 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 | |
| def home(): | |
| return {"message": "سيرفر الذكاء الاصطناعي يعمل بنجاح 🚀"} | |
| def embed_text(req: TextRequest): | |
| try: | |
| vector = model.encode(req.text).tolist() | |
| return {"embedding": vector} | |
| except Exception as e: | |
| return {"error": str(e)} | |
| 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)} |