Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,9 @@ from fastapi import FastAPI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import Optional
|
| 4 |
|
| 5 |
-
# ✅ Modules
|
| 6 |
from llama_index.core.settings import Settings
|
| 7 |
-
from llama_index.core import Document
|
| 8 |
from llama_index.llms.llama_cpp import LlamaCPP
|
| 9 |
from llama_index.core.node_parser import SemanticSplitterNodeParser
|
| 10 |
|
|
@@ -24,7 +24,6 @@ os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR
|
|
| 24 |
os.environ["HF_MODULES_CACHE"] = CACHE_DIR
|
| 25 |
os.environ["HF_HUB_CACHE"] = CACHE_DIR
|
| 26 |
|
| 27 |
-
|
| 28 |
# ✅ Configuration du modèle d’embedding local (ex: BGE / Nomic / GTE etc.)
|
| 29 |
MODEL_NAME = "BAAI/bge-small-en-v1.5"
|
| 30 |
|
|
@@ -70,23 +69,15 @@ async def chunk_text(data: ChunkRequest):
|
|
| 70 |
def get_text_embedding(self, text: str):
|
| 71 |
return get_embedding(text)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
llm=llm,
|
| 77 |
-
embed_model=SimpleEmbedding()
|
| 78 |
-
)
|
| 79 |
-
print("✅ ✔️ Settings configurés via ServiceContext (LLM + Embedding)")
|
| 80 |
-
except Exception as e:
|
| 81 |
-
print(f"❌ Erreur dans la configuration des Settings : {e}")
|
| 82 |
-
return {"error": str(e)}
|
| 83 |
|
| 84 |
print("✅ LLM et embedding configurés - prêt pour le split")
|
| 85 |
print("✅ Début du split sémantique...", flush=True)
|
| 86 |
|
| 87 |
# ✅ Utilisation du Semantic Splitter avec le LLM actuel
|
| 88 |
parser = SemanticSplitterNodeParser.from_defaults(llm=llm)
|
| 89 |
-
fallback_splitter = Settings.node_parser # fallback = splitter par défaut
|
| 90 |
|
| 91 |
doc = Document(text=data.text)
|
| 92 |
|
|
@@ -101,10 +92,6 @@ async def chunk_text(data: ChunkRequest):
|
|
| 101 |
print(f"❌ Erreur lors du split sémantique : {e}")
|
| 102 |
return {"error": str(e)}
|
| 103 |
|
| 104 |
-
# Fallback option (non utilisé ici)
|
| 105 |
-
nodes = fallback_splitter.get_nodes_from_documents([doc])
|
| 106 |
-
print(f"⚠️ Split fallback utilisé - chunks générés : {len(nodes)}")
|
| 107 |
-
|
| 108 |
# ✅ Résultat complet pour l’API
|
| 109 |
return {
|
| 110 |
"chunks": [node.text for node in nodes],
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import Optional
|
| 4 |
|
| 5 |
+
# ✅ Modules LlamaIndex – version >= 0.10.0+
|
| 6 |
from llama_index.core.settings import Settings
|
| 7 |
+
from llama_index.core import Document
|
| 8 |
from llama_index.llms.llama_cpp import LlamaCPP
|
| 9 |
from llama_index.core.node_parser import SemanticSplitterNodeParser
|
| 10 |
|
|
|
|
| 24 |
os.environ["HF_MODULES_CACHE"] = CACHE_DIR
|
| 25 |
os.environ["HF_HUB_CACHE"] = CACHE_DIR
|
| 26 |
|
|
|
|
| 27 |
# ✅ Configuration du modèle d’embedding local (ex: BGE / Nomic / GTE etc.)
|
| 28 |
MODEL_NAME = "BAAI/bge-small-en-v1.5"
|
| 29 |
|
|
|
|
| 69 |
def get_text_embedding(self, text: str):
|
| 70 |
return get_embedding(text)
|
| 71 |
|
| 72 |
+
# ✅ Nouvelle configuration (⚠️ ne plus utiliser ServiceContext)
|
| 73 |
+
Settings.llm = llm
|
| 74 |
+
Settings.embed_model = SimpleEmbedding()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
print("✅ LLM et embedding configurés - prêt pour le split")
|
| 77 |
print("✅ Début du split sémantique...", flush=True)
|
| 78 |
|
| 79 |
# ✅ Utilisation du Semantic Splitter avec le LLM actuel
|
| 80 |
parser = SemanticSplitterNodeParser.from_defaults(llm=llm)
|
|
|
|
| 81 |
|
| 82 |
doc = Document(text=data.text)
|
| 83 |
|
|
|
|
| 92 |
print(f"❌ Erreur lors du split sémantique : {e}")
|
| 93 |
return {"error": str(e)}
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# ✅ Résultat complet pour l’API
|
| 96 |
return {
|
| 97 |
"chunks": [node.text for node in nodes],
|