-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
157 lines (132 loc) · 5 KB
/
app.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# https://chatgpt.com/c/6a20deaf-cb35-41ce-9aa1-23dd55bde40b
# Finish the code according to the sample code above
import os
import flask
from flask_cors import CORS
from flask import Flask, jsonify, request, Response, render_template
from typing import List, Dict, Tuple, Any
from dotenv import load_dotenv
from openai import OpenAI
import requests
import json
import pandas as pd
import numpy as np
import openpyxl
load_dotenv()
app = Flask(__name__)
CORS(app)
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)
message_history = [{'role': 'system', 'content': 'You are a personal finance assistant'}]
@app.route('/')
def home_page():
return render_template('index.html')
@app.route('/get_openai_response', methods=['POST'])
def get_response() -> Response:
try:
data = request.get_json()
new_message = data.get('messages')
if not new_message:
return jsonify({'Error': 'No message provided'}), 400
global message_history
response, message_history = chat_bot(new_message, message_history)
return jsonify({'response': response, 'message_history': message_history}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
def chat_bot(message, message_history):
"""
Input: str, List[str]
Output: str, List[str]
"""
message_history.append({'role': 'user', 'content': message})
try:
response = client.chat.completions.create(
messages=message_history,
model='gpt-4o',
stream=True,
)
response_content = ""
for chunk in response:
if chunk and chunk.choices[0].delta.content:
response_content += chunk.choices[0].delta.content
message_history.append({'role': 'system', 'content': response_content})
return response_content, message_history
except Exception as e:
print({"error": e}), 500
raise
@app.route('/upload', methods=['POST'])
def uploadFile():
try:
file = request.files['file']
if file:
file_path = os.path.join('upload', file.filename)
file.save(file_path)
df = pd.read_csv(file_path)
# convert dataframe to string(csv or json)
df_string = df.to_csv(index=False)
message_history[0]["content"] += "Here are the file content of the user's transaction history, and the user is going to ask question about it. "
message_history[0]["content"] += df_string
return jsonify({ 'success': f'File ${file.filename} uploaded successfully!' }), 200
else:
print("No file uploaded")
return jsonify({"error": "No file uploaded"}), 400
except Exception as e:
return jsonify({ 'Error': f'${e}' }), 500
@app.route('/piechart', methods=['POST'])
def pie_chart():
# Retrive the file somewhere
try:
month = 3
df = pd.read_csv('upload/checking.csv')
df['Date'] = pd.to_datetime(df['Date'])
# Query that month
df['Month'] = pd.Timestamp()
monthly_data = df[df['Month'] == 3]
response = jsonify(monthly_data)
return response, 200
except Exception as e:
return jsonify({ 'Error': f'Error ${e} getting the pie chart' }), 500
@app.route('/trend', methods=['Post'])
def trend():
"""Set the default length to the past 6 months"""
try:
window = 6
hash = {}
# suppose to be a line chart, x-y: month-expense
# should return a json object containting 6 k-v pairs
# key is month, value is expenese
# Have to sum up the each month in a new df
c = get_month()
for i in 6:
cur = df[df['month'] == c - i]
cur['Total'] = cur['Amount'].sum()
hash[c-i] = cur['Total']
return jsonify(hash)
except Exception as e:
return jsonify({ 'Error': f'Error ${e} retrieving the trend' }), 500
if __name__ == "__main__":
app.run(debug=True)
# @app.route('/upload', methods=['POST'])
# def upload_file():
# try:
# if 'file' not in request.files:
# return jsonify({'error': 'No file part'}), 400
# file = request.files['file']
# if file.filename == '':
# return jsonify({'error': 'No selected file'}), 400
# if file:
# if not os.path.exists('upload'):
# os.makedirs('upload')
# file_path = os.path.join('upload', file.filename)
# file.save(file_path)
# print(f'File saved at {file_path}')
# # Read the file contents if needed
# with open(file_path, 'r') as f:
# file_contents = f.read()
# df = pd.read_csv(file_path)
# print(df.info())
# return jsonify({'success': f'File {file.filename} uploaded and read successfully!'}), 200
# else:
# print("File doesn't exist")
# except Exception as e:
# return jsonify({'error': str(e)}), 500