|
| 1 | +# OPENAI_API_KEY = "k-mtujzCGAyXF5x2gLcLBDT3BlbkFJVLdrDwDxsKnpEkhzV7t" |
| 2 | +import sys |
| 3 | +import csv |
| 4 | +import pandas as pd |
| 5 | +import matplotlib # Add this import |
| 6 | +# matplotlib.use('QtAgg') |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +# from io import BytesIO |
| 9 | +# from PIL import Image |
| 10 | +from pandasai import PandasAI |
| 11 | +from pandasai.llm.openai import OpenAI |
| 12 | +from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTextBrowser, QTextEdit, QPushButton, QFileDialog, QTableWidget, QTableWidgetItem, QLabel |
| 13 | + |
| 14 | +from PyQt6.QtCore import Qt |
| 15 | +import os |
| 16 | +from PyQt6.QtGui import QPixmap |
| 17 | + |
| 18 | +# matplotlib.use('Agg') # Move this line here |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +class ChatWindow(QMainWindow): |
| 23 | + def __init__(self): |
| 24 | + super().__init__() |
| 25 | + |
| 26 | + self.setWindowTitle("Chat Window") |
| 27 | + self.setGeometry(100, 100, 800, 600) |
| 28 | + |
| 29 | + self.central_widget = QWidget() |
| 30 | + self.setCentralWidget(self.central_widget) |
| 31 | + |
| 32 | + self.layout = QVBoxLayout() |
| 33 | + |
| 34 | + self.chat_display_label = QLabel() |
| 35 | + self.layout.addWidget(self.chat_display_label) |
| 36 | + |
| 37 | + self.message_input = QTextEdit() |
| 38 | + self.message_input.setFixedHeight(50) |
| 39 | + self.layout.addWidget(self.message_input) |
| 40 | + |
| 41 | + self.send_button = QPushButton("Send") |
| 42 | + self.send_button.clicked.connect(self.send_message) |
| 43 | + self.layout.addWidget(self.send_button) |
| 44 | + |
| 45 | + self.upload_button = QPushButton("Upload CSV") |
| 46 | + self.upload_button.clicked.connect(self.upload_csv) |
| 47 | + self.layout.addWidget(self.upload_button) |
| 48 | + |
| 49 | + self.table_widget = QTableWidget() |
| 50 | + self.layout.addWidget(self.table_widget) |
| 51 | + |
| 52 | + self.central_widget.setLayout(self.layout) |
| 53 | + |
| 54 | + self.chat_history = [] |
| 55 | + self.df = None # Store the CSV data |
| 56 | + |
| 57 | + self.init_pandasai() |
| 58 | + |
| 59 | + def init_pandasai(self): |
| 60 | + OPENAI_API_KEY = "k-mtujzCGAyXF5x2gLcLBDT3BlbkFJVLdrDwDxsKnpEkhzV7t" |
| 61 | + llm = OpenAI(api_token=OPENAI_API_KEY) |
| 62 | + user_defined_path = os.getcwd() |
| 63 | + pandas_ai = PandasAI(llm, verbose=True, conversational=False, save_charts=True, |
| 64 | + save_charts_path=user_defined_path, enable_cache=False) |
| 65 | + |
| 66 | + self.pandas_ai = PandasAI(llm) |
| 67 | + |
| 68 | + # def send_message(self): |
| 69 | + # message = self.message_input.toPlainText() |
| 70 | + # if message: |
| 71 | + # self.chat_history.append(f"User: {message}") |
| 72 | + # response = self.run_pandasai(message) |
| 73 | + # if response is not None: |
| 74 | + # if isinstance(response, float): |
| 75 | + # self.chat_history.append(f"PandasAI: {response}") |
| 76 | + # elif isinstance(response, str) and "plot" in response.lower(): |
| 77 | + # self.generate_and_display_plot(response) |
| 78 | + # else: |
| 79 | + # self.chat_history.append(f"PandasAI: {response}") |
| 80 | + # self.update_chat_display() |
| 81 | + |
| 82 | + # print(response) |
| 83 | + |
| 84 | + def send_message(self): |
| 85 | + message = self.message_input.toPlainText() |
| 86 | + if message: |
| 87 | + self.chat_history.append(f"User: {message}") |
| 88 | + response = self.run_pandasai(message) |
| 89 | + if response is not None: |
| 90 | + if isinstance(response, float): |
| 91 | + self.chat_history.append(f"PandasAI: {response}") |
| 92 | + elif isinstance(response, str) and "plot" in response.lower(): |
| 93 | + self.generate_and_display_plot(response) |
| 94 | + else: |
| 95 | + self.chat_history.append(f"PandasAI: {response}") |
| 96 | + self.update_chat_display() |
| 97 | + |
| 98 | + print(response) |
| 99 | + |
| 100 | + def run_pandasai(self, message): |
| 101 | + try: |
| 102 | + if self.df is not None: |
| 103 | + |
| 104 | + response = self.pandas_ai.run(self.df, prompt=message) |
| 105 | + plt.show() |
| 106 | + # code = self.pandas_ai.last_code_generated(self.df, prompt=message) |
| 107 | + print("Pandasai response:", response) |
| 108 | + return response |
| 109 | + else: |
| 110 | + return "Please upload a CSV file first." |
| 111 | + except Exception as e: |
| 112 | + print("PandasAI Error:", e) |
| 113 | + return None |
| 114 | + |
| 115 | + def update_chat_display(self): |
| 116 | + chat_text = "\n".join(self.chat_history) |
| 117 | + self.chat_display_label.setText(chat_text) |
| 118 | + |
| 119 | + |
| 120 | + def upload_csv(self): |
| 121 | + file_dialog = QFileDialog() |
| 122 | + file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile) |
| 123 | + file_name, _ = file_dialog.getOpenFileName(self, "Upload CSV File", "", "CSV Files (*.csv);;All Files (*)") |
| 124 | + |
| 125 | + if file_name: |
| 126 | + try: |
| 127 | + self.df = pd.read_csv(file_name) |
| 128 | + self.load_csv_to_table(file_name) |
| 129 | + self.chat_history.append("CSV file uploaded.") |
| 130 | + self.update_chat_display() |
| 131 | + except Exception as e: |
| 132 | + print("Error:", e) |
| 133 | + |
| 134 | + def load_csv_to_table(self, file_name): |
| 135 | + self.table_widget.clear() |
| 136 | + self.table_widget.setRowCount(0) |
| 137 | + self.table_widget.setColumnCount(0) |
| 138 | + |
| 139 | + with open(file_name, 'r') as csv_file: |
| 140 | + csv_reader = csv.reader(csv_file) |
| 141 | + for row_idx, row in enumerate(csv_reader): |
| 142 | + if row_idx == 0: |
| 143 | + self.table_widget.setColumnCount(len(row)) |
| 144 | + self.table_widget.insertRow(row_idx) |
| 145 | + for col_idx, cell_value in enumerate(row): |
| 146 | + self.table_widget.setItem(row_idx, col_idx, QTableWidgetItem(cell_value)) |
| 147 | + |
| 148 | + |
| 149 | + # def generate_and_display_chart(self, data): |
| 150 | + # plt.figure() |
| 151 | + # plt.title("Generated Chart") |
| 152 | + # plt.bar(range(len(data)), data) |
| 153 | + # plt.xlabel("X-axis") |
| 154 | + # plt.ylabel("Y-axis") |
| 155 | + # plt.tight_layout() |
| 156 | + |
| 157 | + # # Display the figure using Matplotlib and process PyQt events |
| 158 | + # plt.show(block=True) |
| 159 | + # QApplication.processEvents() |
| 160 | + |
| 161 | + # self.chat_history.append("PandasAI: Generated Chart") |
| 162 | + |
| 163 | + |
| 164 | + def clear_layout(self): |
| 165 | + layout = self.layout() |
| 166 | + if layout.count() > 5: |
| 167 | + for i in range(5, layout.count()): |
| 168 | + widget = layout.itemAt(i).widget() |
| 169 | + if widget: |
| 170 | + widget.deleteLater() |
| 171 | + |
| 172 | +def main(): |
| 173 | + app = QApplication(sys.argv) |
| 174 | + chat_app = ChatWindow() |
| 175 | + chat_app.show() |
| 176 | + sys.exit(app.exec()) |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + main() |
0 commit comments