I am working on a project for my Computer Class wherein we were instructed to follow a particular (YouTube Video). I have followed the video in its entirety but I’ve been encountering a few errors, some of which I was able to change. My current code looks like: (I have listed my problems underneath)
import Constants
import sys
import openai
#PyQt5 helps with the UI Interface
from PyQt5 import QtCore
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QLabel,
QLineEdit,
QPushButton,
QVBoxLayout,
QHBoxLayout,
QGroupBox,
QTextEdit
)
openai.api_key = Constants.API_KEY
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self): #Create the widgets in this method
self.logo_label = QLabel()
self.logo_pixmap = QPixmap(r'C:Users*****Desktop******MACHINELEARNING_BOTimageplaceholder.png').scaled(150,150,QtCore.Qt.AspectRatioMode.KeepAspectRatio,QtCore.Qt.TransformationMode.SmoothTransformation)
self.logo_label.setPixmap(self.logo_pixmap)
self.input_label = QLabel("Ask a question",self)
self.input_field = QLineEdit("Help",self) #create the textbox
self.input_field.setPlaceholderText("Type your text")
self.answer_label = QLabel("Answer:")
self.answer_field = QTextEdit(self)
self.answer_field.setReadOnly(True)
self.submit_button = QPushButton('Submit')
self.submit_button.setStyleSheet(
"""
QPushButton{
background-color: #4CAF50;
border: none;
color: white;
padding: 15px 32 px;
font-size: 18 px;
font-weight: bold;
border-radius: 10px;
}
QPushButton:hover{
background-color: #3eBe41;
}
"""
)
self.popular_questions_group = QGroupBox('Popular Questions')
self.popular_questions_layout = QVBoxLayout()
self.popular_questions = ["What is Machine Learning","What is Artificial Intelligence", "Where do I learn Data Structures from?"]
self.question_buttons = []
#Create a layout
layout = QVBoxLayout()
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(20)
layout.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
#Add Logo
layout.addWidget(self.logo_label, alignment = QtCore.Qt.AlignmentFlag.AlignCenter)
#Add Input field
input_layout = QHBoxLayout()
input_layout.addWidget(self.input_label)
input_layout.addWidget(self.input_field)
input_layout.addWidget(self.submit_button)
#Ass Popular Questions Button
for question in self.popular_questions:
button = QPushButton(question)
button.setStyleSheet(
"""
QPushButton{
background-color: #FFFFFF;
border: 2px solid #00AEFF;
color: #00AEFF;
padding: 10px, 20px;
font-size: 18px;
font-weight: bold;
border-radius: 5px;
}
QPushButton:hover{
background-color: #00AEFF;
color: #FFFFFF;
}
"""
)
button.clicked.connect(lambda _, q=question: self.input_field.setText(q))
self.popular_questions_layout.addWidget(button)
self.question_buttons.append(button)
self.popular_questions_group.setLayout(self.popular_questions_layout)
layout.addWidget(self.popular_questions_group)
#Set the Layout
self.setLayout(layout)
#Set the window properties
self.setWindowTitle("Placeholder Title")
self.setGeometry(200,200,600,600)
#Connet Submit button to function which queries OpenAI's API
self.submit_button.clicked.connect(self.get_answer)
def get_answer(self): #Creating the function which queries the API
question = self.input_field.text()
completion = openai.chat.completions.create(
model = "gpt-3.5-turbo",
messages = [{"role":"user", "content":"You are a computer scientist, answer the following questions in points"},
{"role":"user", "content":f'{question}'}],
max_tokens = 1024,
n = 1,
stop = None,
temperature = 0.7,
stream = True
)
answer = completion.choices[0].message.content
self.answer_field.setText(answer)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Pictures of how the current application looks:
My Application
The versions of the APIs and Python are:
- openai 1.35.3
- Python 3.12.4
- Name: PyQt5, Version: 5.15.10, License: GPL v3, Requires: PyQt5-Qt5, PyQt5-sip
- I use VS-Code
Note: I used ****, ***** in the Image address just here for privacy, on VS-Code its the real thing.
The problems I had faced include:
1.QPixmap::scaled: Pixmap is a null pixmap
:(Now Corrected)
2. No attribute KeepAspectRatio in Qt:(Now Corrected)
3. Input label, Input field, Answer Label, Answer Field, Submit Button, etc. not showing up in the layout after running.
4. OpenAI’s API returns an error: openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}
What I’ve tried/Corrected:
Problems 1&2:
- The Pixmap Null error has been removed: I found that my version of PyQt5 has somethings moved around, so the
KeepAspectRatio
now comes underAspectRatioMode
, andSmoothTransformation
comes underTransformationMode
. I’ve corrected them and you shall see the corrected code in my code display. - Similarly I figured that the PixMap returned null because there was a mistake in the path of the image file which has also been corrected.
Problem 3: Haven’t been able to get this working
Problem 4: When I change the current code like: self.submit_button = QPushButton('Submit')
to self.submit_button = QPushButton('Submit',self)
then the submit button shows but upon clicking any of the “popular questions” the program stops responding and raises an error after closing the error is: raise self._make_status_error_from_response(err.response) from None openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}
Please help me understand where the code seems to go wrong and what I could do to make the fixes, thankyou so much for your effort and time. I would appreciate if you could also direct me to some resources which could help me, being a beginner looking through the documentation seems very daunting and overwhelming – something I hope to overcome soon 🙂
Aaryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.