Spaces:
Running
Running
admin
commited on
Commit
·
3f72bb9
1
Parent(s):
00617a2
sync ms
Browse files
app.py
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from utils import download_file, EN_US, API_QR, TMP_DIR
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
ZH2EN = {
|
| 5 |
"二维码输出尺寸": "Image size",
|
| 6 |
"输入文本": "Input text",
|
|
@@ -15,6 +25,24 @@ def _L(zh_txt: str):
|
|
| 15 |
return ZH2EN[zh_txt] if EN_US else zh_txt
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def infer(img_size: int, input_txt: str):
|
| 19 |
status = "Success"
|
| 20 |
img = None
|
|
@@ -23,7 +51,7 @@ def infer(img_size: int, input_txt: str):
|
|
| 23 |
raise ValueError("Please input valid text!")
|
| 24 |
|
| 25 |
img = download_file(
|
| 26 |
-
f"{API_QR}/?size={img_size}x{img_size}&data={input_txt}",
|
| 27 |
f"{TMP_DIR}/qrcode.jpg",
|
| 28 |
)
|
| 29 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import requests
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 7 |
+
API_QR = os.getenv("api_qr")
|
| 8 |
+
if not API_QR:
|
| 9 |
+
print("请检查环境变量")
|
| 10 |
+
exit()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
TMP_DIR = "./__pycache__"
|
| 14 |
ZH2EN = {
|
| 15 |
"二维码输出尺寸": "Image size",
|
| 16 |
"输入文本": "Input text",
|
|
|
|
| 25 |
return ZH2EN[zh_txt] if EN_US else zh_txt
|
| 26 |
|
| 27 |
|
| 28 |
+
def clean_dir(dir_path: str):
|
| 29 |
+
if os.path.exists(dir_path):
|
| 30 |
+
shutil.rmtree(dir_path)
|
| 31 |
+
|
| 32 |
+
os.makedirs(dir_path)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def download_file(url, local_filename):
|
| 36 |
+
clean_dir(os.path.dirname(local_filename))
|
| 37 |
+
response = requests.get(url, stream=True)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
with open(local_filename, "wb") as f:
|
| 40 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 41 |
+
f.write(chunk)
|
| 42 |
+
|
| 43 |
+
return local_filename
|
| 44 |
+
|
| 45 |
+
|
| 46 |
def infer(img_size: int, input_txt: str):
|
| 47 |
status = "Success"
|
| 48 |
img = None
|
|
|
|
| 51 |
raise ValueError("Please input valid text!")
|
| 52 |
|
| 53 |
img = download_file(
|
| 54 |
+
f"{API_QR}/?size={img_size}x{img_size}&data={input_txt}" if EN_US else f"{API_QR}/?text={input_txt}&size={img_size}",
|
| 55 |
f"{TMP_DIR}/qrcode.jpg",
|
| 56 |
)
|
| 57 |
|
utils.py
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import shutil
|
| 3 |
-
import requests
|
| 4 |
-
|
| 5 |
-
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 6 |
-
API_QR = os.getenv("api_qr")
|
| 7 |
-
if not API_QR:
|
| 8 |
-
print("请检查环境变量")
|
| 9 |
-
exit()
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
TMP_DIR = "./__pycache__"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def clean_dir(dir_path: str):
|
| 16 |
-
if os.path.exists(dir_path):
|
| 17 |
-
shutil.rmtree(dir_path)
|
| 18 |
-
|
| 19 |
-
os.makedirs(dir_path)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def download_file(url, local_filename):
|
| 23 |
-
clean_dir(os.path.dirname(local_filename))
|
| 24 |
-
response = requests.get(url, stream=True)
|
| 25 |
-
response.raise_for_status()
|
| 26 |
-
with open(local_filename, "wb") as f:
|
| 27 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 28 |
-
f.write(chunk)
|
| 29 |
-
|
| 30 |
-
return local_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|