Spaces:
Sleeping
Sleeping
first commit
Browse files- .gitignore +1 -0
- app.py +15 -0
- static/chatbot.js +46 -0
- static/styles.css +0 -0
- templates/chatbot.html +20 -0
- templates/index.html +4 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, Response
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
@app.route('/')
|
| 7 |
+
def index():
|
| 8 |
+
return render_template('index.html')
|
| 9 |
+
|
| 10 |
+
@app.route('/chatbot', methods=['GET'])
|
| 11 |
+
def chatbot():
|
| 12 |
+
return render_template('chatbot.html')
|
| 13 |
+
|
| 14 |
+
if __name__ == '__main__':
|
| 15 |
+
app.run(debug=True)
|
static/chatbot.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const item = {
|
| 2 |
+
user: [],
|
| 3 |
+
ai: []
|
| 4 |
+
};
|
| 5 |
+
async function generate() {
|
| 6 |
+
try {
|
| 7 |
+
// process history
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
// get reply
|
| 12 |
+
|
| 13 |
+
// append reply form api
|
| 14 |
+
let userP = document.createElement('p');
|
| 15 |
+
const yourReply = document.getElementById('user-reply').value;
|
| 16 |
+
userP.textContent = 'User: ' + yourReply;
|
| 17 |
+
item.user.push(`${yourReply}.\n`);
|
| 18 |
+
document.getElementById('content').appendChild(userP);
|
| 19 |
+
|
| 20 |
+
link = 'https://arcsu1-fine-tuned-models.hf.space/generate'
|
| 21 |
+
link = 'http://127.0.0.1:8000/generate';
|
| 22 |
+
|
| 23 |
+
const response = await fetch(link, {
|
| 24 |
+
method: 'POST', // Method type
|
| 25 |
+
headers: {
|
| 26 |
+
'Content-Type': 'application/json' // Specify that we are sending JSON
|
| 27 |
+
},
|
| 28 |
+
body: JSON.stringify(item) // Convert the JavaScript object to a JSON string
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
const reply = await response.json();
|
| 32 |
+
|
| 33 |
+
let aiP = document.createElement('p');
|
| 34 |
+
aiP.textContent = 'AI: ' + reply;
|
| 35 |
+
item.ai.push(`${reply}.\n`);
|
| 36 |
+
console.log(reply);
|
| 37 |
+
document.getElementById('content').appendChild(aiP);
|
| 38 |
+
document.getElementById('user-reply').value = '';
|
| 39 |
+
|
| 40 |
+
} catch (error) {
|
| 41 |
+
|
| 42 |
+
console.error('Error fetching the root endpoint:', error);
|
| 43 |
+
document.getElementById('content').appendChild("Error fetching the root endpoint");
|
| 44 |
+
document.getElementById('user-reply').value = '';
|
| 45 |
+
}
|
| 46 |
+
}
|
static/styles.css
ADDED
|
File without changes
|
templates/chatbot.html
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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>FastAPI Root</title>
|
| 7 |
+
</head>
|
| 8 |
+
<body>
|
| 9 |
+
<h1>Response from FastAPI</h1>
|
| 10 |
+
|
| 11 |
+
<div id="content">
|
| 12 |
+
</div>
|
| 13 |
+
|
| 14 |
+
<input type="text" id="user-reply">
|
| 15 |
+
<input type="submit" onclick="generate()">
|
| 16 |
+
|
| 17 |
+
<script src="{{ url_for('static', filename='chatbot.js') }}"></script>
|
| 18 |
+
|
| 19 |
+
</body>
|
| 20 |
+
</html>
|
templates/index.html
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
Hello World
|
| 3 |
+
<a href="/chatbot">Chatbot</a>
|
| 4 |
+
</html>
|