Caros,
Eu utilizei o código abaixo e está me retornando KeyError: 'preco', mas não consigo entender onde está o erro no código.
from flask import Flask, request, jsonify
from textblob import TextBlob
from googletrans import Translator
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
df = pd.read_csv('E:/Users/Fabio/PYTHON/api/casas.csv', sep = ',')
colunas = ['tamanho','ano','garagem','preco']
x = df.drop('preco',axis=1)
y = df['preco']
x_train, x_test, y_train, y_test = train_test_split(
x,y,test_size = 0.3, random_state=42)
modelo = LinearRegression()
modelo.fit(x_train,y_train)
##Agora vamos construir todos os end points para cada modelo
app = Flask(__name__)
@app.route('/cotacao_json/', methods=['POST'])
def cotacao_json():
dados = request.get_json()
dados_input = [dados[col] for col in colunas]
preco = modelo.predict([dados_input])
return jsonify(preco=preco[0])
app.run(debug=True)
No postman, ele me deu essas msg de erro:
<!doctype html>
<html lang=en>
<head>
<title>KeyError: 'preco'
// Werkzeug Debugger</title>
............
Traceback (most recent call last):
File "C:\Users\frani\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 1498, in __call__
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\frani\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 1476, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\frani\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\frani\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\frani\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\frani\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Users\Fabio\PYTHON\api\main.py", line 74, in cotacao_json
dados_input = [dados[col] for col in colunas]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Users\Fabio\PYTHON\api\main.py", line 74, in <listcomp>
dados_input = [dados[col] for col in colunas]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'preco'
Por gentileza, alguém pode me dar uma ajuda sobre o que pode estar errado?