Skip to content

Commit

Permalink
Argument Refactoring and Web Server Argument
Browse files Browse the repository at this point in the history
* Updated Arguments for valid languages
* Added port argument and detection. Web server will start if `portnumber` arg is added
* Web server will die peacefully rather forcefully
* Transcription will save when app is killed using `ctrl` + `c`
  • Loading branch information
cyberofficial committed Aug 7, 2023
1 parent 07ec714 commit f61b71a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
12 changes: 9 additions & 3 deletions modules/api_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import Flask, send_from_directory, url_for
from threading import Thread

def flask_server(operation, port):
def flask_server(operation, portnumber):
if operation == "start":
# Define paths
script_dir = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -15,7 +15,7 @@ def flask_server(operation, port):
app.config["DEBUG"] = False

# Set port number
port = 5000
port = portnumber

# Time to start the server
print("Starting Flask Server on port:", port)
Expand Down Expand Up @@ -52,4 +52,10 @@ def run():
app.do_teardown_appcontext()

# Start the server in a new thread
Thread(target=run).start()
Thread(target=run).start()

def kill_server():
print("Killing Server")
os._exit(0)

print("Web Server Module Loaded")
1 change: 1 addition & 0 deletions modules/checkenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ def env_message():
elif current_os == 'linux':
os.system('clear')

print("Check ENV Module Loaded")
print("Checking if you are in a virtual environment...\n\n")
19 changes: 15 additions & 4 deletions modules/parser_args.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from modules.imports import *
from modules.languages import get_valid_languages

# Define a constant variable for valid language choices
VALID_LANGUAGES = get_valid_languages()

def valid_port_number(value):
port = int(value)
if not 1 <= port <= 65535:
raise argparse.ArgumentTypeError(f"Invalid port number: {value}. Please choose a number between 1 and 65535.")
return port

