Spaces:
Sleeping
Sleeping
Yaron Koresh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -112,14 +112,7 @@ function custom(){
|
|
112 |
|
113 |
@cpu(string(string),cache=True,parallel=True)
|
114 |
def run(cmd):
|
115 |
-
|
116 |
-
result = subprocess.run(cmd, shell=True, capture_output=True, env=None)
|
117 |
-
if result.returncode != 0:
|
118 |
-
logging.error(
|
119 |
-
f"Command '{cmd}' failed with exit status code '{result.returncode}'. Exiting..."
|
120 |
-
)
|
121 |
-
sys.exit()
|
122 |
-
return str(result.stdout)
|
123 |
|
124 |
@gpu(string(string,string),device=True,inline=True)
|
125 |
def translate(text,lang):
|
@@ -142,20 +135,18 @@ def translate(text,lang):
|
|
142 |
padded_chars = re.sub("[(^\-)(\-$)]","",text.replace("","-").replace("- -"," ")).strip()
|
143 |
query_text = f'Please translate {padded_chars}, into {lang}'
|
144 |
url = f'https://www.google.com/search?q={query_text}'
|
145 |
-
|
146 |
url = url,
|
147 |
headers = {
|
148 |
'User-Agent': random.choice(user_agents)
|
149 |
}
|
150 |
-
)
|
151 |
-
content = resp.content
|
152 |
-
html = fromstring(content)
|
153 |
translated = text
|
154 |
try:
|
155 |
-
src_lang =
|
156 |
-
trgt_lang =
|
157 |
-
src_text =
|
158 |
-
trgt_text =
|
159 |
if trgt_lang == lang:
|
160 |
translated = trgt_text
|
161 |
except:
|
@@ -166,8 +157,7 @@ def translate(text,lang):
|
|
166 |
|
167 |
@gpu(string(int),device=True,inline=True)
|
168 |
def generate_random_string(length):
|
169 |
-
|
170 |
-
characters = string.ascii_letters + string.digits
|
171 |
return ''.join(random.choice(characters) for _ in range(length))
|
172 |
|
173 |
@gpu(void(),device=True,inline=True)
|
@@ -216,11 +206,12 @@ def infer():
|
|
216 |
global pinp
|
217 |
global out
|
218 |
|
219 |
-
out =[]
|
|
|
220 |
|
221 |
-
p1 = pm["p"]
|
222 |
|
223 |
-
neg = pm["n"]
|
224 |
if neg != "":
|
225 |
neg = f"{neg} where in the image"
|
226 |
|
@@ -338,7 +329,6 @@ def events():
|
|
338 |
@cpu(void(),cache=True,parallel=True)
|
339 |
def entry():
|
340 |
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
341 |
-
mp.set_start_method("spawn", force=True)
|
342 |
pre[1,32]()
|
343 |
ui[1,32]()
|
344 |
events[1,32]()
|
|
|
112 |
|
113 |
@cpu(string(string),cache=True,parallel=True)
|
114 |
def run(cmd):
|
115 |
+
return str(subprocess.run(cmd, shell=True, capture_output=True, env=None).stdout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
@gpu(string(string,string),device=True,inline=True)
|
118 |
def translate(text,lang):
|
|
|
135 |
padded_chars = re.sub("[(^\-)(\-$)]","",text.replace("","-").replace("- -"," ")).strip()
|
136 |
query_text = f'Please translate {padded_chars}, into {lang}'
|
137 |
url = f'https://www.google.com/search?q={query_text}'
|
138 |
+
content = str(requests.get(
|
139 |
url = url,
|
140 |
headers = {
|
141 |
'User-Agent': random.choice(user_agents)
|
142 |
}
|
143 |
+
).content)
|
|
|
|
|
144 |
translated = text
|
145 |
try:
|
146 |
+
src_lang = fromstring(content).xpath('//*[@class="source-language"]')[0].text_content().lower().strip()
|
147 |
+
trgt_lang = fromstring(content).xpath('//*[@class="target-language"]')[0].text_content().lower().strip()
|
148 |
+
src_text = fromstring(content).xpath('//*[@id="tw-source-text"]/*')[0].text_content().lower().strip()
|
149 |
+
trgt_text = fromstring(content).xpath('//*[@id="tw-target-text"]/*')[0].text_content().lower().strip()
|
150 |
if trgt_lang == lang:
|
151 |
translated = trgt_text
|
152 |
except:
|
|
|
157 |
|
158 |
@gpu(string(int),device=True,inline=True)
|
159 |
def generate_random_string(length):
|
160 |
+
characters = str(string.ascii_letters + string.digits)
|
|
|
161 |
return ''.join(random.choice(characters) for _ in range(length))
|
162 |
|
163 |
@gpu(void(),device=True,inline=True)
|
|
|
206 |
global pinp
|
207 |
global out
|
208 |
|
209 |
+
out = [""]
|
210 |
+
out.remove("")
|
211 |
|
212 |
+
p1 = str(pm["p"])
|
213 |
|
214 |
+
neg = str(pm["n"])
|
215 |
if neg != "":
|
216 |
neg = f"{neg} where in the image"
|
217 |
|
|
|
329 |
@cpu(void(),cache=True,parallel=True)
|
330 |
def entry():
|
331 |
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
|
|
332 |
pre[1,32]()
|
333 |
ui[1,32]()
|
334 |
events[1,32]()
|