tharu22 commited on
Commit
e83c648
·
1 Parent(s): 385f9be
Files changed (2) hide show
  1. __pycache__/main.cpython-313.pyc +0 -0
  2. 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, HTTPException
2
  from pydantic import BaseModel
3
  from sentence_transformers import SentenceTransformer
4
- import numpy as np
5
 
6
- # Initialize the FastAPI app
7
- app = FastAPI()
8
 
9
- # Load the pre-trained SentenceTransformer model from Hugging Face
 
10
 
11
- model=SentenceTransformer("all-MiniLM-L6-V2")
 
12
 
13
- # Define the request body schema
14
- class TextInput(BaseModel):
15
  text: str
16
 
17
- # Home route
18
- @app.get("/")
19
- async def home():
20
- return {"message": "Welcome to embedding SMS API, use /docs to post SMS text and get dimensions"}
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
- # Return structured response
40
- return {
41
- "dimensions": dimensions,
42
- "embeddings": [rounded_embedding] # Wrap the embedding inside a list
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