Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- Dockerfile +1 -4
- app/config.py +1 -1
- app/main.py +18 -3
- app/static/styles.css +27 -0
- app/templates/chat.html +85 -0
- requirements.txt +2 -1
Dockerfile
CHANGED
@@ -3,14 +3,11 @@ 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 |
|
11 |
-
RUN mkdir -p .cache/hub
|
12 |
-
RUN chmod -R 777 .cache/hub
|
13 |
-
|
14 |
# Copy requirements first to leverage Docker cache
|
15 |
COPY requirements.txt .
|
16 |
|
|
|
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 .
|
13 |
|
app/config.py
CHANGED
@@ -2,7 +2,7 @@ from pydantic_settings import BaseSettings
|
|
2 |
|
3 |
class Settings(BaseSettings):
|
4 |
HUGGINGFACE_TOKEN: str
|
5 |
-
MODEL_NAME: str = "deepseek-ai/
|
6 |
|
7 |
class Config:
|
8 |
env_file = ".env"
|
|
|
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,7 +1,11 @@
|
|
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",
|
@@ -9,6 +13,10 @@ app = FastAPI(
|
|
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(
|
@@ -18,10 +26,17 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
18 |
device_map="auto"
|
19 |
)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
@app.post("/chat")
|
22 |
-
async def chat(message:
|
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)
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.templating import Jinja2Templates
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.responses import HTMLResponse
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
import torch
|
7 |
from .config import settings
|
8 |
+
from pydantic import BaseModel
|
9 |
|
10 |
app = FastAPI(
|
11 |
title="Deepseek Chat API",
|
|
|
13 |
version="1.0.0"
|
14 |
)
|
15 |
|
16 |
+
# Mount static files and templates
|
17 |
+
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
18 |
+
templates = Jinja2Templates(directory="app/templates")
|
19 |
+
|
20 |
# Initialize model and tokenizer
|
21 |
tokenizer = AutoTokenizer.from_pretrained(settings.MODEL_NAME, token=settings.HUGGINGFACE_TOKEN)
|
22 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
26 |
device_map="auto"
|
27 |
)
|
28 |
|
29 |
+
class ChatMessage(BaseModel):
|
30 |
+
message: str
|
31 |
+
|
32 |
+
@app.get("/", response_class=HTMLResponse)
|
33 |
+
async def home(request: Request):
|
34 |
+
return templates.TemplateResponse("chat.html", {"request": request})
|
35 |
+
|
36 |
@app.post("/chat")
|
37 |
+
async def chat(message: ChatMessage):
|
38 |
# Prepare the prompt
|
39 |
+
prompt = f"### Instruction: {message.message}\n\n### Response:"
|
40 |
|
41 |
# Generate response
|
42 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
app/static/styles.css
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* Custom scrollbar */
|
2 |
+
#chat-messages::-webkit-scrollbar {
|
3 |
+
width: 6px;
|
4 |
+
}
|
5 |
+
|
6 |
+
#chat-messages::-webkit-scrollbar-track {
|
7 |
+
background: #f1f1f1;
|
8 |
+
}
|
9 |
+
|
10 |
+
#chat-messages::-webkit-scrollbar-thumb {
|
11 |
+
background: #888;
|
12 |
+
border-radius: 3px;
|
13 |
+
}
|
14 |
+
|
15 |
+
#chat-messages::-webkit-scrollbar-thumb:hover {
|
16 |
+
background: #666;
|
17 |
+
}
|
18 |
+
|
19 |
+
/* Message animations */
|
20 |
+
@keyframes fadeIn {
|
21 |
+
from { opacity: 0; transform: translateY(10px); }
|
22 |
+
to { opacity: 1; transform: translateY(0); }
|
23 |
+
}
|
24 |
+
|
25 |
+
#chat-messages > div {
|
26 |
+
animation: fadeIn 0.3s ease-out forwards;
|
27 |
+
}
|
app/templates/chat.html
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Deepseek Chat</title>
|
7 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
|
8 |
+
<link rel="stylesheet" href="{{ url_for('static', path='/styles.css') }}">
|
9 |
+
</head>
|
10 |
+
<body class="bg-gray-100 h-screen">
|
11 |
+
<div class="container mx-auto px-4 h-screen flex flex-col">
|
12 |
+
<header class="py-4">
|
13 |
+
<h1 class="text-2xl font-bold text-gray-800">Deepseek Chat</h1>
|
14 |
+
</header>
|
15 |
+
|
16 |
+
<div class="flex-1 bg-white rounded-lg shadow-lg flex flex-col">
|
17 |
+
<div id="chat-messages" class="flex-1 p-4 overflow-y-auto space-y-4">
|
18 |
+
<!-- Messages will be inserted here -->
|
19 |
+
</div>
|
20 |
+
|
21 |
+
<div class="border-t p-4">
|
22 |
+
<form id="chat-form" class="flex space-x-4">
|
23 |
+
<input type="text"
|
24 |
+
id="message-input"
|
25 |
+
class="flex-1 rounded-lg border border-gray-300 px-4 py-2 focus:outline-none focus:border-blue-500"
|
26 |
+
placeholder="Type your message...">
|
27 |
+
<button type="submit"
|
28 |
+
class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600 focus:outline-none">
|
29 |
+
Send
|
30 |
+
</button>
|
31 |
+
</form>
|
32 |
+
</div>
|
33 |
+
</div>
|
34 |
+
</div>
|
35 |
+
|
36 |
+
<script>
|
37 |
+
const chatMessages = document.getElementById('chat-messages');
|
38 |
+
const chatForm = document.getElementById('chat-form');
|
39 |
+
const messageInput = document.getElementById('message-input');
|
40 |
+
|
41 |
+
function appendMessage(content, isUser) {
|
42 |
+
const messageDiv = document.createElement('div');
|
43 |
+
messageDiv.className = `flex ${isUser ? 'justify-end' : 'justify-start'}`;
|
44 |
+
|
45 |
+
const bubble = document.createElement('div');
|
46 |
+
bubble.className = `max-w-[70%] rounded-lg p-3 ${
|
47 |
+
isUser ? 'bg-blue-500 text-white' : 'bg-gray-100 text-gray-800'
|
48 |
+
}`;
|
49 |
+
bubble.textContent = content;
|
50 |
+
|
51 |
+
messageDiv.appendChild(bubble);
|
52 |
+
chatMessages.appendChild(messageDiv);
|
53 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
54 |
+
}
|
55 |
+
|
56 |
+
chatForm.addEventListener('submit', async (e) => {
|
57 |
+
e.preventDefault();
|
58 |
+
const message = messageInput.value.trim();
|
59 |
+
if (!message) return;
|
60 |
+
|
61 |
+
// Clear input
|
62 |
+
messageInput.value = '';
|
63 |
+
|
64 |
+
// Add user message
|
65 |
+
appendMessage(message, true);
|
66 |
+
|
67 |
+
try {
|
68 |
+
const response = await fetch('/chat', {
|
69 |
+
method: 'POST',
|
70 |
+
headers: {
|
71 |
+
'Content-Type': 'application/json',
|
72 |
+
},
|
73 |
+
body: JSON.stringify({ message }),
|
74 |
+
});
|
75 |
+
|
76 |
+
const data = await response.json();
|
77 |
+
appendMessage(data.response, false);
|
78 |
+
} catch (error) {
|
79 |
+
appendMessage('Sorry, something went wrong. Please try again.', false);
|
80 |
+
console.error('Error:', error);
|
81 |
+
}
|
82 |
+
});
|
83 |
+
</script>
|
84 |
+
</body>
|
85 |
+
</html>
|
requirements.txt
CHANGED
@@ -5,4 +5,5 @@ pydantic-settings
|
|
5 |
python-dotenv
|
6 |
transformers
|
7 |
accelerate>=0.26.0
|
8 |
-
torch
|
|
|
|
5 |
python-dotenv
|
6 |
transformers
|
7 |
accelerate>=0.26.0
|
8 |
+
torch
|
9 |
+
jinja2
|