[Vignesh Nachu] commited on
Commit
60956da
·
1 Parent(s): 657f9f6
Files changed (3) hide show
  1. Dockerfile +25 -0
  2. app.py +69 -0
  3. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a slim Python 3.11 base image
2
+ FROM python:3.13-slim
3
+
4
+ # Avoid writing .pyc files and enable unbuffered stdout/stderr
5
+ ENV PYTHONDONTWRITEBYTECODE=1
6
+ ENV PYTHONUNBUFFERED=1
7
+
8
+ # Set the working directory
9
+ WORKDIR /app
10
+
11
+ RUN apt-get update && apt-get install -y \
12
+ build-essential \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+
16
+ COPY requirements.txt .
17
+ RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+
20
+ COPY . .
21
+
22
+
23
+ EXPOSE 7860
24
+
25
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from uuid import uuid4
3
+ from fastapi import FastAPI
4
+ from fastapi.responses import FileResponse
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from fastapi.staticfiles import StaticFiles
7
+ from pydantic import BaseModel, Field
8
+ from sentence_transformers import SentenceTransformer
9
+ import weaviate
10
+ from weaviate.classes.init import Auth
11
+
12
+ WEAVIATE_URL = os.getenv("WEAVIATE_URL", "https://hrdhwtqlrkqmc8sfizwvpq.c0.asia-southeast1.gcp.weaviate.cloud")
13
+ WEAVIATE_API_KEY = os.getenv("WEAVIATE_API_KEY", "pMDX7ysJPkSTUMdV3gEwxhGmyB7wB301fLaJ")
14
+ CLASS_NAME = "PdfChunk"
15
+
16
+ app = FastAPI()
17
+
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
+
25
+ app.mount("/static", StaticFiles(directory="static"), name="static")
26
+
27
+ model = SentenceTransformer("all-MiniLM-L6-v2")
28
+
29
+ client = weaviate.connect_to_weaviate_cloud(
30
+ cluster_url=WEAVIATE_URL,
31
+ auth_credentials=Auth.api_key(WEAVIATE_API_KEY),
32
+ )
33
+
34
+ class QueryRequest(BaseModel):
35
+ text: str = Field(..., min_length=1)
36
+ top_k: int = Field(default=7, ge=1, le=100)
37
+
38
+ @app.get("/")
39
+ def root():
40
+ return {"message": "PDF RAG API is running. Use POST / to query."}
41
+
42
+ @app.get("/openapi.yaml")
43
+ def serve_openapi_yaml():
44
+ return FileResponse("static/openapi.yaml", media_type="text/yaml")
45
+
46
+ @app.post("/")
47
+ def query_weaviate(q: QueryRequest):
48
+ try:
49
+ query_vector = model.encode(q.text).tolist()
50
+ collection = client.collections.get(CLASS_NAME)
51
+
52
+ results = collection.query.near_vector(
53
+ near_vector=query_vector,
54
+ limit=q.top_k,
55
+ )
56
+
57
+ return {
58
+ "query": q.text,
59
+ "results": [
60
+ {
61
+ "text": obj.properties.get("text", ""),
62
+ "source": obj.properties.get("source", ""),
63
+ }
64
+ for obj in results.objects
65
+ ]
66
+ }
67
+
68
+ except Exception as e:
69
+ return {"error": str(e)}
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pydantic
4
+ sentence-transformers
5
+ weaviate-client