Spaces:
Sleeping
Sleeping
MohitGupta41 commited on
Commit ·
10a955c
1
Parent(s): 8d5a4b2
FastAPI RAG backend (Docker)
Browse files
app.py
CHANGED
|
@@ -28,6 +28,9 @@ class AskRequest(BaseModel):
|
|
| 28 |
question: str
|
| 29 |
top_k: int = 3
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@app.post("/ingest")
|
| 33 |
async def ingest(file: UploadFile = File(...)) -> Dict[str, Any]:
|
|
|
|
| 28 |
question: str
|
| 29 |
top_k: int = 3
|
| 30 |
|
| 31 |
+
@app.get("/")
|
| 32 |
+
def root():
|
| 33 |
+
return {"status": "ok", "service": "rag-backend", "docs": "/docs"}
|
| 34 |
|
| 35 |
@app.post("/ingest")
|
| 36 |
async def ingest(file: UploadFile = File(...)) -> Dict[str, Any]:
|
rag.py
CHANGED
|
@@ -22,8 +22,8 @@ EMBEDDER = SentenceTransformer("all-MiniLM-L6-v2")
|
|
| 22 |
# For MVP: use a smallish instruct model if possible
|
| 23 |
# NOTE: Mistral 7B is heavy; if you can't run it locally, use a smaller HF model.
|
| 24 |
GENERATOR = pipeline(
|
| 25 |
-
"
|
| 26 |
-
model="google/flan-t5-base",
|
| 27 |
max_new_tokens=256
|
| 28 |
)
|
| 29 |
|
|
@@ -80,13 +80,8 @@ def generate_answer(question: str, context: str) -> str:
|
|
| 80 |
f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"
|
| 81 |
)
|
| 82 |
|
| 83 |
-
# For flan-t5 pipeline: use text2text-generation instead normally,
|
| 84 |
-
# but keeping this simple - if needed swap pipeline.
|
| 85 |
out = GENERATOR(prompt)
|
| 86 |
-
|
| 87 |
-
if isinstance(out, list) and out and "generated_text" in out[0]:
|
| 88 |
-
return out[0]["generated_text"]
|
| 89 |
-
return str(out)
|
| 90 |
|
| 91 |
|
| 92 |
def create_session(chunks: List[str]) -> str:
|
|
|
|
| 22 |
# For MVP: use a smallish instruct model if possible
|
| 23 |
# NOTE: Mistral 7B is heavy; if you can't run it locally, use a smaller HF model.
|
| 24 |
GENERATOR = pipeline(
|
| 25 |
+
"text2text-generation",
|
| 26 |
+
model="google/flan-t5-base",
|
| 27 |
max_new_tokens=256
|
| 28 |
)
|
| 29 |
|
|
|
|
| 80 |
f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"
|
| 81 |
)
|
| 82 |
|
|
|
|
|
|
|
| 83 |
out = GENERATOR(prompt)
|
| 84 |
+
return out[0]["generated_text"].strip()
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
def create_session(chunks: List[str]) -> str:
|