QTX-API / main.py
rairo's picture
Update main.py
c968cc6 verified
from langchain_google_genai import ChatGoogleGenerativeAI
import pandas as pd
import os
import io
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain.agents.agent_types import AgentType
import pandas as pd
from dotenv import load_dotenv
import json
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
cors = CORS(app)
@app.route("/", methods=["GET"])
def home():
return "Hello Qx!"
gemini_api_key = os.environ['GOOGLE_API_KEY']
@app.route("/predict", methods=["POST"])
@cross_origin()
def bot():
load_dotenv()
#
json_table = request.json.get("json_table")
user_question = request.json.get("user_question")
#data = request.get_json(force=True)TRye
#print(req_body)
#data = eval(req_body)
#json_table = data["json_table"]
#user_question = data["user_question"]
#print(json_table)
print(user_question)
data = eval(str(json_table))
df = pd.DataFrame(data)
print(list(df))
#df = df.rename(columns={'dateupdated': 'date'})
#df['date'] = pd.to_datetime(df['date'], format="%b %d", errors='coerce')
#df['Profit'] = df['Profit'].apply(lambda x: "R{:.1f}".format((x)))
#df['Revenue'] = df['Revenue'].apply(lambda x: "R{:.1f}".format((x)))
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro', temperature=0.1, google_api_key=gemini_api_key)
#llm = GooglePalm(temperature=0, google_api_key=os.environ['PALM'])
agent = create_pandas_dataframe_agent(llm, df, agent="structured_chat-zero-shot-react-description", verbose=True)
response = agent.run(user_question)
#response.headers.add('Access-Control-Allow-Origin', '*')
return jsonify(response)
if __name__ == "__main__":
app.run(debug=True,host="0.0.0.0", port=7860)