npv2k1 commited on
Commit
4a485db
·
1 Parent(s): ab89dc7

feat: add hello world endpoint with embedding and introduce plot endpoint

Browse files
src/modules/api/routes/v1.py CHANGED
@@ -1,17 +1,21 @@
1
  """API v1 route handlers."""
2
 
3
- from fastapi import APIRouter
4
  from typing import Dict, List
5
  from src.modules.transporter import publish_message
6
-
 
7
  # Create v1 router
8
  router = APIRouter(prefix='/v1', tags=['v1'])
 
9
 
10
  @router.get("/hello")
11
- async def hello_world() -> Dict[str, str]:
12
  """Hello world endpoint."""
13
  publish_message("hello-python", "Hello from FastAPI!")
14
- return {"message": "Hello, World!"}
 
 
15
 
16
  @router.get("/health")
17
  async def health_check() -> Dict[str, str]:
@@ -25,3 +29,24 @@ async def metrics() -> Dict[str, int]:
25
  "total_routes": len(router.routes),
26
  "api_version": 1
27
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """API v1 route handlers."""
2
 
3
+ from fastapi import APIRouter, Response
4
  from typing import Dict, List
5
  from src.modules.transporter import publish_message
6
+ from src.modules.models.index import embed_text
7
+ import io
8
  # Create v1 router
9
  router = APIRouter(prefix='/v1', tags=['v1'])
10
+ import matplotlib.pyplot as plt
11
 
12
  @router.get("/hello")
13
+ async def hello_world():
14
  """Hello world endpoint."""
15
  publish_message("hello-python", "Hello from FastAPI!")
16
+ embed = embed_text("Hello, world!")
17
+ print(f"Generated embedding: {embed}")
18
+ return {"message": "Hello, reloaded!", "embedding": list(embed)}
19
 
20
  @router.get("/health")
21
  async def health_check() -> Dict[str, str]:
 
29
  "total_routes": len(router.routes),
30
  "api_version": 1
31
  }
32
+
33
+ @router.get("/plot")
34
+ async def get_plot():
35
+ # Tạo biểu đồ
36
+ plt.figure(figsize=(6, 4))
37
+ x = [1, 2, 3, 4, 5]
38
+ y = [i ** 2 for i in x]
39
+ plt.plot(x, y, label="y = x^2")
40
+ plt.title("Sample Plot")
41
+ plt.xlabel("x")
42
+ plt.ylabel("y")
43
+ plt.legend()
44
+
45
+ # Lưu vào buffer
46
+ buf = io.BytesIO()
47
+ plt.savefig(buf, format="png")
48
+ plt.close()
49
+ buf.seek(0)
50
+
51
+ # Trả về dưới dạng ảnh PNG
52
+ return Response(content=buf.getvalue(), media_type="image/png")
src/modules/models/__init__.py ADDED
File without changes
src/modules/models/index.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from sentence_transformers import SentenceTransformer
3
+
4
+ # Load embedding model once when the server starts
5
+ model = SentenceTransformer("all-MiniLM-L6-v2")
6
+
7
+ def embed_text(req):
8
+ embedding = model.encode(req).tolist()
9
+ return embedding
10
+