moraxgiga commited on
Commit
3f995e2
1 Parent(s): fcd50c4

Update TextGen/router.py

Browse files
Files changed (1) hide show
  1. TextGen/router.py +25 -0
TextGen/router.py CHANGED
@@ -1,6 +1,26 @@
1
  from pydantic import BaseModel
2
  from fastapi.middleware.cors import CORSMiddleware
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  from TextGen import app
6
 
@@ -15,3 +35,8 @@ app.add_middleware(
15
  @app.get("/", tags=["Home"])
16
  def api_home():
17
  return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
 
 
 
 
 
 
1
  from pydantic import BaseModel
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from transformers import AutoModel
4
+ from typing import List
5
+ import os, platform, time
6
 
7
+ # Your model loading and app initialization code
8
+ model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-en', trust_remote_code=True)
9
+
10
+ class Validation(BaseModel):
11
+ prompt: List[str]
12
+
13
+ def generate_embeddings(x):
14
+ start_time = time.time()
15
+ embeddings = model.encode(x).tolist()
16
+ end_time = time.time()
17
+ time_taken = end_time - start_time # Calculate the time taken
18
+
19
+ return {
20
+ "embeddings": embeddings,
21
+ "time_taken": f"{time_taken:.2f} seconds",
22
+ "Number_of_sentence_processed": len(x)
23
+ }
24
 
25
  from TextGen import app
26
 
 
35
  @app.get("/", tags=["Home"])
36
  def api_home():
37
  return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
38
+
39
+
40
+ @app.post("/api/generate", summary="Generate embeddings", tags=["Generate"], response_model=Validation)
41
+ def inference(input_prompt: List[str]):
42
+ return generate_embeddings(prompt=input_prompt)