mirror of
https://github.com/modelscope/modelscope.git
synced 2025-12-16 08:17:45 +01:00
remove assets and download from oss when running app (#875)
This commit is contained in:
@@ -3,8 +3,10 @@ import inspect
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import tarfile
|
||||
|
||||
import gradio as gr
|
||||
import requests
|
||||
from challenges.ch1 import challenge1
|
||||
from challenges.ch2 import challenge2
|
||||
from challenges.ch3 import challenge3
|
||||
@@ -158,6 +160,49 @@ def generate_share_image(state):
|
||||
return gr.Image.update(visible=True, value=img_pil)
|
||||
|
||||
|
||||
def download_resource(url, extract_path='.'):
|
||||
"""
|
||||
下载资源文件,解压到指定路径。
|
||||
|
||||
Args:
|
||||
url: 要下载的文件的URL
|
||||
extract_path: 解压文件的目标路径
|
||||
"""
|
||||
try:
|
||||
# 定义文件名
|
||||
filename = url.split('/')[-1]
|
||||
|
||||
# 下载文件
|
||||
print(f'Downloading the file from {url}...')
|
||||
response = requests.get(url, stream=True)
|
||||
if response.status_code == 200:
|
||||
with open(filename, 'wb') as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
else:
|
||||
print(
|
||||
f'Error: Unable to download file. Status code: {response.status_code}'
|
||||
)
|
||||
return
|
||||
|
||||
# 解压文件
|
||||
print(f'Extracting the file to {extract_path}...')
|
||||
if tarfile.is_tarfile(filename):
|
||||
with tarfile.open(filename, 'r:*') as tar:
|
||||
tar.extractall(path=extract_path)
|
||||
else:
|
||||
print('Error: The downloaded file is not a tar file.')
|
||||
|
||||
# 删除临时文件
|
||||
print(f'Removing the temporary file {filename}...')
|
||||
os.remove(filename)
|
||||
print(
|
||||
'File downloaded, extracted, and temporary file removed successfully.'
|
||||
)
|
||||
except Exception as e:
|
||||
print(f'An error occurred: {e}')
|
||||
|
||||
|
||||
def create_app():
|
||||
# Gradio界面构建
|
||||
block = gr.Blocks()
|
||||
@@ -222,4 +267,8 @@ def create_app():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not os.path.exists('assets'):
|
||||
download_resource(
|
||||
'https://modelscope.oss-cn-beijing.aliyuncs.com/resource/llm_riddles_assets.tar'
|
||||
)
|
||||
create_app()
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:8afcec15a87bcfaff327a5c9564a31ff1fe185a63cb286bd9772c8c68216768a
|
||||
size 757003
|
||||
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:16afb18994ad0654b31117931aad2ee05863492e964e10f4c559556e29618320
|
||||
size 839643
|
||||
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:8afcec15a87bcfaff327a5c9564a31ff1fe185a63cb286bd9772c8c68216768a
|
||||
size 757003
|
||||
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:966a013913042e1574ccbc299b1914272cb47df69a552bf1723b96b2d8902de3
|
||||
size 1114172
|
||||
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:5253bbed99be55e6ac9080ea320df75c95592204696d6d41ba90f9905384fdca
|
||||
size 1198295
|
||||
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:4cf462f8db7583843bc152ccfc87bb033b91880c98db9f83ba87fcca5d5d07f2
|
||||
size 1056053
|
||||
Binary file not shown.
Reference in New Issue
Block a user