init template
Browse files- Dockerfile +32 -0
- main.py +33 -0
- requirements.txt +8 -0
- static/index.html +22 -0
- static/script.js +52 -0
- static/style.css +48 -0
Dockerfile
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.8
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
# Upgrade pip
|
| 6 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 7 |
+
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 11 |
+
|
| 12 |
+
# download model from transformer
|
| 13 |
+
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Set up a new user named "user" with user ID 1000
|
| 17 |
+
RUN useradd -m -u 1000 user
|
| 18 |
+
|
| 19 |
+
# Switch to the "user" user
|
| 20 |
+
USER user
|
| 21 |
+
|
| 22 |
+
# Set home to the user's home directory
|
| 23 |
+
ENV HOME=/home/user \
|
| 24 |
+
PATH=/home/user/.local/bin:$PATH
|
| 25 |
+
|
| 26 |
+
# Set the working directory to the user's home directory
|
| 27 |
+
WORKDIR $HOME/app
|
| 28 |
+
|
| 29 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 30 |
+
COPY --chown=user . $HOME/app
|
| 31 |
+
|
| 32 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Depends, HTTPException, Query
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from typing import List
|
| 4 |
+
from fastapi.responses import HTMLResponse
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 10 |
+
|
| 11 |
+
@app.get("/", response_class=HTMLResponse)
|
| 12 |
+
async def read_root():
|
| 13 |
+
with open("static/index.html", "r") as f:
|
| 14 |
+
content = f.read()
|
| 15 |
+
return HTMLResponse(content=content)
|
| 16 |
+
|
| 17 |
+
# Initialize model and tokenizer
|
| 18 |
+
# tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-14B-Chat-int4")
|
| 19 |
+
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-14B-Chat-int4").eval()
|
| 20 |
+
|
| 21 |
+
@app.post("/chat/")
|
| 22 |
+
def chat(user_input: str, api_key: str):
|
| 23 |
+
# Here you can validate the API key, e.g., check if it exists in your database
|
| 24 |
+
# If the API key is not valid, raise an HTTPException
|
| 25 |
+
# if not validate_api_key(api_key):
|
| 26 |
+
# raise HTTPException(status_code=400, detail="Invalid API key")
|
| 27 |
+
|
| 28 |
+
# Tokenize the user input and get model's response
|
| 29 |
+
# input_ids = tokenizer.encode(user_input, return_tensors="pt")
|
| 30 |
+
# output = model.generate(input_ids)
|
| 31 |
+
# response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 32 |
+
|
| 33 |
+
return {"response": f"user input: {user_input}, api_key: {api_key}"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==0.28.0
|
| 2 |
+
pandas==2.0.3
|
| 3 |
+
tqdm~=4.66.1
|
| 4 |
+
annoy==1.17.3
|
| 5 |
+
fastapi
|
| 6 |
+
uvicorn
|
| 7 |
+
transformers
|
| 8 |
+
torch
|
static/index.html
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
<link rel="stylesheet" href="/static/style.css">
|
| 7 |
+
<script src="/static/script.js" defer></script>
|
| 8 |
+
<title>ASK-ANRG</title>
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<div class="container">
|
| 12 |
+
<h1>ASK-ANRG</h1>
|
| 13 |
+
<label for="api-key-input">API Key:</label>
|
| 14 |
+
<input type="text" id="api-key-input" placeholder="Enter your API key...">
|
| 15 |
+
<div class="chat-box" id="chat-box">
|
| 16 |
+
<!-- Chat messages will be displayed here -->
|
| 17 |
+
</div>
|
| 18 |
+
<input type="text" id="user-input" placeholder="Type your message...">
|
| 19 |
+
<button id="send-btn">Send</button>
|
| 20 |
+
</div>
|
| 21 |
+
</body>
|
| 22 |
+
</html>
|
static/script.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const sendBtn = document.getElementById('send-btn');
|
| 2 |
+
const userInput = document.getElementById('user-input');
|
| 3 |
+
const apiKeyInput = document.getElementById('api-key-input');
|
| 4 |
+
const chatBox = document.getElementById('chat-box');
|
| 5 |
+
|
| 6 |
+
sendBtn.addEventListener('click', () => {
|
| 7 |
+
const message = userInput.value.trim();
|
| 8 |
+
const apiKey = apiKeyInput.value.trim();
|
| 9 |
+
console.log("message: ", message)
|
| 10 |
+
console.log("apiKey: ", apiKey)
|
| 11 |
+
|
| 12 |
+
if (message && apiKey) {
|
| 13 |
+
// Display user's message immediately
|
| 14 |
+
const userMessageDiv = document.createElement('div');
|
| 15 |
+
userMessageDiv.className = 'user-message';
|
| 16 |
+
userMessageDiv.innerText = message;
|
| 17 |
+
chatBox.appendChild(userMessageDiv);
|
| 18 |
+
scrollToBottom();
|
| 19 |
+
|
| 20 |
+
// Call FastAPI backend to get response
|
| 21 |
+
fetch('/chat/', {
|
| 22 |
+
method: 'POST',
|
| 23 |
+
headers: {
|
| 24 |
+
'Content-Type': 'application/json'
|
| 25 |
+
},
|
| 26 |
+
body: JSON.stringify({user_input: message, api_key: apiKey})
|
| 27 |
+
})
|
| 28 |
+
.then(response => {
|
| 29 |
+
if (!response.ok) {
|
| 30 |
+
throw new Error('Network response was not ok');
|
| 31 |
+
}
|
| 32 |
+
return response.json();
|
| 33 |
+
})
|
| 34 |
+
.then(data => {
|
| 35 |
+
// Display chatbot's response
|
| 36 |
+
const botMessageDiv = document.createElement('div');
|
| 37 |
+
botMessageDiv.className = 'bot-message';
|
| 38 |
+
botMessageDiv.innerText = data.response;
|
| 39 |
+
chatBox.appendChild(botMessageDiv);
|
| 40 |
+
scrollToBottom();
|
| 41 |
+
})
|
| 42 |
+
.catch(error => {
|
| 43 |
+
console.log('There was a problem with the fetch operation:', error.message);
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
userInput.value = ''; // Clear the input field
|
| 47 |
+
}
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
function scrollToBottom() {
|
| 51 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 52 |
+
}
|
static/style.css
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
font-family: Arial, sans-serif;
|
| 3 |
+
background-color: #f4f4f4;
|
| 4 |
+
margin: 0;
|
| 5 |
+
padding: 0;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
.container {
|
| 9 |
+
max-width: 600px;
|
| 10 |
+
margin: 50px auto;
|
| 11 |
+
background-color: #fff;
|
| 12 |
+
padding: 20px;
|
| 13 |
+
border-radius: 5px;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
.chat-box {
|
| 17 |
+
border: 1px solid #e0e0e0;
|
| 18 |
+
padding: 20px;
|
| 19 |
+
height: 300px;
|
| 20 |
+
overflow-y: scroll;
|
| 21 |
+
margin-bottom: 10px;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
button {
|
| 25 |
+
background-color: #007BFF;
|
| 26 |
+
color: #fff;
|
| 27 |
+
border: none;
|
| 28 |
+
padding: 10px 15px;
|
| 29 |
+
border-radius: 5px;
|
| 30 |
+
cursor: pointer;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.user-message, .bot-message {
|
| 34 |
+
margin: 10px 0;
|
| 35 |
+
padding: 10px;
|
| 36 |
+
border-radius: 5px;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
.user-message {
|
| 40 |
+
background-color: #e0e0e0;
|
| 41 |
+
align-self: flex-end;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.bot-message {
|
| 45 |
+
background-color: #007BFF;
|
| 46 |
+
color: white;
|
| 47 |
+
align-self: flex-start;
|
| 48 |
+
}
|