Spaces:
Runtime error
Runtime error
File size: 3,368 Bytes
38b7815 56cf38b 38b7815 22ba884 56cf38b 22ba884 56cf38b 22ba884 56cf38b 679a546 56cf38b 22ba884 56cf38b 22ba884 56cf38b 679a546 22ba884 38b7815 679a546 22ba884 56cf38b 22ba884 56cf38b 22ba884 679a546 22ba884 56cf38b 38b7815 56cf38b 38b7815 22ba884 679a546 39d4f47 56cf38b 39d4f47 22ba884 39d4f47 22ba884 39d4f47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
"""Failed attempt to fix "This event loop is already running" in Widows:
from platform import platform
if platform().lower().startswith("windows"):
from install import install
try:
import nest_asyncio
except ModuleNotFoundError:
try:
install("nest-asyncio")
import nest_asyncio
except Exception as exc:
print(exc)
raise SystemExit(1)
nest_asyncio.apply()
from gevent import monkey
monkey.patch_all()
# """
# pylint: disable=broad-except
# from gevent import monkey
# monkey.patch_all()
# import nest_asyncio
# nest_asyncio.apply()
import asyncio
from multiprocessing import Process # , freeze_support
import gradio as gr
import httpx
# from deepl_fastapi.run_uvicorn import main
# from deepl_fastapi_pw.run_uvicorn_async import main
from deepl_fastapi_pw.deepl_server_async import main
from logzero import logger
# from deepl_scraper_pw.deepl_tr import deepl_tr
from split_text import split_text
arun = asyncio.get_event_loop().run_until_complete
def deepl(text, from_lang, to_lang):
"""Translate."""
try:
text = str(text).strip()
except Exception:
text = ""
if not text:
return "Put something there, man."
# "http://127.0.0.1:8000/text/?q=test%20me&to_lang=zh"
url = "http://127.0.0.1:8000/text/"
url = "http://127.0.0.1:8001/text/"
res_tot = ""
text_tot = text[:]
slash = "_slash_"
text_tot = text_tot.replace("/", slash)
_ = split_text(text_tot)
len_ = len(_)
logger.info("Split to %s", len_)
for idx, chunk in enumerate(_):
logger.info("%s/%s", idx + 1, len_)
try:
# resp = httpx.get(f"{url}?q={text}&from_lang={from_lang}&to_lang={to_lang}")
resp = httpx.post(
url,
json={
"text": chunk,
"from_lang": from_lang,
"to_lang": to_lang,
},
timeout=90,
)
resp.raise_for_status()
except Exception as exc:
logger.error(exc)
return str(exc)
try:
jdata = resp.json()
except Exception as exc:
logger.error(exc)
return str(exc)
try:
# res = jdata.get("trtext")
res = jdata.get("result")
except Exception as exc:
logger.error(exc)
res = str(exc)
if res_tot:
res_tot = res_tot + "\n" + res
else:
res_tot = res
_ = """
try:
# _ = deepl_tr(text, from_lang, to_lang)
_ = arun(deepl_tr(text, from_lang, to_lang))
except Exception as exc:
logger.error(exc)
return str(exc)
# """
# return res
res_tot = res_tot.replace(slash, "/")
return res_tot
if __name__ == "__main__":
# freeze_support()
Process(target=main).start()
iface = gr.Interface(
fn=deepl,
inputs=[
gr.Textbox(
placeholder="Paste text here (not limited to 5000 chars)",
lines=7,
),
gr.Textbox(label="from_lang", value="en", lines=1),
gr.Textbox(label="to_lang", value="zh", lines=1),
],
outputs="textarea",
allow_flagging="never",
)
iface.launch()
|