def set_model_by_ram(ram, language):
ram = ram.lower()
Expand Down Expand Up @@ -40,10 +50,8 @@ def parse_arguments():
parser.add_argument("--no_log", action='store_true', help="Only show the last line of the transcription.")
parser.add_argument("--translate", action='store_true', help="Translate the transcriptions to English.")
parser.add_argument("--transcribe", action='store_true', help="transcribe the text into the desired language.")
parser.add_argument("--language", help="Language to translate from.", type=str,
choices=["af", "am", "ar", "as", "az", "ba", "be", "bg", "bn", "bo", "br", "bs", "ca", "cs", "cy", "da", "de", "el", "en", "es", "et", "eu", "fa", "fi", "fo", "fr", "gl", "gu", "ha", "haw", "he", "hi", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "jw", "ka", "kk", "km", "kn", "ko", "la", "lb", "ln", "lo", "lt", "lv", "mg", "mi", "mk", "ml", "mn", "mr", "ms", "mt", "my", "ne", "nl", "nn", "no", "oc", "pa", "pl", "ps", "pt", "ro", "ru", "sa", "sd", "si", "sk", "sl", "sn", "so", "sq", "sr", "su", "sv", "sw", "ta", "te", "tg", "th", "tk", "tl", "tr", "tt", "uk", "ur", "uz", "vi", "yi", "yo", "zh", "Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian", "Assamese", "Azerbaijani", "Bashkir", "Basque", "Belarusian", "Bengali", "Bosnian", "Breton", "Bulgarian", "Burmese", "Castilian", "Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian", "Faroese", "Finnish", "Flemish", "French", "Galician", "Georgian", "German", "Greek", "Gujarati", "Haitian", "Haitian Creole", "Hausa", "Hawaiian", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Italian", "Japanese", "Javanese", "Kannada", "Kazakh", "Khmer", "Korean", "Lao", "Latin", "Latvian", "Letzeburgesch", "Lingala", "Lithuanian", "Luxembourgish", "Macedonian", "Malagasy", "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Moldavian", "Moldovan", "Mongolian", "Myanmar", "Nepali", "Norwegian", "Nynorsk", "Occitan", "Panjabi", "Pashto", "Persian", "Polish", "Portuguese", "Punjabi", "Pushto", "Romanian", "Russian", "Sanskrit", "Serbian", "Shona", "Sindhi", "Sinhala", "Sinhalese", "Slovak", "Slovenian", "Somali", "Spanish", "Sundanese", "Swahili", "Swedish", "Tagalog", "Tajik", "Tamil", "Tatar", "Telugu", "Thai", "Tibetan", "Turkish", "Turkmen", "Ukrainian", "Urdu", "Uzbek", "Valencian", "Vietnamese", "Welsh", "Yiddish", "Yoruba"])
parser.add_argument("--target_language", help="Language to translate to.", type=str,
choices=["af", "am", "ar", "as", "az", "ba", "be", "bg", "bn", "bo", "br", "bs", "ca", "cs", "cy", "da", "de", "el", "en", "es", "et", "eu", "fa", "fi", "fo", "fr", "gl", "gu", "ha", "haw", "he", "hi", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "jw", "ka", "kk", "km", "kn", "ko", "la", "lb", "ln", "lo", "lt", "lv", "mg", "mi", "mk", "ml", "mn", "mr", "ms", "mt", "my", "ne", "nl", "nn", "no", "oc", "pa", "pl", "ps", "pt", "ro", "ru", "sa", "sd", "si", "sk", "sl", "sn", "so", "sq", "sr", "su", "sv", "sw", "ta", "te", "tg", "th", "tk", "tl", "tr", "tt", "uk", "ur", "uz", "vi", "yi", "yo", "zh", "Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian", "Assamese", "Azerbaijani", "Bashkir", "Basque", "Belarusian", "Bengali", "Bosnian", "Breton", "Bulgarian", "Burmese", "Castilian", "Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian", "Faroese", "Finnish", "Flemish", "French", "Galician", "Georgian", "German", "Greek", "Gujarati", "Haitian", "Haitian Creole", "Hausa", "Hawaiian", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Italian", "Japanese", "Javanese", "Kannada", "Kazakh", "Khmer", "Korean", "Lao", "Latin", "Latvian", "Letzeburgesch", "Lingala", "Lithuanian", "Luxembourgish", "Macedonian", "Malagasy", "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Moldavian", "Moldovan", "Mongolian", "Myanmar", "Nepali", "Norwegian", "Nynorsk", "Occitan", "Panjabi", "Pashto", "Persian", "Polish", "Portuguese", "Punjabi", "Pushto", "Romanian", "Russian", "Sanskrit", "Serbian", "Shona", "Sindhi", "Sinhala", "Sinhalese", "Slovak", "Slovenian", "Somali", "Spanish", "Sundanese", "Swahili", "Swedish", "Tagalog", "Tajik", "Tamil", "Tatar", "Telugu", "Thai", "Tibetan", "Turkish", "Turkmen", "Ukrainian", "Urdu", "Uzbek", "Valencian", "Vietnamese", "Welsh", "Yiddish", "Yoruba"])
parser.add_argument("--language", help="Language to translate from.", type=str, choices=VALID_LANGUAGES)
parser.add_argument("--target_language", help="Language to translate to.", type=str, choices=VALID_LANGUAGES)
parser.add_argument("--auto_model_swap", action='store_true', help="Automatically swap model based on detected language.")
parser.add_argument("--device", default="cuda", help="Device to use for model. If not specified, will use CUDA if available. Available options: cpu, cuda")
parser.add_argument("--cuda_device", default=0, help="CUDA device to use for model. If not specified, will use CUDA device 0.", type=int)
Expand All @@ -55,6 +63,9 @@ def parse_arguments():
parser.add_argument("--use_finetune", action='store_true', help="Use finetuned model.")
parser.add_argument("--updatebranch", default="master", help="Check which branch from the repo to check for updates. Default is master, choices are master and dev-testing and bleeding-under-work. To turn off update checks use disable. bleeding-under-work is basically latest changes and can break at any time.", choices=["master", "dev-testing", "disable", "bleeding-under-work"])
parser.add_argument("--keep_temp", action='store_true', help="Keep temporary audio files.")
# add portnumber argument, but make it optional, so if a portnumber is set we know to run the webserver if there is no portnumber we don't run the webserver, though make the choices 0-65535
parser.add_argument(
"--portnumber", default=None, help="Port number to run the web server on. If not specified, the web server will not run.", type=valid_port_number)
parser.add_argument("--about", action='store_true', help="About the project.")
args = parser.parse_args()
return args
Expand Down
54 changes: 34 additions & 20 deletions transcribe_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
isinenv = checkenv.in_virtualenv()

if isinenv == False:
checkenv.env_message()
print("Checking if portable version is being used...")
if os.path.exists("transcribe_audio.exe"):
print("Portable version detected, continuing with script...\n\n")
else:
checkenv.env_message()
else:
print("You are in a virtual environment, continuing with script...\n\n")

try:
print("Loading Primary Imports")
from modules.imports import *
print("\n\n")
except Exception as e:
print("Error Loading Primary Imports")
print("Check the Modules folder for the imports.py file and make sure it is not missing or corrupted.")
Expand All @@ -17,8 +22,6 @@

init()

api_backend.flask_server(operation="start", portnumber=5000)

try:
cuda_available = torch.cuda.is_available()
except:
Expand All @@ -29,6 +32,10 @@
def main():
args = parser_args.parse_arguments()

if args.portnumber:
print("Port number was set, so spinning up a web server...")
api_backend.flask_server(operation="start", portnumber=args.portnumber)

# if args.updatebranch is set as disable then skip
if args.updatebranch != "disable":
print("\nChecking for updates...")
Expand Down Expand Up @@ -561,24 +568,31 @@ def is_input_device(device_index):
print("Exiting...")
if args.discord_webhook:
send_to_discord_webhook(webhook_url, "Service has stopped.")
break
# break

if not os.path.isdir('out'):
os.mkdir('out')

transcript = os.path.join(os.getcwd(), 'out', 'transcription.txt')
if os.path.isfile(transcript):
transcript = os.path.join(os.getcwd(), 'out', 'transcription_' + str(len(os.listdir('out'))) + '.txt')
transcription_file = open(transcript, 'w', encoding='utf-8')

for original_text, translated_text, transcribed_text, detected_language in transcription:
transcription_file.write(f"-=-=-=-=-=-=-=-\nOriginal ({detected_language}): {original_text}\n")
if translated_text:
transcription_file.write(f"Translation: {translated_text}\n")
if transcribed_text:
transcription_file.write(f"Transcription: {transcribed_text}\n")
transcription_file.close()
print(f"Transcription was saved to {transcript}")

if args.portnumber:
api_backend.kill_server()

sys.exit(0)


if not os.path.isdir('out'):
os.mkdir('out')

transcript = os.path.join(os.getcwd(), 'out', 'transcription.txt')
if os.path.isfile(transcript):
transcript = os.path.join(os.getcwd(), 'out', 'transcription_' + str(len(os.listdir('out'))) + '.txt')
transcription_file = open(transcript, 'w', encoding='utf-8')

for original_text, translated_text, transcribed_text, detected_language in transcription:
transcription_file.write(f"-=-=-=-=-=-=-=-\nOriginal ({detected_language}): {original_text}\n")
if translated_text:
transcription_file.write(f"Translation: {translated_text}\n")
if transcribed_text:
transcription_file.write(f"Transcription: {transcribed_text}\n")
transcription_file.close()
print(f"Transcription was saved to {transcript}")


if __name__ == "__main__":
Expand Down

0 comments on commit f61b71a

Please sign in to comment.