Skip to content

Commit

Permalink
Modify frontend and fix backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhrankan-Chakrabarti committed Aug 2, 2024
1 parent fac377b commit 40c00b6
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 278 deletions.
Binary file modified __pycache__/app.cpython-311.pyc
Binary file not shown.
59 changes: 51 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
from flask import *
import json
import threading
import time
from queue import Queue
from flask import Flask, Response, render_template, request

app = Flask(__name__)
app.static_folder = 'static'
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get", methods=['GET', 'POST'])

prompt_queue = Queue()
sse_event_queue = Queue()
response_thread = None

@app.route('/', methods=['GET', 'POST'])
def index():
global prompt_queue

if request.method == 'POST':

prompt = request.form.get('prompt')

prompt_queue.put(prompt)

return {'status': 'success'}, 200

return render_template('index.html')

def send_sse_data():
global prompt_queue, sse_event_queue, response_thread
while True:
if not prompt_queue.empty():
if response_thread and response_thread.is_alive():
continue

prompt = prompt_queue.get()

response_thread = threading.Thread(target=stream, args=(prompt, model))
response_thread.start()

while not sse_event_queue.empty():
sse_event = sse_event_queue.get()
yield f"data: {json.dumps(sse_event)}\n\n"

time.sleep(1)

@app.route('/stream', methods=['GET'])
def get_bot_response():
userText = request.args.get('msg') if request.method == "POST" else None
return Response(stream(input=userText, model=model) if request.method == "POST" else None, mimetype='text/event-stream')
def event_stream():
return send_sse_data()

return Response(event_stream(), mimetype='text/event-stream')

def globalize(m, s):
global model, stream
model, stream = m, s

def main(model, stream):
globalize(model, stream)
app.run()
app.run(debug=True, use_reloader=False)
Loading

0 comments on commit 40c00b6

Please sign in to comment.