-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (41 loc) · 1.39 KB
/
app.py
File metadata and controls
53 lines (41 loc) · 1.39 KB
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, request, jsonify, render_template
from flask_cors import CORS
import requests
import os
from lastmileai import LastMile
import signal
lastmile = LastMile(api_key=os.environ["LASTMILEAI_API_KEY"])
app = Flask(__name__)
CORS(app)
NEWS_API_KEY = "b5b9a807cf6649128b07b112213b234d" # Replace with your News API key
@app.route('/close_the_app')
def close_the_app():
token = request.args.get('token')
if token == 'johnlocke':
print('Shutting down...')
os.kill(os.getpid(), signal.SIGINT)
return 'Server shutting down...'
else:
return 'Invalid token'
@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
data = request.get_json()
messages = data['messages']
completion = lastmile.create_openai_chat_completion(
completion_params = {
'model': "gpt-4",
'messages': messages,
}
)
return completion["completionResponse"]["choices"][0]["message"]["content"]
@app.route('/')
def index():
# Fetch top headlines from News API
url = f'https://newsapi.org/v2/top-headlines?country=us&apiKey={NEWS_API_KEY}'
response = requests.get(url)
news_data = response.json()
# Extract relevant information from the response
articles = news_data.get('articles', [])
return render_template('index.html', articles=articles)
if __name__ == '__main__':
app.run(debug=True)