Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- Dockerfile +22 -0
- app.py +36 -0
- requirements.txt +2 -0
- templates/index.html +75 -0
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install system dependencies
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
build-essential \
|
| 8 |
+
cmake \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
# Copy requirements and install Python dependencies
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
# Copy the application code
|
| 16 |
+
COPY . .
|
| 17 |
+
|
| 18 |
+
# Expose the port the app runs on
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
# Command to run the application
|
| 22 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Initialize the LLM
|
| 8 |
+
llm = Llama.from_pretrained(
|
| 9 |
+
repo_id="unsloth/DeepSeek-R1-Distill-Qwen-1.5B-GGUF",
|
| 10 |
+
filename="DeepSeek-R1-Distill-Qwen-1.5B-Q2_K.gguf",
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
@app.route('/')
|
| 14 |
+
def index():
|
| 15 |
+
return render_template('index.html')
|
| 16 |
+
|
| 17 |
+
@app.route('/chat', methods=['POST'])
|
| 18 |
+
def chat():
|
| 19 |
+
data = request.json
|
| 20 |
+
message = data.get('message', '')
|
| 21 |
+
|
| 22 |
+
# Format the message for chat completion
|
| 23 |
+
messages = [
|
| 24 |
+
{"role": "user", "content": message}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = llm.create_chat_completion(messages=messages)
|
| 29 |
+
return jsonify({
|
| 30 |
+
"response": response['choices'][0]['message']['content']
|
| 31 |
+
})
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return jsonify({"error": str(e)}), 500
|
| 34 |
+
|
| 35 |
+
if __name__ == '__main__':
|
| 36 |
+
app.run(host='0.0.0.0', port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
llama-cpp-python
|
templates/index.html
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<head>
|
| 3 |
+
<title>LLM Chat</title>
|
| 4 |
+
<style>
|
| 5 |
+
#chat-container {
|
| 6 |
+
width: 80%;
|
| 7 |
+
margin: 20px auto;
|
| 8 |
+
padding: 20px;
|
| 9 |
+
}
|
| 10 |
+
#chat-messages {
|
| 11 |
+
height: 400px;
|
| 12 |
+
border: 1px solid #ccc;
|
| 13 |
+
overflow-y: auto;
|
| 14 |
+
margin-bottom: 20px;
|
| 15 |
+
padding: 10px;
|
| 16 |
+
}
|
| 17 |
+
#message-input {
|
| 18 |
+
width: 80%;
|
| 19 |
+
padding: 10px;
|
| 20 |
+
}
|
| 21 |
+
#send-button {
|
| 22 |
+
padding: 10px 20px;
|
| 23 |
+
}
|
| 24 |
+
</style>
|
| 25 |
+
</head>
|
| 26 |
+
<body>
|
| 27 |
+
<div id="chat-container">
|
| 28 |
+
<div id="chat-messages"></div>
|
| 29 |
+
<input type="text" id="message-input" placeholder="Type your message...">
|
| 30 |
+
<button id="send-button">Send</button>
|
| 31 |
+
</div>
|
| 32 |
+
|
| 33 |
+
<script>
|
| 34 |
+
const messageInput = document.getElementById('message-input');
|
| 35 |
+
const sendButton = document.getElementById('send-button');
|
| 36 |
+
const chatMessages = document.getElementById('chat-messages');
|
| 37 |
+
|
| 38 |
+
async function sendMessage() {
|
| 39 |
+
const message = messageInput.value.trim();
|
| 40 |
+
if (!message) return;
|
| 41 |
+
|
| 42 |
+
// Add user message to chat
|
| 43 |
+
appendMessage('User: ' + message);
|
| 44 |
+
messageInput.value = '';
|
| 45 |
+
|
| 46 |
+
try {
|
| 47 |
+
const response = await fetch('/chat', {
|
| 48 |
+
method: 'POST',
|
| 49 |
+
headers: {
|
| 50 |
+
'Content-Type': 'application/json',
|
| 51 |
+
},
|
| 52 |
+
body: JSON.stringify({ message: message }),
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
const data = await response.json();
|
| 56 |
+
appendMessage('AI: ' + data.response);
|
| 57 |
+
} catch (error) {
|
| 58 |
+
appendMessage('Error: Failed to get response');
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
function appendMessage(message) {
|
| 63 |
+
const messageElement = document.createElement('div');
|
| 64 |
+
messageElement.textContent = message;
|
| 65 |
+
chatMessages.appendChild(messageElement);
|
| 66 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
sendButton.addEventListener('click', sendMessage);
|
| 70 |
+
messageInput.addEventListener('keypress', (e) => {
|
| 71 |
+
if (e.key === 'Enter') sendMessage();
|
| 72 |
+
});
|
| 73 |
+
</script>
|
| 74 |
+
</body>
|
| 75 |
+
</html>
|