1inkusFace commited on
Commit
cdce176
·
verified ·
1 Parent(s): 75175ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -7
app.py CHANGED
@@ -19,6 +19,10 @@ import gradio as gr
19
  import numpy as np
20
  import random
21
 
 
 
 
 
22
  import torch
23
  torch.backends.cuda.matmul.allow_tf32 = False
24
  torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
@@ -58,19 +62,81 @@ FTP_HOST = 'noahcohn.com'
58
  FTP_USER = 'ford442'
59
  FTP_PASS = os.getenv("FTP_PASS")
60
  FTP_DIR = 'img.noahcohn.com/stablediff/'
 
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def upload_to_ftp(filename):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  try:
64
- transport = paramiko.Transport((FTP_HOST, 22))
65
- destination_path=FTP_DIR+filename
66
- transport.connect(username = FTP_USER, password = FTP_PASS)
67
- sftp = paramiko.SFTPClient.from_transport(transport)
 
 
 
 
 
 
 
 
 
 
 
 
68
  sftp.put(filename, destination_path)
 
 
 
69
  sftp.close()
70
- transport.close()
71
- print(f"Uploaded {filename} to FTP server")
 
72
  except Exception as e:
73
- print(f"FTP upload error: {e}")
74
  '''
75
 
76
  pyx = cyper.inline(code, fast_indexing=True, directives=dict(boundscheck=False, wraparound=False, language_level=3))
 
19
  import numpy as np
20
  import random
21
 
22
+ import socket
23
+ import threading # NEW IMPORT
24
+ import queue # NEW IMPORT
25
+
26
  import torch
27
  torch.backends.cuda.matmul.allow_tf32 = False
28
  torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
 
62
  FTP_USER = 'ford442'
63
  FTP_PASS = os.getenv("FTP_PASS")
64
  FTP_DIR = 'img.noahcohn.com/stablediff/'
65
+ FTP_HOST_FALLBACK = '1ink.us'
66
+ FTP_DIR_FALLBACK = 'img.1ink.us/stablediff/'
67
 
68
+ # --- WORKER FUNCTION FOR THREADING ---
69
+ # This function contains the logic to connect to a single host.
70
+ # It will be executed by each of our threads.
71
+ def connect_worker(host, result_queue):
72
+ """Tries to connect to a single host and puts the successful transport object into the queue."""
73
+ transport = None
74
+ try:
75
+ transport = paramiko.Transport((host, 22))
76
+ # We still use the 5-second timeout for the handshake
77
+ transport.start_client(timeout=5)
78
+ transport.auth_password(username=FTP_USER, password=FTP_PASS)
79
+
80
+ # If we reach here, the connection was successful.
81
+ # Put the result in the queue for the main thread to use.
82
+ print(f"✅ Connection to {host} succeeded first.")
83
+ result_queue.put(transport)
84
+ except (paramiko.SSHException, socket.timeout, EOFError) as e:
85
+ # This is an expected failure, just print a note.
86
+ print(f"ℹ️ Connection to {host} failed or was too slow: {e}")
87
+ if transport:
88
+ transport.close()
89
+ except Exception as e:
90
+ # Handle any other unexpected errors.
91
+ print(f"❌ Unexpected error connecting to {host}: {e}")
92
+ if transport:
93
+ transport.close()
94
+
95
  def upload_to_ftp(filename):
96
+ """
97
+ Attempts to connect to two FTP hosts simultaneously and uses the first one that responds.
98
+ It now uses a corresponding directory for the primary and fallback hosts.
99
+ """
100
+ hosts = [FTP_HOST]
101
+ if FTP_HOST_FALLBACK:
102
+ hosts.append(FTP_HOST_FALLBACK)
103
+
104
+ result_queue = queue.Queue()
105
+ threads = []
106
+
107
+ print(f"--> Racing connections to {hosts} for uploading {filename}...")
108
+ for host in hosts:
109
+ thread = threading.Thread(target=connect_worker, args=(host, result_queue))
110
+ thread.daemon = True
111
+ thread.start()
112
+ threads.append(thread)
113
  try:
114
+ winning_transport = result_queue.get(timeout=7)
115
+
116
+ # --- THIS IS THE NEW LOGIC ---
117
+ # 1. Determine which host won the race.
118
+ winning_host = winning_transport.getpeername()[0]
119
+
120
+ # 2. Select the correct destination directory based on the winning host.
121
+ # If the fallback directory isn't specified, it safely defaults to the primary directory.
122
+ if winning_host == FTP_HOST:
123
+ destination_directory = FTP_DIR
124
+ else:
125
+ destination_directory = FTP_DIR_FALLBACK if FTP_DIR_FALLBACK else FTP_DIR
126
+ print(f"--> Proceeding with upload to {winning_host} in directory {destination_directory}...")
127
+ # 3. Construct the full destination path using the selected directory.
128
+ sftp = paramiko.SFTPClient.from_transport(winning_transport)
129
+ destination_path = os.path.join(destination_directory, os.path.basename(filename))
130
  sftp.put(filename, destination_path)
131
+
132
+ print(f"✅ Successfully uploaded {filename}.")
133
+
134
  sftp.close()
135
+ winning_transport.close()
136
+ except queue.Empty:
137
+ print("❌ Critical Error: Neither FTP host responded in time.")
138
  except Exception as e:
139
+ print(f" An unexpected error occurred during SFTP operation: {e}")
140
  '''
141
 
142
  pyx = cyper.inline(code, fast_indexing=True, directives=dict(boundscheck=False, wraparound=False, language_level=3))