Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +1 -2
- app/config.py +1 -3
- app/main.py +33 -34
- requirements.txt +2 -9
Dockerfile
CHANGED
@@ -3,11 +3,10 @@ FROM python:3.10-slim
|
|
3 |
WORKDIR /app
|
4 |
|
5 |
# Install system dependencies
|
6 |
-
RUN apt-get update &&
|
7 |
git \
|
8 |
&& rm -rf /var/lib/apt/lists/*
|
9 |
|
10 |
-
RUN mkdir cacheddir_joblib
|
11 |
|
12 |
# Copy requirements first to leverage Docker cache
|
13 |
COPY requirements.txt .
|
|
|
3 |
WORKDIR /app
|
4 |
|
5 |
# Install system dependencies
|
6 |
+
RUN sudo apt-get update && sudapt-get install -y \
|
7 |
git \
|
8 |
&& rm -rf /var/lib/apt/lists/*
|
9 |
|
|
|
10 |
|
11 |
# Copy requirements first to leverage Docker cache
|
12 |
COPY requirements.txt .
|
app/config.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
from pydantic_settings import BaseSettings
|
2 |
|
3 |
class Settings(BaseSettings):
|
4 |
-
MONGODB_URL: str
|
5 |
-
COLLECTION_NAME: str = "documents"
|
6 |
-
DATABASE_NAME: str = "ragbot"
|
7 |
HUGGINGFACE_TOKEN: str
|
|
|
8 |
|
9 |
class Config:
|
10 |
env_file = ".env"
|
|
|
1 |
from pydantic_settings import BaseSettings
|
2 |
|
3 |
class Settings(BaseSettings):
|
|
|
|
|
|
|
4 |
HUGGINGFACE_TOKEN: str
|
5 |
+
MODEL_NAME: str = "deepseek-ai/deepseek-coder-33b-instruct"
|
6 |
|
7 |
class Config:
|
8 |
env_file = ".env"
|
app/main.py
CHANGED
@@ -1,44 +1,43 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
# Apply patch before importing DSPy
|
6 |
-
patch_dspy_cache()
|
7 |
-
|
8 |
-
from .database.mongodb import db
|
9 |
-
from .rag.document_processor import DocumentProcessor
|
10 |
-
from .rag.retriever import RAGRetriever
|
11 |
-
|
12 |
-
@asynccontextmanager
|
13 |
-
async def lifespan(app: FastAPI):
|
14 |
-
# Startup
|
15 |
-
await db.connect()
|
16 |
-
yield
|
17 |
-
# Shutdown
|
18 |
-
await db.close()
|
19 |
|
20 |
app = FastAPI(
|
21 |
-
title="
|
22 |
-
description="A
|
23 |
version="1.0.0"
|
24 |
)
|
25 |
-
document_processor = DocumentProcessor()
|
26 |
-
rag_retriever = RAGRetriever()
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
@app.post("/chat")
|
35 |
-
async def chat(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
import uvicorn
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
from .config import settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
app = FastAPI(
|
7 |
+
title="Deepseek Chat API",
|
8 |
+
description="A simple chat API using DeepSeek model",
|
9 |
version="1.0.0"
|
10 |
)
|
|
|
|
|
11 |
|
12 |
+
# Initialize model and tokenizer
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(settings.MODEL_NAME, token=settings.HUGGINGFACE_TOKEN)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
settings.MODEL_NAME,
|
16 |
+
token=settings.HUGGINGFACE_TOKEN,
|
17 |
+
torch_dtype=torch.float16,
|
18 |
+
device_map="auto"
|
19 |
+
)
|
20 |
|
21 |
@app.post("/chat")
|
22 |
+
async def chat(message: str):
|
23 |
+
# Prepare the prompt
|
24 |
+
prompt = f"### Instruction: {message}\n\n### Response:"
|
25 |
+
|
26 |
+
# Generate response
|
27 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
28 |
+
outputs = model.generate(
|
29 |
+
**inputs,
|
30 |
+
max_new_tokens=512,
|
31 |
+
temperature=0.7,
|
32 |
+
do_sample=True,
|
33 |
+
pad_token_id=tokenizer.eos_token_id
|
34 |
+
)
|
35 |
+
|
36 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
+
# Extract only the response part
|
38 |
+
response = response.split("### Response:")[-1].strip()
|
39 |
+
|
40 |
+
return {"response": response}
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
import uvicorn
|
requirements.txt
CHANGED
@@ -1,15 +1,8 @@
|
|
1 |
fastapi
|
2 |
uvicorn
|
3 |
-
motor
|
4 |
-
pymongo
|
5 |
-
python-multipart
|
6 |
-
PyPDF2
|
7 |
-
dspy-ai
|
8 |
pydantic
|
9 |
pydantic-settings
|
10 |
python-dotenv
|
11 |
-
sentence-transformers
|
12 |
-
numpy
|
13 |
transformers
|
14 |
-
|
15 |
-
|
|
|
1 |
fastapi
|
2 |
uvicorn
|
|
|
|
|
|
|
|
|
|
|
3 |
pydantic
|
4 |
pydantic-settings
|
5 |
python-dotenv
|
|
|
|
|
6 |
transformers
|
7 |
+
accelerate>=0.26.0
|
8 |
+
torch
|