-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathapi.py
55 lines (40 loc) · 1.38 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, render_template, jsonify, request
from urllib.parse import unquote
from decouple import config
from web import decode_website
# from doc import decoded_doc
from summary import summarize_webpage
import os
import nltk
nltk.data.path.append('nltk_data')
os.environ["OPENAI_API_KEY"] = config('OPENAI_API_KEY')
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/api/data', methods=['GET','POST'])
def get_data():
if request.method == 'GET':
sample_data = {
'message': 'Hello, Flask API!',
'data': [1, 2, 3, 4, 5]
}
print ("DEBUG",sample_data)
return jsonify(sample_data)
elif request.method == 'POST':
print ("DEBUG request",request)
encode_url = unquote(unquote(request.args.get('url')))
print ("DEBUG encode_url",encode_url)
if not encode_url:
return jsonify({'error': 'URL is required'}), 400
decoded_text = decode_website(encode_url)
print ("DEBUG decoded_text",decoded_text)
summary = summarize_webpage(decoded_text)
response = {
'submitted_url': encode_url,
'summary': summary,
}
return jsonify(response)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5001))
app.run(debug=True, host='0.0.0.0', port=port)