KJ24 commited on
Commit
5583ab1
·
verified ·
1 Parent(s): 5dc76d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -12
app.py CHANGED
@@ -7,11 +7,9 @@ from llama_index.llms.llama_cpp import LlamaCPP
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
  from llama_index.core.node_parser import SemanticSplitterNodeParser
9
 
10
- import os
11
-
12
  app = FastAPI()
13
 
14
- # 🔹 Schéma d'entrée
15
  class ChunkRequest(BaseModel):
16
  text: str
17
  source_id: Optional[str] = None
@@ -19,27 +17,29 @@ class ChunkRequest(BaseModel):
19
  source: Optional[str] = None
20
  type: Optional[str] = None
21
 
22
- # 🔹 Endpoint principal
23
-
24
  @app.post("/chunk")
25
  async def chunk_text(data: ChunkRequest):
 
26
  llm = LlamaCPP(
27
- model_path="/models/mistral-7b-instruct.gguf",
28
  temperature=0.1,
29
  max_new_tokens=512,
30
  context_window=2048,
31
  generate_kwargs={"top_p": 0.95},
32
- model_kwargs={"n_gpu_layers": 1},
33
  )
34
 
 
35
  embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
36
 
 
37
  service_context = ServiceContext.from_defaults(
38
  llm=llm,
39
  embed_model=embed_model
40
  )
41
 
42
  try:
 
43
  parser = SemanticSplitterNodeParser.from_defaults(service_context=service_context)
44
  nodes = parser.get_nodes_from_documents([Document(text=data.text)])
45
 
@@ -53,8 +53,3 @@ async def chunk_text(data: ChunkRequest):
53
  }
54
  except Exception as e:
55
  return {"error": str(e)}
56
-
57
-
58
-
59
-
60
-
 
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
  from llama_index.core.node_parser import SemanticSplitterNodeParser
9
 
 
 
10
  app = FastAPI()
11
 
12
+ # 📥 Modèle de la requête JSON envoyée à /chunk
13
  class ChunkRequest(BaseModel):
14
  text: str
15
  source_id: Optional[str] = None
 
17
  source: Optional[str] = None
18
  type: Optional[str] = None
19
 
 
 
20
  @app.post("/chunk")
21
  async def chunk_text(data: ChunkRequest):
22
+ # ✅ Chargement direct d’un modèle hébergé sur Hugging Face (pas de fichier local .gguf)
23
  llm = LlamaCPP(
24
+ model_url="https://huggingface.co/leafspark/Mistral-7B-Instruct-v0.2-Q4_K_M-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf",
25
  temperature=0.1,
26
  max_new_tokens=512,
27
  context_window=2048,
28
  generate_kwargs={"top_p": 0.95},
29
+ model_kwargs={"n_gpu_layers": 1}, # Laisse 1 si CPU
30
  )
31
 
32
+ # ✅ Embedding open-source via Hugging Face
33
  embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
34
 
35
+ # ✅ Configuration du service IA
36
  service_context = ServiceContext.from_defaults(
37
  llm=llm,
38
  embed_model=embed_model
39
  )
40
 
41
  try:
42
+ # ✅ Découpage sémantique intelligent
43
  parser = SemanticSplitterNodeParser.from_defaults(service_context=service_context)
44
  nodes = parser.get_nodes_from_documents([Document(text=data.text)])
45
 
 
53
  }
54
  except Exception as e:
55
  return {"error": str(e)}