Spaces:
Runtime error
Runtime error
Change from ajax to javascript fetch
Browse files- app.py +5 -27
- requirements.txt +1 -1
- templates/index.html +42 -5
app.py
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify
|
| 2 |
from modules.model import summarize
|
| 3 |
-
from flask_cors import CORS, cross_origin
|
| 4 |
-
|
| 5 |
import __main__
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
CORS(app, supports_credentials=True)
|
| 9 |
-
app.config['CORS_ALLOW_HEADERS'] = 'Content-Type'
|
| 10 |
-
app.config['CORS_SUPPORTS_CREDENTIALS'] = 'True'
|
| 11 |
# shortTokenizer = BartTokenizer.from_pretrained('sshleifer/distilbart-xsum-12-6')
|
| 12 |
# shortModel = BartForConditionalGeneration.from_pretrained('sshleifer/distilbart-xsum-12-6')
|
| 13 |
|
|
@@ -19,43 +14,26 @@ def home():
|
|
| 19 |
return render_template('index.html')
|
| 20 |
|
| 21 |
|
| 22 |
-
@app.route('
|
| 23 |
-
@cross_origin()
|
| 24 |
def recommend():
|
| 25 |
if request.method == "POST":
|
| 26 |
# Get form data
|
| 27 |
-
request_data = request.get_json()
|
| 28 |
-
input_text = request_data['input_text']
|
|
|
|
| 29 |
|
| 30 |
# Call the function summarize to run the text summarization
|
| 31 |
try:
|
| 32 |
short_output_summary, long_output_summary = summarize(input_text)
|
| 33 |
response = jsonify({'short': short_output_summary.strip(), 'long': long_output_summary.strip()})
|
| 34 |
# Pass output summary to the output template
|
| 35 |
-
response.headers.add('Access-Control-Allow-Origin', 'http://0.0.0.0:7860')
|
| 36 |
-
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
| 37 |
-
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
|
| 38 |
-
response.headers.add('Access-Control-Allow-Credentials', 'true')
|
| 39 |
return response
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
return render_template('index.html', query=e)
|
| 43 |
|
| 44 |
-
elif request.method == "OPTIONS":
|
| 45 |
-
response = make_response("OK")
|
| 46 |
-
response.status_code = 201
|
| 47 |
-
response.headers.add('Access-Control-Allow-Origin', 'http://0.0.0.0:7860')
|
| 48 |
-
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
| 49 |
-
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
|
| 50 |
-
response.headers.add('Access-Control-Allow-Credentials', 'true')
|
| 51 |
-
# Debug
|
| 52 |
-
print("Right")
|
| 53 |
-
return response
|
| 54 |
-
|
| 55 |
pass
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
def main():
|
| 60 |
# app.config['TEMPLATES_AUTO_RELOAD'] = True
|
| 61 |
# app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
from modules.model import summarize
|
|
|
|
|
|
|
| 3 |
import __main__
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
| 6 |
# shortTokenizer = BartTokenizer.from_pretrained('sshleifer/distilbart-xsum-12-6')
|
| 7 |
# shortModel = BartForConditionalGeneration.from_pretrained('sshleifer/distilbart-xsum-12-6')
|
| 8 |
|
|
|
|
| 14 |
return render_template('index.html')
|
| 15 |
|
| 16 |
|
| 17 |
+
@app.route('/summarize', methods=['GET', 'POST'])
|
|
|
|
| 18 |
def recommend():
|
| 19 |
if request.method == "POST":
|
| 20 |
# Get form data
|
| 21 |
+
# request_data = request.args.get("input").get_json()
|
| 22 |
+
# input_text = request_data['input_text']
|
| 23 |
+
input_text = request.args.get("input")
|
| 24 |
|
| 25 |
# Call the function summarize to run the text summarization
|
| 26 |
try:
|
| 27 |
short_output_summary, long_output_summary = summarize(input_text)
|
| 28 |
response = jsonify({'short': short_output_summary.strip(), 'long': long_output_summary.strip()})
|
| 29 |
# Pass output summary to the output template
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return response
|
| 31 |
|
| 32 |
except Exception as e:
|
| 33 |
return render_template('index.html', query=e)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
pass
|
| 36 |
|
|
|
|
|
|
|
| 37 |
def main():
|
| 38 |
# app.config['TEMPLATES_AUTO_RELOAD'] = True
|
| 39 |
# app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
torch==1.11.0
|
| 2 |
Flask==2.1.0
|
| 3 |
transformers==4.20.0
|
| 4 |
-
flask_cors==3.0.10
|
|
|
|
| 1 |
torch==1.11.0
|
| 2 |
Flask==2.1.0
|
| 3 |
transformers==4.20.0
|
| 4 |
+
# flask_cors==3.0.10
|
templates/index.html
CHANGED
|
@@ -186,20 +186,56 @@
|
|
| 186 |
}, 100);
|
| 187 |
|
| 188 |
// ======= COMMUNICATE WITH FLSAK BACK END TO RUN ML TASK =======
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
$(document).on('submit', '#input_form', function (e) {
|
| 190 |
var outputField = document.getElementById("output_text");
|
| 191 |
var data = { "input_text": $("#input_text").val() };
|
| 192 |
e.preventDefault();
|
| 193 |
$.ajax({
|
| 194 |
type: 'POST',
|
| 195 |
-
url: '
|
| 196 |
data: JSON.stringify(data),
|
| 197 |
contentType: "application/json",
|
| 198 |
dataType: 'json',
|
| 199 |
-
|
| 200 |
-
withCredentials: true
|
| 201 |
-
},
|
| 202 |
-
crossDomain: true,
|
| 203 |
beforeSend: function () {
|
| 204 |
$("#check").attr('checked', false);
|
| 205 |
$("#input_text").attr("readonly", true);
|
|
@@ -220,6 +256,7 @@
|
|
| 220 |
},
|
| 221 |
})
|
| 222 |
});
|
|
|
|
| 223 |
|
| 224 |
</script>
|
| 225 |
</body>
|
|
|
|
| 186 |
}, 100);
|
| 187 |
|
| 188 |
// ======= COMMUNICATE WITH FLSAK BACK END TO RUN ML TASK =======
|
| 189 |
+
|
| 190 |
+
const translateText = async (jsonfile) => {
|
| 191 |
+
const inferResponse = await fetch(`/summarize?input=${jsonfile}`, {method: 'POST'});
|
| 192 |
+
const inferJson = await inferResponse.json();
|
| 193 |
+
|
| 194 |
+
return inferJson;
|
| 195 |
+
};
|
| 196 |
+
|
| 197 |
+
const textGenForm = document.querySelector('#input_form');
|
| 198 |
+
|
| 199 |
+
textGenForm.addEventListener('submit', async (event) => {
|
| 200 |
+
event.preventDefault();
|
| 201 |
+
|
| 202 |
+
const textGenParagraph = document.querySelector('#output_text');
|
| 203 |
+
const textGenInput = document.querySelector('#input_text');
|
| 204 |
+
$("#check").attr('checked', false);
|
| 205 |
+
$("#input_text").attr("readonly", true);
|
| 206 |
+
$(".summarize-button").hide();
|
| 207 |
+
$(".progress-icon").fadeIn();
|
| 208 |
+
$("#load-noti").fadeIn();
|
| 209 |
+
|
| 210 |
+
// var data = {"input_text": textGenInput.value};
|
| 211 |
+
|
| 212 |
+
try {
|
| 213 |
+
data_out = await translateText(textGenInput.value);
|
| 214 |
+
$("#check").attr('checked', true);
|
| 215 |
+
$("#input_text").attr("readonly", false);
|
| 216 |
+
$("#load-noti").text("Completed")
|
| 217 |
+
runOnce = true;
|
| 218 |
+
short_summary = data_out.short;
|
| 219 |
+
long_summary = data_out.long;
|
| 220 |
+
textGenParagraph.innerHTML = short_summary;
|
| 221 |
+
|
| 222 |
+
} catch (err) {
|
| 223 |
+
console.error(err);
|
| 224 |
+
}
|
| 225 |
+
});
|
| 226 |
+
|
| 227 |
+
/*
|
| 228 |
$(document).on('submit', '#input_form', function (e) {
|
| 229 |
var outputField = document.getElementById("output_text");
|
| 230 |
var data = { "input_text": $("#input_text").val() };
|
| 231 |
e.preventDefault();
|
| 232 |
$.ajax({
|
| 233 |
type: 'POST',
|
| 234 |
+
url: '/summarize',
|
| 235 |
data: JSON.stringify(data),
|
| 236 |
contentType: "application/json",
|
| 237 |
dataType: 'json',
|
| 238 |
+
|
|
|
|
|
|
|
|
|
|
| 239 |
beforeSend: function () {
|
| 240 |
$("#check").attr('checked', false);
|
| 241 |
$("#input_text").attr("readonly", true);
|
|
|
|
| 256 |
},
|
| 257 |
})
|
| 258 |
});
|
| 259 |
+
*/
|
| 260 |
|
| 261 |
</script>
|
| 262 |
</body>
|