Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from llama_index.core import Document, ServiceContext
|
4 |
+
from llama_index.llms.openai import OpenAI
|
5 |
+
from llama_index.core.node_parser import SemanticSplitterNodeParser
|
6 |
+
import os
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Requête d'entrée
|
11 |
+
class ChunkRequest(BaseModel):
|
12 |
+
text: str
|
13 |
+
|
14 |
+
# Réponse
|
15 |
+
@app.post("/chunk")
|
16 |
+
async def chunk_text(data: ChunkRequest):
|
17 |
+
llm = OpenAI(
|
18 |
+
model="meta-llama/llama-4-maverick:free",
|
19 |
+
api_base="https://openrouter.ai/api/v1",
|
20 |
+
api_key=os.getenv("OPENROUTER_API_KEY")
|
21 |
+
)
|
22 |
+
|
23 |
+
service_context = ServiceContext.from_defaults(llm=llm)
|
24 |
+
|
25 |
+
try:
|
26 |
+
parser = SemanticSplitterNodeParser.from_defaults(service_context=service_context)
|
27 |
+
nodes = parser.get_nodes_from_documents([Document(text=data.text)])
|
28 |
+
return {"chunks": [node.text for node in nodes]}
|
29 |
+
except Exception as e:
|
30 |
+
return {"error": str(e)}
|