File size: 2,838 Bytes
248fbeb b3a99ca 1a0d7ed 988c7cc af2645a 1791798 977039f 2e9353d 975f08e 2e9353d 05e9000 2e9353d cbef1a5 988c7cc 5f2f34f 988c7cc 5f2f34f 40fbdfe 12ad28b 06154c7 2e9353d 06154c7 2e9353d 06154c7 2e9353d 06154c7 2e9353d cbef1a5 2e9353d cbef1a5 06154c7 cbef1a5 f8ef4e3 cbef1a5 2e9353d cbef1a5 b1c090f cbef1a5 f8ef4e3 cbef1a5 0f71b02 cbef1a5 30743bb cbef1a5 05e9000 cbef1a5 05e9000 cbef1a5 05e9000 2e9353d cbef1a5 c33867e 988c7cc cbef1a5 49d9f84 d25a8b4 14b6a24 988c7cc 2e9353d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
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
import logging
from dotenv import load_dotenv
from pandasai import SmartDatalake
from pandasai import SmartDataframe
from pandasai.responses.response_parser import ResponseParser
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from datetime import datetime
import matplotlib.pyplot as plt
import google.generativeai as genai
load_dotenv()
app = Flask(__name__)
cors = CORS(app)
class FlaskResponse(ResponseParser):
def __init__(self, context) -> None:
super().__init__(context)
def format_dataframe(self, result):
return result['value'].to_html()
def format_plot(self, result):
try:
img_path = result['value']
except ValueError:
img_path = str(result['value'])
print("ValueError:", img_path)
print("response_class_path:", img_path)
return img_path
def format_other(self, result):
return str(result['value'])
gemini_api_key = os.getenv('Gemini')
llm = ChatGoogleGenerativeAI(api_key=gemini_api_key, model='gemini-1.5-flash', temperature=0.1)
gemini_api_key = os.environ['Gemini']
genai.configure(api_key=gemini_api_key)
generation_config = {
"temperature": 0.2,
"top_p": 0.95,
"max_output_tokens": 5000,
}
model = genai.GenerativeModel(
model_name="gemini-2.0-flash-thinking-exp",
generation_config=generation_config,
)
# Endpoint for chat
@app.route("/chat", methods=["POST"])
@cross_origin()
def bot():
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))
pandas_agent = SmartDataframe(df,config={"llm":llm, "response_parser":FlaskResponse})
answer = pandas_agent.chat(user_question)
return jsonify(answer)
return answer
#df = df.rename(co
# Reports endpoint
@app.route("/report", methods=["POST"])
@cross_origin()
def marketing_rec():
json_data = request.json.get("json_data")
prompt = """
You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report, including appropriate key perfomance indicators and recommendations.
data:
""" + str(json_data)
response = model.generate_content(prompt)
report = response.text
return jsonify(str(report))
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=7860) |