freemt commited on
Commit
362de3f
·
1 Parent(s): 1f95340
Files changed (5) hide show
  1. .flake8 +14 -0
  2. .gitignore +1 -0
  3. app.py +30 -11
  4. gradio_queue.db +0 -0
  5. pyproject.toml +8 -0
.flake8 ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [flake8]
2
+ # E503: line break before binary operator, black's format style
3
+ ignore = D203,E501,W503
4
+ exclude =
5
+ .git,
6
+ __pycache__,
7
+ docs/source/conf.py,
8
+ old,
9
+ build,
10
+ dist,
11
+ .venv,
12
+ pad*.py
13
+ max-line-length = 120
14
+ max-complexity = 30
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio_queue.db
app.py CHANGED
@@ -1,19 +1,19 @@
1
  """See https://huggingface.co/spaces/Gradio-Blocks/Story-to-video/blob/main/app.py."""
2
  import base64
3
  import io
 
4
  import os
5
  import re
6
  import time
7
- from random import choice
8
 
9
  import gradio as gr
10
  import translators as ts
11
  from fastlid import fastlid
12
  from logzero import logger
13
  from PIL import Image # opencv-python
14
- from random import choices
15
  from tenacity import retry
16
- from tenacity.stop import stop_after_attempt # , stop_after_delay
17
 
18
  # from PIL import Image
19
  # from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,pipeline
@@ -29,11 +29,14 @@ try:
29
  except Exception:
30
  ... # Windows
31
 
 
 
 
32
 
33
  examples_ = [
34
- "黑云压城城欲摧 ,甲光向日金鳞开。",
35
  "黄金在河里流淌,宝石遍地,空中铺满巨大的彩虹。",
36
  "蓝色的夜,森林中好多萤火虫",
 
37
  "季姬寂,集鸡,鸡即棘鸡。棘鸡饥叽,季姬及箕稷济鸡。",
38
  "an apple",
39
  "a cat",
@@ -42,9 +45,13 @@ examples_ = [
42
  ]
43
 
44
 
45
- # @retry(stop=(stop_after_delay(10) | stop_after_attempt(5)))
46
- @retry(stop=stop_after_attempt(5))
47
- def tr_(text: str) -> str:
 
 
 
 
48
  """Wrap [ts.deepl, ts.baidu, ts.google] with tenacity.
49
 
50
  not working: sogou; ?tencent
@@ -52,14 +59,18 @@ def tr_(text: str) -> str:
52
  cand = [ts.baidu, ts.youdao, ts.google]
53
  for tr in [ts.deepl] + choices(cand, k=len(cand)):
54
  try:
55
- res = tr(text)
 
 
 
 
56
  logger.info(" api used: %s", tr.__name__)
57
  tr_.api_used = tr.__name__
58
  break
59
  except Exception:
60
  continue
61
  else:
62
- res = "Something is probably wong, ping dev to fix it if you like."
63
 
64
  return res
65
 
@@ -76,13 +87,21 @@ def generate_images(phrase: str, steps: int = 125):
76
  except Exception as exc:
77
  logger.error(exc)
78
 
 
 
 
79
  # safeguard short Chinese phrases
80
- if len(phrase) < 10 and re.search(r"[一-龟]+", phrase):
81
  detected = "zh"
 
82
 
83
  if detected not in ["en"]:
84
  try:
85
- generated_text = tr_(phrase)
 
 
 
 
86
  extra_info = f"({tr_.api_used}: {generated_text})"
87
  except Exception as exc:
88
  logger.error(exc)
 
1
  """See https://huggingface.co/spaces/Gradio-Blocks/Story-to-video/blob/main/app.py."""
2
  import base64
3
  import io
4
+ import logzero
5
  import os
6
  import re
7
  import time
8
+ from random import choice, choices
9
 
10
  import gradio as gr
11
  import translators as ts
12
  from fastlid import fastlid
13
  from logzero import logger
14
  from PIL import Image # opencv-python
 
15
  from tenacity import retry
16
+ from tenacity.stop import stop_after_attempt, stop_after_delay
17
 
18
  # from PIL import Image
19
  # from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,pipeline
 
29
  except Exception:
30
  ... # Windows
31
 
32
+ loglevel = 10 # change to 20 to switch off debug
33
+ logzero.loglevel(loglevel)
34
+
35
 
36
  examples_ = [
 
37
  "黄金在河里流淌,宝石遍地,空中铺满巨大的彩虹。",
38
  "蓝色的夜,森林中好多萤火虫",
39
+ "黑云压城城欲摧 ,甲光向日金鳞开。",
40
  "季姬寂,集鸡,鸡即棘鸡。棘鸡饥叽,季姬及箕稷济鸡。",
41
  "an apple",
42
  "a cat",
 
45
  ]
46
 
47
 
48
+ # @retry(stop=stop_after_attempt(5))
49
+ @retry(stop=(stop_after_delay(10) | stop_after_attempt(5)))
50
+ def tr_(
51
+ text: str,
52
+ from_language="auto",
53
+ to_language="en",
54
+ ) -> str:
55
  """Wrap [ts.deepl, ts.baidu, ts.google] with tenacity.
56
 
57
  not working: sogou; ?tencent
 
59
  cand = [ts.baidu, ts.youdao, ts.google]
60
  for tr in [ts.deepl] + choices(cand, k=len(cand)):
61
  try:
62
+ res = tr(
63
+ text,
64
+ from_language=from_language,
65
+ to_language=to_language,
66
+ )
67
  logger.info(" api used: %s", tr.__name__)
68
  tr_.api_used = tr.__name__
69
  break
70
  except Exception:
71
  continue
72
  else:
73
+ res = "Something is probably wrong, ping dev to fix it if you like."
74
 
75
  return res
76
 
 
87
  except Exception as exc:
88
  logger.error(exc)
89
 
90
+ logzero.loglevel(loglevel)
91
+ logger.debug("phrase: %s, deteted: %s", phrase, detected)
92
+
93
  # safeguard short Chinese phrases
94
+ if len(phrase) <= 10 and re.search(r"[一-龟]+", phrase):
95
  detected = "zh"
96
+ logger.debug(" safeguard branch ")
97
 
98
  if detected not in ["en"]:
99
  try:
100
+ generated_text = tr_(
101
+ phrase,
102
+ detected,
103
+ "en",
104
+ )
105
  extra_info = f"({tr_.api_used}: {generated_text})"
106
  except Exception as exc:
107
  logger.error(exc)
gradio_queue.db CHANGED
Binary files a/gradio_queue.db and b/gradio_queue.db differ
 
pyproject.toml CHANGED
@@ -34,3 +34,11 @@ export = "poetry export --without-hashes -f requirements.txt -o requirements.txt
34
  [tool.isort]
35
  profile = "black"
36
  multi_line_output = 3
 
 
 
 
 
 
 
 
 
34
  [tool.isort]
35
  profile = "black"
36
  multi_line_output = 3
37
+
38
+ [tool.pyright]
39
+ venvPath = ".venv"
40
+ reportTypeshedErrors = false
41
+ reportMissingImports = true
42
+ reportMissingTypeStubs = false
43
+ pythonVersion = "3.8"
44
+ ignore = []