Spaces:
Runtime error
Runtime error
change
Browse files- __pycache__/main.cpython-313.pyc +0 -0
- main.py +18 -37
__pycache__/main.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-313.pyc and b/__pycache__/main.cpython-313.pyc differ
|
|
|
main.py
CHANGED
|
@@ -1,47 +1,28 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
|
|
|
| 10 |
|
| 11 |
-
model
|
|
|
|
| 12 |
|
| 13 |
-
# Define
|
| 14 |
-
class
|
| 15 |
text: str
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# Define the API endpoint
|
| 23 |
-
@app.post("/embed")
|
| 24 |
-
async def generate_embedding(text_input: TextInput):
|
| 25 |
-
"""
|
| 26 |
-
Generate a 768-dimensional embedding for the input text.
|
| 27 |
-
Returns the embedding in a structured format with rounded values.
|
| 28 |
-
"""
|
| 29 |
-
try:
|
| 30 |
-
# Generate the embedding
|
| 31 |
-
embedding = model.encode(text_input.text, convert_to_tensor=True).cpu().numpy()
|
| 32 |
-
|
| 33 |
-
# Round embedding values to 2 decimal places
|
| 34 |
-
rounded_embedding = np.round(embedding, decimals=2).tolist()
|
| 35 |
-
|
| 36 |
-
# Get the number of dimensions
|
| 37 |
-
dimensions = len(rounded_embedding)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
# Handle any errors
|
| 46 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 4 |
|
| 5 |
+
import os
|
| 6 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 7 |
|
| 8 |
+
# Initialize FastAPI app
|
| 9 |
+
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Load pretrained model from Hugging Face (instead of hf_hub_download)
|
| 12 |
+
model = SentenceTransformer("all-MiniLM-L6-v2") # Updated model
|
| 13 |
|
| 14 |
+
# Define request structure
|
| 15 |
+
class TextRequest(BaseModel):
|
| 16 |
text: str
|
| 17 |
|
| 18 |
+
# Define response structure
|
| 19 |
+
class EmbeddingResponse(BaseModel):
|
| 20 |
+
dimensions: int
|
| 21 |
+
embedding: list[float]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Endpoint to get text embedding
|
| 24 |
+
@app.post("/get_embedding", response_model=EmbeddingResponse)
|
| 25 |
+
async def get_embedding(request: TextRequest):
|
| 26 |
+
embedding = model.encode([request.text])[0] # Generate embedding
|
| 27 |
+
return {"dimensions": len(embedding), "embedding": embedding.tolist()}
|
|
|
|
|
|
|
|
|
|
| 28 |
|