Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
|
@@ -1,38 +1,59 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
model_name = "gpt2" # Replace with your desired model name (e.g., gpt-neo)
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
|
| 12 |
-
@app.route(
|
| 13 |
-
def
|
| 14 |
try:
|
| 15 |
data = request.get_json()
|
| 16 |
-
if not data or
|
| 17 |
return jsonify({"error": "Missing 'prompt' in request body"}), 400
|
| 18 |
|
| 19 |
-
prompt = data[
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
-
|
| 35 |
-
return jsonify({"error": "An error occurred."}), 500
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
-
app.run(debug=False, host=
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response
|
| 2 |
+
import subprocess
|
| 3 |
+
import json
|
| 4 |
+
import logging
|
| 5 |
|
| 6 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
@app.route('/completions', methods=['POST'])
|
| 12 |
+
def get_completion():
|
| 13 |
try:
|
| 14 |
data = request.get_json()
|
| 15 |
+
if not data or 'prompt' not in data:
|
| 16 |
return jsonify({"error": "Missing 'prompt' in request body"}), 400
|
| 17 |
|
| 18 |
+
prompt = data['prompt']
|
| 19 |
+
|
| 20 |
+
def generate():
|
| 21 |
+
try:
|
| 22 |
+
process = subprocess.Popen(
|
| 23 |
+
["ollama", "run", "llama2", "-p", prompt], # Replace "llama2" with your model
|
| 24 |
+
stdout=subprocess.PIPE,
|
| 25 |
+
stderr=subprocess.PIPE,
|
| 26 |
+
text=True,
|
| 27 |
+
encoding="utf-8"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
for line in process.stdout:
|
| 31 |
+
try:
|
| 32 |
+
json_line = json.loads(line)
|
| 33 |
+
if "response" in json_line:
|
| 34 |
+
yield f"data: {json.dumps({'text': json_line['response']})}\n\n"
|
| 35 |
+
elif "done" in json_line: #check for done message
|
| 36 |
+
break
|
| 37 |
+
except json.JSONDecodeError:
|
| 38 |
+
logger.warning(f"Invalid JSON line from Ollama: {line.strip()}")
|
| 39 |
+
|
| 40 |
+
stderr = process.stderr.read()
|
| 41 |
+
if stderr:
|
| 42 |
+
logger.error(f"Ollama stderr: {stderr}")
|
| 43 |
+
|
| 44 |
+
process.wait()
|
| 45 |
+
|
| 46 |
+
except FileNotFoundError:
|
| 47 |
+
yield f"data: {json.dumps({'text': 'Error: Ollama not found. Is it installed and in your PATH?'})}\n\n"
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logger.exception("Error in Ollama subprocess:")
|
| 50 |
+
yield f"data: {json.dumps({'text': f'Error in Ollama: {e}'})}\n\n"
|
| 51 |
+
|
| 52 |
+
return Response(generate(), mimetype='text/event-stream')
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
+
logger.exception("Error in /completions route:")
|
| 56 |
+
return jsonify({"error": "An error occurred during processing."}), 500
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
app.run(debug=False, host='0.0.0.0', port=5000)
|