-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (71 loc) · 2.27 KB
/
main.py
File metadata and controls
86 lines (71 loc) · 2.27 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import asyncio
import threading
from queue import Queue
from flask import Flask, request
import uuid
from multiprocessing import Process
app = Flask(__name__)
import Openai_calls
result = {}
result_completion = {}
task_que = Queue(10)
"""
Pushes the request into a queue
"""
def queue_openai_response(message, request_id):
print("request id is: " + request_id)
result_completion[request_id] = False
print(message)
print(request_id)
task_que.put([message, request_id])
# response = await Openai_calls.get_roast(message)
"""
Check if there is an element in the queue and start processing the response if there is
"""
async def get_open_ai_response():
while True:
if not task_que.empty():
task = task_que.get()
result[task[1]] = Openai_calls.get_roast(task[0])
print("process complete")
print(task[0])
print(task[1])
result_completion[task[1]] = True
else:
await asyncio.sleep(1)
"""
Gets the request with the message to respond to. Returns a request_id that would represent the result to the particular message
"""
@app.route('/', methods=['POST'])
def receiving_request():
data = request.data.decode()
request_id = str(uuid.uuid4())
queue_openai_response(data, request_id)
return request_id, 200
"""
Responds to whether the response has finished processing
"""
@app.route('/retrieve', methods=['POST'])
def responding_to_request():
request_id = request.data.decode()
print("request_id is " + request_id)
print(result)
if request_id in result_completion:
if request_id in result:
print("returning result")
print(result)
return result[request_id], 200
else:
return "processing", 202
print("returning 404")
return "request id not found", 404
def run_async_loop():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(get_open_ai_response())
if __name__ == "__main__":
flask_thread = threading.Thread(target=app.run)
processing_thread = threading.Thread(target=run_async_loop)
#run the flask and processing on separate threads so both can be running at the same time
flask_thread.start()
processing_thread.start()