Skip to content

Commit

Permalink
Merge pull request #25 from KIOS-Research/dev
Browse files Browse the repository at this point in the history
v0.6
  • Loading branch information
Mariosmsk authored Jul 2, 2023
2 parents 2cf2069 + 4e42c90 commit b7d1c27
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pip install openai
pip install SpeechRecognition
pip install pyaudio
pip install pyttsx3
pip install pdfgpt
```

### macOS + QGIS 3
Expand Down
2 changes: 1 addition & 1 deletion install_packages/check_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def check(required_packages):
importlib.import_module(package)

# Upgrade openai
if not update_version and not missing_packages:
if update_version:
message = "The package openai needs an update for the plugin QChatGPT:\n\n"
message += "\n".join(missing_packages)
message += "\n\nWould you like to update now?"
Expand Down
3 changes: 2 additions & 1 deletion install_packages/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
openai
SpeechRecognition
pyaudio
pyttsx3
pyttsx3
pdfgpt
12 changes: 8 additions & 4 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
name=QChatGPT
qgisMinimumVersion=3.0
description=A plugin integration between QGIS and openai API.
version=0.5
version=0.6
author=Marios S. Kyriakou, KIOS Research and Innovation Center of Excellence (KIOS CoE)
[email protected]

about=A plugin integration between QGIS and openai API. Displays the results from openai in QGIS.

tracker=https://github.com/Mariosmsk/QChatGPT/issues
repository=https://github.com/Mariosmsk/QChatGPT
tracker=https://github.com/KIOS-Research/QChatGPT/issues
repository=https://github.com/KIOS-Research/QChatGPT
# End of mandatory metadata

# Recommended items:

hasProcessingProvider=no
# Uncomment the following line and add your changelog:
changelog=2023-05-06 QChatGPT 0.5:
changelog=2023-07-02 QChatGPT 0.6:
Transfer repo to KIOS Research and Innovation Center of Excellence (KIOS CoE).
Some fixes.
Add pdfgpt package to talk with your pdf in QGIS.
2023-05-06 QChatGPT 0.5:
Implement AI voice response feature.
Add microphone button for input with a specified duration.
Add option to stop voice response.
Expand Down
67 changes: 59 additions & 8 deletions qchatqpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

API_EXIST = False
try:
check(['openai', 'SpeechRecognition', 'pyaudio', 'sounddevice', 'pyttsx3'])
check(['openai', 'SpeechRecognition', 'pyaudio', 'sounddevice', 'pyttsx3', 'pdfgpt'])
finally:
import openai

Expand All @@ -59,7 +59,10 @@
import pyttsx3
except:
pass

try:
from pdfgpt import *
except:
pass
API_EXIST = True

try:
Expand Down Expand Up @@ -114,6 +117,10 @@ def __init__(self, iface):
"""
# Save reference to the QGIS interface

self.pdf_d = None
self.pdf_df = None
self.pdf_num_pages = None
self.pdf_file = None
self.engine2 = None
self.task_read2 = None
self.engine = None
Expand Down Expand Up @@ -268,6 +275,22 @@ def unload(self):
action)
self.iface.removeToolBarIcon(action)

def showYesNoMessage(self, title, msg, yesMethod, icon):
msgBox = QMessageBox()
if icon == 'Warning':
msgBox.setIcon(QMessageBox.Warning)
if icon == 'Info':
msgBox.setIcon(QMessageBox.Information)
msgBox.setWindowTitle(title)
msgBox.setText(msg)
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
buttonY = msgBox.button(QMessageBox.Yes)
buttonY.setText('Yes')
buttonY.clicked.connect(yesMethod)
buttonNo = msgBox.button(QMessageBox.No)
buttonNo.setText('No')
msgBox.exec_()

def showMessage(self, title, msg, button, icon, fontsize=9):
msgBox = QMessageBox()
if icon == 'Warning':
Expand Down Expand Up @@ -340,7 +363,12 @@ def send_message(self):
if ask:
try:
question_history = " ".join(self.history) + " " + self.question
if self.dlg.image.isChecked():
if self.dlg.pdfchat.isChecked():
prompt = self.pdf_d.generatePrompt(self.pdf_df, self.pdf_num_pages, self.question)
print(prompt)
self.last_ans = self.pdf_d.sendPrompt(prompt, model="gpt-3.5-turbo")
print(self.last_ans)
elif self.dlg.image.isChecked():
self.response = openai.Image.create(
prompt=self.question,
n=1,
Expand All @@ -357,7 +385,7 @@ def send_message(self):
document = QTextDocument()
document.setHtml("<img src='{}'>".format(data_uri))
else:
if model in ["gpt-3.5-turbo", "gpt-3.5-turbo-0301"]:
if model in ["gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-4"]:
self.response = openai.ChatCompletion.create(
model=model,
max_tokens=max_tokens - len(self.question),
Expand Down Expand Up @@ -447,9 +475,9 @@ def validate_json(self):
def add_completed(self, task):
pass

def add_on_map_task(self):
self.task_add = QgsTask.fromFunction(f'QChatGPT Add files..', self.add_on_map, on_finished=self.add_completed)
QgsApplication.taskManager().addTask(self.task_add)
# def add_on_map_task(self):
# self.task_add = QgsTask.fromFunction(f'QChatGPT Add files..', self.add_on_map, on_finished=self.add_completed)
# QgsApplication.taskManager().addTask(self.task_add)

def add_on_map(self, task):
# Use regular expression to find the link
Expand Down Expand Up @@ -681,6 +709,26 @@ def start_speaking(self, text):
self.engine.runAndWait()
self.engine.stop()

def load_pdf_openai(self):
self.pdf_d = PDFBot(openai_key=self.dlg.custom_apikey.text())
extracted_text, self.pdf_num_pages = self.pdf_d.generateText(file_path=self.pdf_file)
self.pdf_df = self.pdf_d.generateEmbeddings(extracted_text)

def upload_pdf(self):
path = QFileDialog.getOpenFileName(None, 'Choose File', os.path.join(os.path.join(os.path.expanduser('~')),
'Desktop'), "PDF(*.pdf)")[0]
if len(path) > 0:
self.pdf_file = path
self.dlg.pdfchat.setEnabled(True)
self.dlg.pdf_path.setText(self.pdf_file)

self.showYesNoMessage("QChatGPT", "Do you want to continue to read the PDF file using your openai API "
"key?", self.load_pdf_openai, "Info")
else:
self.pdf_file = None
self.dlg.pdf_path.setText('')
self.dlg.pdfchat.setEnabled(False)

def stopped(self, task):
try:
self.voice_stop()
Expand Down Expand Up @@ -766,6 +814,7 @@ def run(self):

self.questions = []
self.answers = ['Welcome to the QChatGPT.']
self.dlg.pdfchat.setEnabled(False)

# self.dlg.setWindowFlags(Qt.Dialog | Qt.WindowStaysOnTopHint | Qt.WindowMinMaxButtonsHint |
# Qt.WindowCloseButtonHint)
Expand All @@ -776,14 +825,16 @@ def run(self):
self.dlg.microphone.clicked.connect(self.microphone_task)
self.dlg.voiceresponse.clicked.connect(self.voice_repsonse)
self.dlg.voicestop.clicked.connect(self.stopped)
self.dlg.toolButton.clicked.connect(self.upload_pdf)
palette = self.dlg.microphone.palette()
background_color = palette.color(QPalette.Button)
self.background__default_color = background_color.getRgb()

self.dlg.export_ans.clicked.connect(lambda: self.export_messages(ans=self.answers))
self.dlg.save_last_ans.clicked.connect(lambda: self.export_messages(text='Save AI',
ans=self.dlg.chatgpt_edit.toPlainText()))
self.dlg.addonmap.clicked.connect(self.add_on_map_task)
# self.dlg.addonmap.clicked.connect(self.add_on_map_task)
self.dlg.addonmap.clicked.connect(self.add_on_map)
self.dlg.question.returnPressed.connect(self.send_message)

# enable history questions
Expand Down
95 changes: 91 additions & 4 deletions qchatqpt_dialog_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1203</width>
<width>1259</width>
<height>375</height>
</rect>
</property>
Expand Down Expand Up @@ -96,6 +96,13 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="pdfchat">
<property name="text">
<string>PDF</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="image">
<property name="text">
Expand Down Expand Up @@ -419,7 +426,7 @@ p, li { white-space: pre-wrap; }
<string>The model which will generate the completion.</string>
</property>
<property name="text">
<string>&lt;a href=&quot;https://platform.openai.com/docs/models/overview&quot;&gt;Models &lt;/a&gt;:</string>
<string>&lt;a href=&quot;https://platform.openai.com/docs/models/overview&quot;&gt;Models &lt;/a&gt;: </string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
Expand All @@ -436,7 +443,7 @@ p, li { white-space: pre-wrap; }
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<width>46</width>
<height>20</height>
</size>
</property>
Expand All @@ -461,6 +468,11 @@ p, li { white-space: pre-wrap; }
<string>text-davinci-003</string>
</property>
</item>
<item>
<property name="text">
<string>gpt-4</string>
</property>
</item>
<item>
<property name="text">
<string>gpt-3.5-turbo</string>
Expand Down Expand Up @@ -639,6 +651,19 @@ p, li { white-space: pre-wrap; }
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
Expand All @@ -649,7 +674,7 @@ p, li { white-space: pre-wrap; }
<string>If you want, you can use your API key.</string>
</property>
<property name="text">
<string>&lt;a href=&quot;https://platform.openai.com/account/api-keys&quot;&gt;Use your API key&lt;/a&gt;</string>
<string>&lt;a href=&quot;https://platform.openai.com/account/api-keys&quot;&gt;Use your API key&lt;/a&gt; </string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down Expand Up @@ -677,6 +702,68 @@ p, li { white-space: pre-wrap; }
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Please select PDF from Chat.</string>
</property>
<property name="text">
<string>Use PDF file: </string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="pdf_path">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Please select PDF from Chat.</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down

0 comments on commit b7d1c27

Please sign in to comment.