Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cx_freeze problem with pytorch #2495

Closed
MJT369 opened this issue Jul 9, 2024 · 6 comments
Closed

cx_freeze problem with pytorch #2495

MJT369 opened this issue Jul 9, 2024 · 6 comments

Comments

@MJT369
Copy link

MJT369 commented Jul 9, 2024

Hello,
I have encountered a problem to convert a python script to exe with cx_freeze that uses the Argos translate library and is written on PyTorch. When the file is converted to exe with cx_freeze , it gives an error when calling Torch.

[WinError 193] %1 is not a valid Win32 application. Error loading "N:\taraz_sofware\build\exe.win-amd64-3.11\lib\torch\lib\dbghelp.dll" or one of its dependencies.

  • Platform information :windows 10
  • OS architecture : win10 64bit
  • cx_Freeze version : cx-Freeze 7.1.1
  • Python version : 3.11
    here is example code :
import sys
import os
import tempfile
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QLabel, QPushButton, QComboBox, \
    QVBoxLayout, QHBoxLayout, QWidget, QFileDialog
from PyQt5.QtGui import QFont, QColor, QTextCursor
import argostranslate.translate as Translator
import fitz
import docx

class TranslationWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("MJT_Translator_313")
        self.setGeometry(100, 100, 800, 600)

        self.text_font = QFont("Segoe UI", 12)  # Default font

        self.input_console = QTextEdit(self)
        self.input_console.setFont(self.text_font)
        self.input_console.setStyleSheet("background-color: white; color: black;")

        self.translated_parts = []  # List to store translated parts
        self.current_part = 0
        self.parts_text = []

        self.output_console = QTextEdit(self)
        self.output_console.setFont(self.text_font)
        self.output_console.setStyleSheet("background-color: white; color: blue;")
        self.output_console.setReadOnly(True)

        self.source_language_label = QLabel("Select source language:")
        self.source_language_combo = QComboBox()
        self.source_language_combo.addItems(["English", "Persian",])

        self.target_language_label = QLabel("Select target language:")
        self.target_language_combo = QComboBox()
        self.target_language_combo.addItems(["Persian", "English",])

        self.file_button = QPushButton("Select PDF or Word file")
        self.translate_button = QPushButton("Translate")
        self.export_button = QPushButton("Save translated text")
        self.load_button = QPushButton("Load Next part (each 3 pages)")
        self.clear_button = QPushButton("CLEAR")
        self.save_parts_button = QPushButton("Save Parts")

        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        layout.addWidget(self.input_console)
        layout.addWidget(self.output_console)

        source_layout = QHBoxLayout()
        source_layout.addWidget(self.source_language_label)
        source_layout.addWidget(self.source_language_combo)

        target_layout = QHBoxLayout()
        target_layout.addWidget(self.target_language_label)
        target_layout.addWidget(self.target_language_combo)

        layout.addLayout(source_layout)
        layout.addLayout(target_layout)
        layout.addWidget(self.translate_button)
        
        layout.addWidget(self.clear_button)

        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        self.translate_button.clicked.connect(self.translate_text)
        self.clear_button.clicked.connect(self.clear)


    def translate_text(self):
        source_language = self.source_language_combo.currentText()
        target_language = self.target_language_combo.currentText()
        self.language_codes = {
                'English': "en",
                
                'Farsi': "fa",
             }
        self.from_code = self.language_codes.get(source_language)
        self.to_code = self.language_codes.get(target_language)
        text = self.input_console.toPlainText()
        try:
            #translator = Translator
            translated_text=Translator.translate(text, self.from_code, self.to_code)
            self.translated_parts.append(translated_text)  # Append translated part to the list
        except Exception as e:
            translated_text = f"PLEASE CHECK INTERNET CONNECTION, An error occurred during translation: {e}"
        self.output_console.setPlainText(translated_text)

    def clear(self):
        self.input_console.clear()
        self.output_console.clear()
        self.translated_parts.clear()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = TranslationWindow()
    win.show()
    sys.exit(app.exec_())
#using cx_freeze setup.py    ... python setup.py build
import sys
from cx_Freeze import setup, Executable
company_name = 'MJT'
product_name = 'Taraz document editor'
sys.setrecursionlimit(5000)
bdist_msi_options = {
    'add_to_path': False,
    'initial_target_dir': r'[C:\Program Files (x86)]\%s\%s' % (company_name, product_name),
    }
path = sys.path
build_exe_options = {
"path": path,
"icon": "icon.ico"}
base = "Win32GUI"
#Qt_tr_313 taraz_limited
exe = Executable(script='Tr_gui.py',
                 base=base,
                 icon='icon.ico',
                 copyright="© 2024 [Mjt +989914604366]. All rights reserved."
                )

setup(name = "Taraz software",
      version = "1.1",
      description = "Edit and translate  text or document",
      executables = [exe],
      options = {'bdist_msi': bdist_msi_options})
@marcelotduarte
Copy link
Owner

How to reproduce? (A minimal sample program).

@MJT369
Copy link
Author

MJT369 commented Jul 31, 2024

How to reproduce? (A minimal sample program).

first commend edited.

@marcelotduarte
Copy link
Owner

marcelotduarte commented Sep 22, 2024

I'm trying to reproduce it, but I have doubts about the requirements. They would be:
pyqt5
argostranslate
fitz
docx

Apparently, fitz and docx would be for py2 and causes syntax errors. Please point me to the correct packages.

@marcelotduarte
Copy link
Owner

Using example from https://pypi.org/project/argostranslate/ it runs and freezes.
Your sample does not run in python either (removing fitz and docx imports).
It need improvements to use argostranslate.package like in the example.
Also, the self.to_code is None.
If you get your sample run in python, you can get it frozen.
Can I close this issue?

@MJT369
Copy link
Author

MJT369 commented Sep 25, 2024 via email

@marcelotduarte
Copy link
Owner

For pdf2docx I made a fix recently #2581 and was released in v7.2.2

@MJT369 MJT369 closed this as completed Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants