-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
124 lines (103 loc) · 3.68 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
import os
from flask import Flask, render_template, request, jsonify
import pandas as pd
import matplotlib.pyplot as plt
import io
import base64
import openai
from dotenv import load_dotenv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
load_dotenv()
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
openai.api_key = os.getenv("OPENAI_API_KEY")
ALLOWED_EXTENSIONS = {'csv', 'xlsx', 'xls'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html')
@app.route('/example')
def example():
return render_template('example.html')
def read_file_with_flexible_delimiter(file):
file.seek(0)
sample = file.read(1024).decode('ISO-8859-1')
file.seek(0)
if ',' in sample and ';' not in sample:
delimiter = ','
elif ';' in sample:
delimiter = ';'
elif '\t' in sample:
delimiter = '\t'
elif '|' in sample:
delimiter = '|'
else:
raise ValueError('Unknown delimiter')
return pd.read_csv(file, delimiter=delimiter, encoding='ISO-8859-1', on_bad_lines='skip')
@app.route('/upload', methods=['POST'])
def upload_file():
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 and allowed_file(file.filename):
try:
if file.filename.endswith('.csv'):
df = read_file_with_flexible_delimiter(file)
else:
df = pd.read_excel(file, on_bad_lines='skip', encoding='ISO-8859-1')
except pd.errors.ParserError:
return jsonify({'error': 'Invalid CSV file format'}), 400
charts = analyze_and_generate_charts(df)
return render_template('result.html', charts=charts)
else:
return jsonify({'error': 'File type not allowed'}), 400
def analyze_and_generate_charts(df):
columns = df.columns[:10].tolist()
sample_data = df.iloc[:, :10].head(25).to_dict(orient='records')
prompt = f"""
Given the following dataset information:
Column names: {columns}
Sample data: {sample_data}
Please analyze this data and suggest 3 appropriate chart types. For each chart, provide:
1. A brief explanation of why this chart is suitable.
2. An appropriate title for the chart based on the data.
3. Python code using matplotlib to create the chart. Use the variable `df` to represent the dataset.
Respond in the following format:
Title: [Your chart title here]
Explanation: [Your explanation here]
Code:
```python
[Your Python code here]
```"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=1000,
temperature=0.7,
)
charts = []
chart_blocks = response.choices[0].message['content'].split('Title:')[1:]
for block in chart_blocks:
title = block.split('Explanation:')[0].strip()
explanation = block.split('Explanation:')[1].split('Code:')[0].strip()
chart_code = block.split('```python')[1].split('```')[0].strip()
plt.figure(figsize=(10, 6))
exec(chart_code)
img = io.BytesIO()
plt.savefig(img, format='png')
img.seek(0)
chart = base64.b64encode(img.getvalue()).decode()
plt.close()
charts.append({
'title': title,
'explanation': explanation,
'image': chart
})
return charts
if __name__ == '__main__':
app.run()