marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.36k stars 219 forks source link

cx_freeze problem with pytorch #2495

Closed MJT369 closed 1 month ago

MJT369 commented 4 months ago

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.

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 commented 3 months ago

How to reproduce? (A minimal sample program).

MJT369 commented 3 months ago

How to reproduce? (A minimal sample program).

first commend edited.

marcelotduarte commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

As that part of large code not working properly  just sent for find solution for cx_freeze But I test cx_freeze with other pc and freezes script include torch well working but another problem in that pc with pdf2docx that not working ... However pdf2docx working fine with main pc and torch not working with...  On Monday, September 23, 2024 at 04:29:48 AM GMT+3:30, Marcelo Duarte @.***> wrote:

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?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

marcelotduarte commented 1 month ago

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