-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatwithcsv-2.py
167 lines (132 loc) · 5.56 KB
/
chatwithcsv-2.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
158
159
160
161
162
163
164
165
166
167
# OPENAI_API_KEY = "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"
import sys
import csv
import pandas as pd
import matplotlib.pyplot as plt
from io import BytesIO
from PIL import Image
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTextBrowser, QTextEdit, QPushButton, QFileDialog, QTableWidget, QTableWidgetItem, QLabel
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap
class ChatWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Chat Window")
self.setGeometry(100, 100, 800, 600)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout()
self.chat_display = QTextBrowser()
self.layout.addWidget(self.chat_display)
self.message_input = QTextEdit()
self.message_input.setFixedHeight(50)
self.layout.addWidget(self.message_input)
self.send_button = QPushButton("Send")
self.send_button.clicked.connect(self.send_message)
self.layout.addWidget(self.send_button)
self.upload_button = QPushButton("Upload CSV")
self.upload_button.clicked.connect(self.upload_csv)
self.layout.addWidget(self.upload_button)
self.table_widget = QTableWidget()
self.layout.addWidget(self.table_widget)
self.central_widget.setLayout(self.layout)
self.chat_history = []
self.df = None # Store the CSV data
self.init_pandasai()
def init_pandasai(self):
OPENAI_API_KEY = "k-mtujzCGAyXF5x2gLcLBDT3BlbkFJVLdrDwDxsKnpEkhzV7t"
llm = OpenAI(api_token=OPENAI_API_KEY)
self.pandas_ai = PandasAI(llm)
# def send_message(self):
# message = self.message_input.toPlainText()
# if message:
# self.chat_history.append(f"User: {message}")
# response = self.run_pandasai(message)
# if response is not None:
# if isinstance(response, float):
# self.chat_history.append(f"PandasAI: {response}")
# elif isinstance(response, str) and "plot" in response.lower():
# self.generate_and_display_plot(response)
# else:
# self.chat_history.append(f"PandasAI: {response}")
# self.update_chat_display()
# print(response)
def send_message(self):
message = self.message_input.toPlainText()
if message:
self.chat_history.append(f"User: {message}")
response = self.run_pandasai(message)
if response is not None:
if isinstance(response, float):
self.chat_history.append(f"PandasAI: {response}")
elif isinstance(response, str) and "plot" in response.lower():
self.generate_and_display_plot(response)
else:
self.chat_history.append(f"PandasAI: {response}")
self.update_chat_display()
print(response)
def run_pandasai(self, message):
try:
if self.df is not None:
response = self.pandas_ai.run(self.df, prompt=message)
return response
else:
return "Please upload a CSV file first."
except Exception as e:
print("PandasAI Error:", e)
return None
def update_chat_display(self):
self.chat_display.clear()
for message in self.chat_history:
self.chat_display.append(message)
def upload_csv(self):
file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
file_name, _ = file_dialog.getOpenFileName(self, "Upload CSV File", "", "CSV Files (*.csv);;All Files (*)")
if file_name:
try:
self.df = pd.read_csv(file_name)
self.load_csv_to_table(file_name)
self.chat_history.append("CSV file uploaded.")
self.update_chat_display()
except Exception as e:
print("Error:", e)
def load_csv_to_table(self, file_name):
self.table_widget.clear()
self.table_widget.setRowCount(0)
self.table_widget.setColumnCount(0)
with open(file_name, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row_idx, row in enumerate(csv_reader):
if row_idx == 0:
self.table_widget.setColumnCount(len(row))
self.table_widget.insertRow(row_idx)
for col_idx, cell_value in enumerate(row):
self.table_widget.setItem(row_idx, col_idx, QTableWidgetItem(cell_value))
def generate_and_display_chart(self, data):
plt.figure()
plt.title("Generated Chart")
plt.bar(range(len(data)), data)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.tight_layout()
# Display the figure using Matplotlib and process PyQt events
plt.show(block=False)
QApplication.processEvents()
self.chat_history.append("PandasAI: Generated Chart")
def clear_layout(self):
layout = self.layout()
if layout.count() > 5:
for i in range(5, layout.count()):
widget = layout.itemAt(i).widget()
if widget:
widget.deleteLater()
def main():
app = QApplication(sys.argv)
chat_app = ChatWindow()
chat_app.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()