OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Aaryan
  • Start date Start date
A

Aaryan

Guest
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)

Code:
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_BOT\imageplaceholder.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: `

Code:
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 :)
<p>I am working on a project for my Computer Class wherein we were instructed to follow a particular (<a href="
" rel="nofollow noreferrer">YouTube Video</a>). 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)</p>
<pre><code>
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_BOT\imageplaceholder.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_())

</code></pre>
<p>Pictures of how the current application looks:</p>
<p><a href="https://i.sstatic.net/Q7YAPInZ.png" rel="nofollow noreferrer">My Application</a></p>
<p>The versions of the APIs and Python are:</p>
<ol>
<li>openai 1.35.3</li>
<li>Python 3.12.4</li>
<li>Name: PyQt5, Version: 5.15.10, License: GPL v3, Requires: PyQt5-Qt5, PyQt5-sip</li>
<li>I use VS-Code</li>
</ol>
<p>Note: I used ****, ***** in the Image address just here for privacy, on VS-Code its the real thing.</p>
<p>The problems I had faced include:</p>
<ol>
<li><code>QPixmap::scaled: Pixmap is a null pixmap</code>:(Now Corrected)</li>
<li>No attribute KeepAspectRatio in Qt:(Now Corrected)</li>
<li>Input label, Input field, Answer Label, Answer Field, Submit Button, etc. not showing up in the layout after running.</li>
<li>OpenAI's API returns an error: <code>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'}}</code></li>
</ol>
<p>What I've tried/Corrected:</p>
<blockquote>
<p><strong>Problems 1&2</strong>:</p>
<ol>
<li>The Pixmap Null error has been removed: I found that my version of PyQt5 has somethings moved around, so the <code>KeepAspectRatio</code> now comes under <code>AspectRatioMode</code>, and <code>SmoothTransformation</code> comes under <code>TransformationMode</code>. I've corrected them and you shall see the corrected code in my code display.</li>
<li>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.</li>
</ol>
</blockquote>
<blockquote>
<p><strong>Problem 3</strong>: Haven't been able to get this working</p>
</blockquote>
<blockquote>
<p><strong>Problem 4</strong>: When I change the current code like: <code>self.submit_button = QPushButton('Submit')</code> to <code>self.submit_button = QPushButton('Submit',self)</code> then the <a href="https://i.sstatic.net/5wm7FPHO.png" rel="nofollow noreferrer">submit button shows</a> but upon clicking any of the "popular questions" <a href="https://i.sstatic.net/TM4BDLCJ.png" rel="nofollow noreferrer">the program stops responding</a> and <a href="https://i.sstatic.net/ler8Bo9F.png" rel="nofollow noreferrer">raises an error after closing</a> the error is:
`</p>
</blockquote>
<pre><code>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'}}
</code></pre>
<p>`</p>
<p>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 :)</p>
 

Latest posts

Top