File size: 968 Bytes
d55d359
 
dc098d6
d55d359
106f076
d55d359
 
de95cf8
d55d359
 
 
 
 
 
 
e9a8141
d55d359
653b524
de95cf8
106f076
d55d359
106f076
d55d359
 
 
 
 
106f076
de95cf8
c30f4e3
d55d359
 
f43dea4
d55d359
f43dea4
 
 
 
d55d359
f43dea4
d55d359
 
f43dea4
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
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from sentence_transformers import SentenceTransformer
import uvicorn

# Initialize with root_path (MANDATORY FOR SPACES)
app = FastAPI(root_path="/proxy")  # 👈 THIS IS CRITICAL

# CORS (required for Spaces)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load model (simplified)
model = SentenceTransformer('BAAI/bge-small-zh')

@app.get("/")
def home():
    return {
        "message": "API is working!",
        "endpoints": {
            "embed": "POST /embed",
            "docs": "/docs"
        }
    }

@app.post("/embed")
async def embed_text(request: Request):
    data = await request.json()
    return {
        "embedding": model.encode(data['text']).tolist()
    }

if __name__ == "__main__":
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=7860,
        reload=False
    )