Tuesday, August 6, 2024

Upgrading the Ollama Hosted Phi3 Offline Chat Program

 In my previous post, "Chat with Ollama Phi3 in Python Offline," I introduced a basic program that allowed you to chat with Ollama's Phi3 language model offline using Python. Today, I’m excited to share an upgraded version of this program. This new version incorporates a graphical user interface (GUI) using PyQt6 and includes a text-to-speech (TTS) engine to read aloud Phi3's responses. The chat logs are displayed in a QTextEdit widget, making the interaction more user-friendly and accessible. This program is created in Python 3.10 same experience I encountered when I first create a chat program using Langchain and Openai. also a good pc/mac will deliver a better experience but it will still run on a Intel M3-100y but it will be very slow considering that I have downloaded the phi3 4b  quantized version.


Upgraded Features

  1. Graphical User Interface (GUI): A chat window created using PyQt6.
  2. Text-to-Speech: Utilizes pyttsx3 to read aloud Phi3's responses.
  3. Chat Logs: Displays the conversation history in a QTextEdit widget.

Code Explanation

Below is the complete code for the upgraded program, followed by detailed explanations of each part.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pyttsx3
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from langchain_community.llms import Ollama

class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()

        # Set up the layout
        layout = QVBoxLayout()
        hlayout = QHBoxLayout()
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        # Initialize the language model
        self.llm = Ollama(model="phi3")
        
        # Add widgets to the layout
        layout.addLayout(hlayout)
        self.te = QLineEdit(self)
        self.le = QTextEdit(self)
        self.le.setReadOnly(True)
        self.btnStart = QPushButton("Ask", self)
        hlayout.addWidget(self.te)
        hlayout.addWidget(self.btnStart)
        layout.addWidget(self.le)
        
        # Connect button click to function
        self.btnStart.clicked.connect(self.onClickedStart)
        
        # Set window properties
        self.setGeometry(25, 45, 900, 500)
        self.setWindowTitle('Speak')
 
    def onClickedStart(self):
        global text
        
        # Get user input and display it in the chat log
        user_input = self.te.text()
        self.le.append(f'You: {user_input}')
        
        # Get response from the language model
        response = self.llm.invoke(user_input)
        
        # Display the response in the chat log
        self.le.append(f'Phi3: {response}')
        
        # Prepare text for text-to-speech
        text = response
        self.worker = WorkerThread()
        self.worker.start()
   
text = ''
engine = []
error = ''
i = 0

class WorkerThread(QThread):
    def run(self):
        global text, engine, error, i
        
        try:
            i += 1
            engine.append(pyttsx3.init())
            engine[i-1].setProperty('rate', 120)
            engine[i-1].say(text)
            engine[i-1].startLoop()
        except Exception as err:
            error = str(err)

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec())

Detailed Explanation

Imports

  • pyttsx3: A text-to-speech conversion library in Python.
  • PyQt6: A set of Python bindings for the Qt application framework, used to create the GUI.
  • Ollama: A module from the langchain_community package for interacting with the Phi3 language model.

Window Class

  • Initialization (__init__ method):

    • Sets up the main layout and widgets (QLineEdit, QTextEdit, QPushButton).
    • Initializes the Phi3 language model using Ollama.
    • Connects the "Ask" button to the onClickedStart method.
    • Configures the window size and title.
  • onClickedStart Method:

    • Gets the user's input from the QLineEdit widget.
    • Appends the user's input to the QTextEdit widget for display.
    • Sends the input to the Phi3 language model and gets the response.
    • Appends the response to the QTextEdit widget.
    • Initiates a text-to-speech conversion of the response using the WorkerThread class.

WorkerThread Class

  • A QThread subclass that handles the text-to-speech functionality.
  • run Method:
    • Initializes a new pyttsx3 engine for each response.
    • Sets the speech rate and converts the response text to speech.
    • Starts the speech engine loop to read the response aloud.

Main Block

  • Creates a QApplication instance and a Window instance.
  • Displays the Window and starts the event loop.

Conclusion

This upgraded version of the Ollama Phi3 offline chat program provides a more interactive and user-friendly experience. The integration of a GUI and text-to-speech functionality makes it easier to interact with the Phi3 model without needing to modify the underlying code. Give it a try and see how these enhancements improve your interactions with Phi3!

No comments:

Post a Comment