Spaces:
Runtime error
Runtime error
Zwea Htet
commited on
Commit
β’
3c8d75d
1
Parent(s):
faa8f19
integrate fastapi
Browse files- .gitignore +5 -0
- Dockerfile +7 -2
- app.py +0 -52
- index.json +0 -0
- main.py +37 -0
- requirements.txt +5 -2
- static/icons/dialogflow-insights-svgrepo-com.svg +20 -0
- static/script.js +66 -0
- {styles β static/styles}/chat.css +0 -0
- styles/auth.css β static/styles/login.css +0 -0
- templates/base.html +4 -3
- templates/chat.html +1 -77
- templates/{auth.html β login.html} +0 -0
- templates/update.html +0 -18
.gitignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
venv/
|
2 |
+
data/__pycache__
|
3 |
+
models/__pycache__
|
4 |
+
.env
|
5 |
+
__pycache__
|
Dockerfile
CHANGED
@@ -4,8 +4,12 @@ WORKDIR /docker
|
|
4 |
|
5 |
ADD . /docker
|
6 |
|
|
|
|
|
7 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
8 |
|
|
|
|
|
9 |
# Set up a new user named "user" with user ID 1000
|
10 |
RUN useradd -m -u 1000 user
|
11 |
|
@@ -14,7 +18,7 @@ USER user
|
|
14 |
|
15 |
# Set home to the user's home directory
|
16 |
ENV HOME=/home/user \
|
17 |
-
|
18 |
|
19 |
# Set the working directory to the user's home directory
|
20 |
WORKDIR $HOME/app
|
@@ -22,4 +26,5 @@ WORKDIR $HOME/app
|
|
22 |
# Set the working directory to the user's home directory
|
23 |
COPY --chown=user . $HOME/app
|
24 |
|
25 |
-
CMD [ "gunicorn" , "
|
|
|
|
4 |
|
5 |
ADD . /docker
|
6 |
|
7 |
+
COPY requirements.txt requirements.txt
|
8 |
+
RUN pip install --upgrade pip
|
9 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
10 |
|
11 |
+
COPY . .
|
12 |
+
|
13 |
# Set up a new user named "user" with user ID 1000
|
14 |
RUN useradd -m -u 1000 user
|
15 |
|
|
|
18 |
|
19 |
# Set home to the user's home directory
|
20 |
ENV HOME=/home/user \
|
21 |
+
PATH=/home/user/.local/bin:$PATH
|
22 |
|
23 |
# Set the working directory to the user's home directory
|
24 |
WORKDIR $HOME/app
|
|
|
26 |
# Set the working directory to the user's home directory
|
27 |
COPY --chown=user . $HOME/app
|
28 |
|
29 |
+
# CMD [ "gunicorn" , "main:app" ]
|
30 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
DELETED
@@ -1,52 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from datetime import datetime
|
3 |
-
|
4 |
-
import openai
|
5 |
-
from flask import Flask, redirect, render_template, request, session, url_for
|
6 |
-
from models.bloom import initialize_index
|
7 |
-
|
8 |
-
app = Flask(__name__)
|
9 |
-
index = None
|
10 |
-
|
11 |
-
@app.route("/")
|
12 |
-
def home():
|
13 |
-
if 'access_token' not in session:
|
14 |
-
return render_template('auth.html')
|
15 |
-
global index
|
16 |
-
try:
|
17 |
-
index = initialize_index("index.json")
|
18 |
-
except:
|
19 |
-
return render_template("auth.html", error="Invalid Token Key")
|
20 |
-
return render_template('chat.html')
|
21 |
-
|
22 |
-
|
23 |
-
@app.route("/login", methods=["GET", "POST"])
|
24 |
-
def login():
|
25 |
-
if request.method == "POST":
|
26 |
-
username = request.form.get("username")
|
27 |
-
access_token = request.form.get("access_token")
|
28 |
-
openai.api_key = access_token
|
29 |
-
session['access_token'] = access_token
|
30 |
-
return redirect(url_for('home'))
|
31 |
-
return render_template('auth.html')
|
32 |
-
|
33 |
-
@app.route("/logout")
|
34 |
-
def logout():
|
35 |
-
session.pop('access_token', None)
|
36 |
-
return redirect(url_for('home'))
|
37 |
-
|
38 |
-
@app.route("/chat")
|
39 |
-
def chat():
|
40 |
-
query_text = request.args.get("msg", None)
|
41 |
-
if query_text is None:
|
42 |
-
return "Invalid input"
|
43 |
-
|
44 |
-
if 'access_token' not in session:
|
45 |
-
return redirect(url_for('login'))
|
46 |
-
|
47 |
-
response = index.query(query_text)
|
48 |
-
return str(response), 200
|
49 |
-
|
50 |
-
|
51 |
-
if __name__ == "__main__":
|
52 |
-
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
index.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
main.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import openai
|
4 |
+
import uvicorn
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
# from flask import Flask, redirect, render_template, request, session, url_for
|
7 |
+
from fastapi import FastAPI, Form, Request
|
8 |
+
from fastapi.responses import HTMLResponse
|
9 |
+
from fastapi.staticfiles import StaticFiles
|
10 |
+
from fastapi.templating import Jinja2Templates
|
11 |
+
|
12 |
+
from models.bloom import initialize_index
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
app = FastAPI()
|
16 |
+
app.secret_key = os.environ.get('SECRET_KEY', 'my-secret-key')
|
17 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
18 |
+
|
19 |
+
# mounts the static folder that contains the css file
|
20 |
+
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
21 |
+
|
22 |
+
# locates the template files that will be modified at run time
|
23 |
+
# with the dialog form the user and bot
|
24 |
+
templates = Jinja2Templates(directory="templates")
|
25 |
+
|
26 |
+
@app.get("/", response_class=HTMLResponse)
|
27 |
+
async def home(request: Request):
|
28 |
+
return templates.TemplateResponse('chat.html', {"request": request})
|
29 |
+
|
30 |
+
@app.get("/chat")
|
31 |
+
async def chat(input: str):
|
32 |
+
bot_reply = index.query(input)
|
33 |
+
return {"bot_reply": bot_reply}
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
index = initialize_index("index.json")
|
37 |
+
uvicorn.run("main:app", reload=True)
|
requirements.txt
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
Click
|
2 |
-
|
|
|
|
|
3 |
Flask-SQLAlchemy
|
4 |
gunicorn
|
5 |
itsdangerous
|
@@ -14,4 +16,5 @@ panda
|
|
14 |
numpy
|
15 |
langchain
|
16 |
python-dotenv
|
17 |
-
flask-login
|
|
|
|
1 |
Click
|
2 |
+
flask
|
3 |
+
fastapi
|
4 |
+
unvicorn
|
5 |
Flask-SQLAlchemy
|
6 |
gunicorn
|
7 |
itsdangerous
|
|
|
16 |
numpy
|
17 |
langchain
|
18 |
python-dotenv
|
19 |
+
flask-login
|
20 |
+
python-multipart
|
static/icons/dialogflow-insights-svgrepo-com.svg
ADDED
static/script.js
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const msgerForm = get(".msger-inputarea");
|
2 |
+
const msgerInput = get(".msger-input");
|
3 |
+
const msgerChat = get(".msger-chat");
|
4 |
+
|
5 |
+
// Icons made by Freepik from www.flaticon.com
|
6 |
+
const BOT_IMG = "./icons/dialogflow-insights-svgrepo-com.svg";
|
7 |
+
const PERSON_IMG = "";
|
8 |
+
const BOT_NAME = " ChatBot";
|
9 |
+
const PERSON_NAME = "You";
|
10 |
+
|
11 |
+
msgerForm.addEventListener("submit", async (event) => {
|
12 |
+
event.preventDefault();
|
13 |
+
|
14 |
+
const msgText = msgerInput.value;
|
15 |
+
if (!msgText) return;
|
16 |
+
|
17 |
+
appendMessage(PERSON_NAME, PERSON_IMG, "right", msgText);
|
18 |
+
msgerInput.value = "";
|
19 |
+
botResponse(msgText);
|
20 |
+
});
|
21 |
+
|
22 |
+
function appendMessage(name, img, side, text) {
|
23 |
+
// Simple solution for small apps
|
24 |
+
const msgHTML = `
|
25 |
+
<div class="msg ${side}-msg">
|
26 |
+
<div class="msg-img" style="background-image: url(${img})"></div>
|
27 |
+
|
28 |
+
<div class="msg-bubble">
|
29 |
+
<div class="msg-info">
|
30 |
+
<div class="msg-info-name">${name}</div>
|
31 |
+
<div class="msg-info-time">${formatDate(
|
32 |
+
new Date()
|
33 |
+
)}</div>
|
34 |
+
</div>
|
35 |
+
|
36 |
+
<div class="msg-text">${text}</div>
|
37 |
+
</div>
|
38 |
+
</div>
|
39 |
+
`;
|
40 |
+
|
41 |
+
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
|
42 |
+
msgerChat.scrollTop += 500;
|
43 |
+
}
|
44 |
+
|
45 |
+
async function botResponse(text) {
|
46 |
+
// Bot Response
|
47 |
+
const response = await fetch("chat?input=${text}");
|
48 |
+
const json = await response.json();
|
49 |
+
|
50 |
+
console.log(text);
|
51 |
+
console.log(json);
|
52 |
+
|
53 |
+
appendMessage(BOT_NAME, BOT_IMG, "left", json.bot_reply);
|
54 |
+
}
|
55 |
+
|
56 |
+
// Utils
|
57 |
+
function get(selector, root = document) {
|
58 |
+
return root.querySelector(selector);
|
59 |
+
}
|
60 |
+
|
61 |
+
function formatDate(date) {
|
62 |
+
const h = "0" + date.getHours();
|
63 |
+
const m = "0" + date.getMinutes();
|
64 |
+
|
65 |
+
return `${h.slice(-2)}:${m.slice(-2)}`;
|
66 |
+
}
|
{styles β static/styles}/chat.css
RENAMED
File without changes
|
styles/auth.css β static/styles/login.css
RENAMED
File without changes
|
templates/base.html
CHANGED
@@ -6,9 +6,10 @@
|
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
8 |
<title>{% block title %}{% endblock %}</title>
|
9 |
-
<link rel="stylesheet" href="{{ url_for('static',
|
10 |
-
<link rel="stylesheet" href="{{ url_for('static',
|
11 |
-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
|
|
12 |
</head>
|
13 |
|
14 |
<body>
|
|
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
8 |
<title>{% block title %}{% endblock %}</title>
|
9 |
+
<link rel="stylesheet" href="{{ url_for('static', path='styles/chat.css') }}">
|
10 |
+
<link rel="stylesheet" href="{{ url_for('static', path='styles/login.css') }}">
|
11 |
+
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> -->
|
12 |
+
<script type="module" src="../static/script.js"></script>
|
13 |
</head>
|
14 |
|
15 |
<body>
|
templates/chat.html
CHANGED
@@ -9,10 +9,6 @@
|
|
9 |
<div class="msger-header-title">
|
10 |
<i class="fas fa-bug"></i> Chatbot <i class="fas fa-bug"></i>
|
11 |
</div>
|
12 |
-
|
13 |
-
{% if session.logged_in %}
|
14 |
-
<a href="{{ url_for('logout') }}" class="msger-logout-btn">Logout</a>
|
15 |
-
{% endif %}
|
16 |
</header>
|
17 |
|
18 |
<main class="msger-chat">
|
@@ -23,7 +19,7 @@
|
|
23 |
<div class="msg-bubble">
|
24 |
<div class="msg-info">
|
25 |
<div class="msg-info-name">Chatbot</div>
|
26 |
-
<div class="msg-info-time">
|
27 |
</div>
|
28 |
|
29 |
<div class="msg-text">
|
@@ -41,76 +37,4 @@
|
|
41 |
</section>
|
42 |
<!-- partial -->
|
43 |
<script src='https://use.fontawesome.com/releases/v5.0.13/js/all.js'></script>
|
44 |
-
<script>
|
45 |
-
|
46 |
-
const msgerForm = get(".msger-inputarea");
|
47 |
-
const msgerInput = get(".msger-input");
|
48 |
-
const msgerChat = get(".msger-chat");
|
49 |
-
|
50 |
-
|
51 |
-
// Icons made by Freepik from www.flaticon.com
|
52 |
-
const BOT_IMG = "https://image.flaticon.com/icons/svg/327/327779.svg";
|
53 |
-
const PERSON_IMG = "https://image.flaticon.com/icons/svg/145/145867.svg";
|
54 |
-
const BOT_NAME = " ChatBot";
|
55 |
-
const PERSON_NAME = "You";
|
56 |
-
|
57 |
-
msgerForm.addEventListener("submit", event => {
|
58 |
-
event.preventDefault();
|
59 |
-
|
60 |
-
const msgText = msgerInput.value;
|
61 |
-
if (!msgText) return;
|
62 |
-
|
63 |
-
appendMessage(PERSON_NAME, PERSON_IMG, "right", msgText);
|
64 |
-
msgerInput.value = "";
|
65 |
-
botResponse(msgText);
|
66 |
-
});
|
67 |
-
|
68 |
-
function appendMessage(name, img, side, text) {
|
69 |
-
// Simple solution for small apps
|
70 |
-
const msgHTML = `
|
71 |
-
<div class="msg ${side}-msg">
|
72 |
-
<div class="msg-img" style="background-image: url(${img})"></div>
|
73 |
-
|
74 |
-
<div class="msg-bubble">
|
75 |
-
<div class="msg-info">
|
76 |
-
<div class="msg-info-name">${name}</div>
|
77 |
-
<div class="msg-info-time">${formatDate(new Date())}</div>
|
78 |
-
</div>
|
79 |
-
|
80 |
-
<div class="msg-text">${text}</div>
|
81 |
-
</div>
|
82 |
-
</div>
|
83 |
-
`;
|
84 |
-
|
85 |
-
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
|
86 |
-
msgerChat.scrollTop += 500;
|
87 |
-
}
|
88 |
-
|
89 |
-
function botResponse(rawText) {
|
90 |
-
|
91 |
-
// Bot Response
|
92 |
-
$.get("/chat", { msg: rawText }).done(function (data) {
|
93 |
-
console.log(rawText);
|
94 |
-
console.log(data);
|
95 |
-
const msgText = data;
|
96 |
-
appendMessage(BOT_NAME, BOT_IMG, "left", msgText);
|
97 |
-
|
98 |
-
});
|
99 |
-
|
100 |
-
}
|
101 |
-
|
102 |
-
|
103 |
-
// Utils
|
104 |
-
function get(selector, root = document) {
|
105 |
-
return root.querySelector(selector);
|
106 |
-
}
|
107 |
-
|
108 |
-
function formatDate(date) {
|
109 |
-
const h = "0" + date.getHours();
|
110 |
-
const m = "0" + date.getMinutes();
|
111 |
-
|
112 |
-
return `${h.slice(-2)}:${m.slice(-2)}`;
|
113 |
-
}
|
114 |
-
</script>
|
115 |
-
|
116 |
{% endblock %}
|
|
|
9 |
<div class="msger-header-title">
|
10 |
<i class="fas fa-bug"></i> Chatbot <i class="fas fa-bug"></i>
|
11 |
</div>
|
|
|
|
|
|
|
|
|
12 |
</header>
|
13 |
|
14 |
<main class="msger-chat">
|
|
|
19 |
<div class="msg-bubble">
|
20 |
<div class="msg-info">
|
21 |
<div class="msg-info-name">Chatbot</div>
|
22 |
+
<div class="msg-info-time">{}</div>
|
23 |
</div>
|
24 |
|
25 |
<div class="msg-text">
|
|
|
37 |
</section>
|
38 |
<!-- partial -->
|
39 |
<script src='https://use.fontawesome.com/releases/v5.0.13/js/all.js'></script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
{% endblock %}
|
templates/{auth.html β login.html}
RENAMED
File without changes
|
templates/update.html
DELETED
@@ -1,18 +0,0 @@
|
|
1 |
-
{% extends 'base.html' %}
|
2 |
-
|
3 |
-
{% block head %}
|
4 |
-
<title>Task Master</title>
|
5 |
-
{% endblock %}
|
6 |
-
|
7 |
-
{% block body %}
|
8 |
-
<div class="content">
|
9 |
-
<h1 style="text-align: center">Update Task</h1>
|
10 |
-
|
11 |
-
<div class="form">
|
12 |
-
<form action="/update/{{task.id}}" method="POST">
|
13 |
-
<input type="text" name="content" id="content" value="{{task.content}}">
|
14 |
-
<input type="submit" value="Update">
|
15 |
-
</form>
|
16 |
-
</div>
|
17 |
-
</div>
|
18 |
-
{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|