Unable to make the text widgets and an error with openai api also follows

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:

  1. openai 1.35.3
  2. Python 3.12.4
  3. Name: PyQt5, Version: 5.15.10, License: GPL v3, Requires: PyQt5-Qt5, PyQt5-sip
  4. 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:

  1. The Pixmap Null error has been removed: I found that my version of PyQt5 has somethings moved around, so the KeepAspectRatio now comes under AspectRatioMode, and SmoothTransformation comes under TransformationMode. I’ve corrected them and you shall see the corrected code in my code display.
  2. 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 🙂

New contributor

Aaryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật