From c82f603d513dce88430178b0877e16aade480c6a Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Wed, 17 Jul 2024 14:31:39 +0200 Subject: [PATCH] =?UTF-8?q?Testar=20olika=20metoder=20f=C3=B6r=20att=20han?= =?UTF-8?q?tera=20Cross-Origin=20Resource=20Sharing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smartassist/src/backend.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/smartassist/src/backend.py b/smartassist/src/backend.py index 7266f7f..b6eb10f 100644 --- a/smartassist/src/backend.py +++ b/smartassist/src/backend.py @@ -2,25 +2,29 @@ # Import the necessary functions from ollama, Flask, requests, threading from ollama import Client from flask import Flask, request, jsonify +from flask_cors import CORS, cross_origin #import requests #import threading # Initialize a Flask application app = Flask(__name__) +CORS(app) +# @app.route('/api/chat', methods=['POST']) +# @cross_origin(origin='http://localhost:8000', headers=['Content-Type']) # Allow cross-origin requests from localhost:8000 @app.route('/api/chat', methods=['POST']) +@cross_origin(origin='*', headers=['Content-Type']) # Enable CORS for this route. Probably not necessary. def chat(): - if request.method == 'POST': - # Get the message from the JSON in the request body - data = request.get_json() - message = data.get('query') + # Get the message from the JSON in the request body + data = request.get_json() + message = data.get('query') - if message: - # response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message}) - response = request.post('http://localhost:11434', json={"model": "llama3",'prompt': message}) - return jsonify({'response': response.json().get('result')}) - else: - return jsonify({'error': 'No query provided'}), 400 + if message: + # response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message}) + response = request.post('http://localhost:11434', json={"model": "llama3",'prompt': message}) + return jsonify({'response': response.json().get('result')}) + else: + return jsonify({'error': 'No query provided'}), 400 @app.route('/smartassist', methods=["POST"])