Spaces:
Sleeping
Sleeping
Fix streaming
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, Response
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
|
@@ -6,16 +6,24 @@ app = Flask(__name__)
|
|
| 6 |
|
| 7 |
OLLAMA_API_URL = "http://localhost:11434"
|
| 8 |
|
| 9 |
-
@stream_with_context
|
| 10 |
def generate(data):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.route('/chat', methods=['POST'])
|
| 16 |
def chat():
|
| 17 |
data = request.get_json(silent=True)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@app.route('/embed', methods=['POST'])
|
| 21 |
def embed():
|
|
@@ -25,4 +33,4 @@ def embed():
|
|
| 25 |
|
| 26 |
if __name__ == '__main__':
|
| 27 |
port = int(os.environ.get('PORT', 7860))
|
| 28 |
-
app.run(host='0.0.0.0', port=port, debug=False)
|
|
|
|
| 1 |
+
from flask import Flask, request, Response
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 6 |
|
| 7 |
OLLAMA_API_URL = "http://localhost:11434"
|
| 8 |
|
|
|
|
| 9 |
def generate(data):
|
| 10 |
+
try:
|
| 11 |
+
with requests.post(f"{OLLAMA_API_URL}/api/chat", json=data, stream=True) as r:
|
| 12 |
+
r.raise_for_status() # Raise an exception for bad status codes
|
| 13 |
+
for line in r.iter_lines():
|
| 14 |
+
if line:
|
| 15 |
+
yield line.decode('utf-8') + '\n'
|
| 16 |
+
except Exception as e:
|
| 17 |
+
# Yield an error message if something goes wrong
|
| 18 |
+
yield f'{{"error": "Stream error: {str(e)}"}}\n'
|
| 19 |
|
| 20 |
@app.route('/chat', methods=['POST'])
|
| 21 |
def chat():
|
| 22 |
data = request.get_json(silent=True)
|
| 23 |
+
response = Response(generate(data), mimetype='text/plain')
|
| 24 |
+
response.headers['Cache-Control'] = 'no-cache'
|
| 25 |
+
response.headers['X-Accel-Buffering'] = 'no' # Disable buffering in nginx
|
| 26 |
+
return response
|
| 27 |
|
| 28 |
@app.route('/embed', methods=['POST'])
|
| 29 |
def embed():
|
|
|
|
| 33 |
|
| 34 |
if __name__ == '__main__':
|
| 35 |
port = int(os.environ.get('PORT', 7860))
|
| 36 |
+
app.run(host='0.0.0.0', port=port, debug=False, threaded=True)
|