Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +128 -33
src/streamlit_app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
4 |
-
import gdown
|
5 |
import zipfile
|
6 |
import os
|
7 |
import tempfile
|
8 |
import json
|
|
|
|
|
9 |
|
10 |
# ํ์ด์ง ์ค์
|
11 |
st.set_page_config(
|
@@ -17,6 +18,114 @@ st.set_page_config(
|
|
17 |
# ๊ตฌ๊ธ ๋๋ผ์ด๋ธ ํ์ผ ID
|
18 |
GDRIVE_FILE_ID = "1RilOqw77G_Kr9_Cl6kR2UTuBa3uvDWQ8"
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
@st.cache_resource
|
21 |
def download_and_load_model():
|
22 |
"""๊ตฌ๊ธ ๋๋ผ์ด๋ธ์์ ๋ชจ๋ธ ๋ค์ด๋ก๋ ๋ฐ ๋ก๋"""
|
@@ -26,42 +135,27 @@ def download_and_load_model():
|
|
26 |
zip_path = os.path.join(temp_dir, "koalpaca_cpu_deployment.zip")
|
27 |
extract_path = os.path.join(temp_dir, "koalpaca_cpu_deployment")
|
28 |
|
29 |
-
|
30 |
-
st.info("๐ฅ ๋ชจ๋ธ ๋ค์ด๋ก๋ ์ค...")
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
)
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# ๋ฐฉ๋ฒ 2: ํ์ผ ID๋ง ์ฌ์ฉ
|
48 |
-
st.info("๋์ ๋ค์ด๋ก๋ ๋ฐฉ๋ฒ ์๋ ์ค...")
|
49 |
-
gdown.download(
|
50 |
-
id=GDRIVE_FILE_ID,
|
51 |
-
output=zip_path,
|
52 |
-
quiet=False
|
53 |
-
)
|
54 |
-
except Exception as e2:
|
55 |
-
st.error(f"๋ ๋ฒ์งธ ๋ค์ด๋ก๋ ๋ฐฉ๋ฒ๋ ์คํจ: {e2}")
|
56 |
-
# ๋ฐฉ๋ฒ 3: ์ง์ URL ๊ตฌ์ฑ
|
57 |
-
st.info("์ต์ข
๋ค์ด๋ก๋ ๋ฐฉ๋ฒ ์๋ ์ค...")
|
58 |
-
import urllib.request
|
59 |
-
direct_url = f"https://drive.google.com/uc?export=download&id={GDRIVE_FILE_ID}"
|
60 |
-
urllib.request.urlretrieve(direct_url, zip_path)
|
61 |
|
62 |
# ํ์ผ ๋ค์ด๋ก๋ ํ์ธ
|
63 |
-
if not os.path.exists(zip_path):
|
64 |
-
raise FileNotFoundError("
|
65 |
|
66 |
st.success(f"โ
๋ค์ด๋ก๋ ์๋ฃ: {os.path.getsize(zip_path) / 1024**3:.2f}GB")
|
67 |
|
@@ -188,6 +282,7 @@ def main():
|
|
188 |
1. ๊ตฌ๊ธ ๋๋ผ์ด๋ธ ํ์ผ ๊ถํ์ '๋งํฌ๊ฐ ์๋ ๋ชจ๋ ์ฌ์ฉ์'๋ก ์ค์
|
189 |
2. ํ์ผ ID๊ฐ ์ ํํ์ง ํ์ธ
|
190 |
3. ํ์ผ ํฌ๊ธฐ๊ฐ ๋๋ฌด ํฐ ๊ฒฝ์ฐ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ์ ์์
|
|
|
191 |
""")
|
192 |
return
|
193 |
|
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
4 |
import zipfile
|
5 |
import os
|
6 |
import tempfile
|
7 |
import json
|
8 |
+
import urllib.request
|
9 |
+
import requests
|
10 |
|
11 |
# ํ์ด์ง ์ค์
|
12 |
st.set_page_config(
|
|
|
18 |
# ๊ตฌ๊ธ ๋๋ผ์ด๋ธ ํ์ผ ID
|
19 |
GDRIVE_FILE_ID = "1RilOqw77G_Kr9_Cl6kR2UTuBa3uvDWQ8"
|
20 |
|
21 |
+
def download_from_google_drive(file_id, destination):
|
22 |
+
"""๊ฒ์ ๊ฒฐ๊ณผ ๊ธฐ๋ฐ ๊ตฌ๊ธ ๋๋ผ์ด๋ธ ๋ค์ด๋ก๋ ํจ์"""
|
23 |
+
try:
|
24 |
+
# ๋ฐฉ๋ฒ 1: confirm=t ํ๋ผ๋ฏธํฐ ์ฌ์ฉ (๊ฒ์ ๊ฒฐ๊ณผ [3], [5]์์ ํ์ธ)
|
25 |
+
st.info("๐ฅ ๋ฐฉ๋ฒ 1: confirm=t ํ๋ผ๋ฏธํฐ๋ก ๋ค์ด๋ก๋ ์๋...")
|
26 |
+
url = f"https://drive.google.com/uc?export=download&id={file_id}&confirm=t"
|
27 |
+
|
28 |
+
response = requests.get(url, stream=True)
|
29 |
+
if response.status_code == 200:
|
30 |
+
with open(destination, 'wb') as f:
|
31 |
+
for chunk in response.iter_content(chunk_size=8192):
|
32 |
+
f.write(chunk)
|
33 |
+
return True
|
34 |
+
|
35 |
+
# ๋ฐฉ๋ฒ 2: ์ฟ ํค๋ฅผ ์ฌ์ฉํ ๋ค๋จ๊ณ ๋ค์ด๋ก๋ (๊ฒ์ ๊ฒฐ๊ณผ [5]์์ ํ์ธ)
|
36 |
+
st.info("๐ฅ ๋ฐฉ๋ฒ 2: ์ฟ ํค ๊ธฐ๋ฐ ๋ค์ด๋ก๋ ์๋...")
|
37 |
+
session = requests.Session()
|
38 |
+
|
39 |
+
# ์ฒซ ๋ฒ์งธ ์์ฒญ์ผ๋ก ์ฟ ํค ํ๋
|
40 |
+
response = session.get(f"https://drive.google.com/uc?export=download&id={file_id}")
|
41 |
+
|
42 |
+
# ํ์ธ ํ ํฐ ์ถ์ถ
|
43 |
+
for key, value in response.cookies.items():
|
44 |
+
if key.startswith('download_warning'):
|
45 |
+
token = value
|
46 |
+
break
|
47 |
+
else:
|
48 |
+
token = 't' # ๊ธฐ๋ณธ๊ฐ
|
49 |
+
|
50 |
+
# ๋ ๋ฒ์งธ ์์ฒญ์ผ๋ก ์ค์ ๋ค์ด๋ก๋
|
51 |
+
params = {'export': 'download', 'id': file_id, 'confirm': token}
|
52 |
+
response = session.get("https://drive.google.com/uc", params=params, stream=True)
|
53 |
+
|
54 |
+
if response.status_code == 200:
|
55 |
+
with open(destination, 'wb') as f:
|
56 |
+
for chunk in response.iter_content(chunk_size=8192):
|
57 |
+
f.write(chunk)
|
58 |
+
return True
|
59 |
+
|
60 |
+
# ๋ฐฉ๋ฒ 3: urllib ์ฌ์ฉ
|
61 |
+
st.info("๐ฅ ๋ฐฉ๋ฒ 3: urllib๋ก ๋ค์ด๋ก๋ ์๋...")
|
62 |
+
urllib.request.urlretrieve(
|
63 |
+
f"https://drive.google.com/uc?export=download&id={file_id}&confirm=t",
|
64 |
+
destination
|
65 |
+
)
|
66 |
+
return True
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
st.error(f"๋ค์ด๋ก๋ ์คํจ: {e}")
|
70 |
+
return False
|
71 |
+
|
72 |
+
def try_gdown_download(file_id, destination):
|
73 |
+
"""gdown์ ์ฌ์ฉํ ๋ค์ด๋ก๋ (๊ฒ์ ๊ฒฐ๊ณผ ํด๊ฒฐ์ฑ
์ ์ฉ)"""
|
74 |
+
try:
|
75 |
+
# ๊ฒ์ ๊ฒฐ๊ณผ [4]์์ ํ์ธ: ์ฟ ํค ํ์ผ ์ญ์
|
76 |
+
import gdown
|
77 |
+
|
78 |
+
# ์ฟ ํค ํ์ผ ๊ฒฝ๋ก๋ค
|
79 |
+
cookie_paths = [
|
80 |
+
os.path.expanduser("~/.cache/gdown/cookies.txt"),
|
81 |
+
os.path.expanduser("~/.cache/gdown/cookies.json"),
|
82 |
+
]
|
83 |
+
|
84 |
+
# ๊ธฐ์กด ์ฟ ํค ํ์ผ ์ญ์
|
85 |
+
for cookie_path in cookie_paths:
|
86 |
+
if os.path.exists(cookie_path):
|
87 |
+
os.remove(cookie_path)
|
88 |
+
st.info(f"๐๏ธ ๊ธฐ์กด ์ฟ ํค ํ์ผ ์ญ์ : {cookie_path}")
|
89 |
+
|
90 |
+
# ๊ฒ์ ๊ฒฐ๊ณผ [4]์์ ํ์ธ: gdown 4.6.0 ๋ฒ์ ์ฌ์ฉ ๊ถ์ฅ
|
91 |
+
st.info("๐ฅ gdown์ผ๋ก ๋ค์ด๋ก๋ ์๋...")
|
92 |
+
|
93 |
+
# ๋ฐฉ๋ฒ 1: ID๋ง ์ฌ์ฉ
|
94 |
+
try:
|
95 |
+
gdown.download(id=file_id, output=destination, quiet=False)
|
96 |
+
return True
|
97 |
+
except:
|
98 |
+
pass
|
99 |
+
|
100 |
+
# ๋ฐฉ๋ฒ 2: confirm=t ์ถ๊ฐ (๊ฒ์ ๊ฒฐ๊ณผ [6]์์ ํ์ธ)
|
101 |
+
try:
|
102 |
+
gdown.download(f"{file_id}&confirm=t", output=destination, quiet=False)
|
103 |
+
return True
|
104 |
+
except:
|
105 |
+
pass
|
106 |
+
|
107 |
+
# ๋ฐฉ๋ฒ 3: use_cookies=True ์ต์
(๊ฒ์ ๊ฒฐ๊ณผ [6]์์ ํ์ธ)
|
108 |
+
try:
|
109 |
+
gdown.download(
|
110 |
+
f"https://drive.google.com/uc?id={file_id}",
|
111 |
+
output=destination,
|
112 |
+
quiet=False,
|
113 |
+
fuzzy=True,
|
114 |
+
use_cookies=True
|
115 |
+
)
|
116 |
+
return True
|
117 |
+
except:
|
118 |
+
pass
|
119 |
+
|
120 |
+
return False
|
121 |
+
|
122 |
+
except ImportError:
|
123 |
+
st.warning("gdown์ด ์ค์น๋์ง ์์์ต๋๋ค. ๋์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.")
|
124 |
+
return False
|
125 |
+
except Exception as e:
|
126 |
+
st.warning(f"gdown ๋ค์ด๋ก๋ ์คํจ: {e}")
|
127 |
+
return False
|
128 |
+
|
129 |
@st.cache_resource
|
130 |
def download_and_load_model():
|
131 |
"""๊ตฌ๊ธ ๋๋ผ์ด๋ธ์์ ๋ชจ๋ธ ๋ค์ด๋ก๋ ๋ฐ ๋ก๋"""
|
|
|
135 |
zip_path = os.path.join(temp_dir, "koalpaca_cpu_deployment.zip")
|
136 |
extract_path = os.path.join(temp_dir, "koalpaca_cpu_deployment")
|
137 |
|
138 |
+
st.info("๐ฅ ๋ชจ๋ธ ๋ค์ด๋ก๋ ์์...")
|
|
|
139 |
|
140 |
+
# ๋ค์ด๋ก๋ ์๋ (์ฌ๋ฌ ๋ฐฉ๋ฒ)
|
141 |
+
download_success = False
|
142 |
|
143 |
+
# 1. gdown ์๋
|
144 |
+
if try_gdown_download(GDRIVE_FILE_ID, zip_path):
|
145 |
+
download_success = True
|
146 |
+
st.success("โ
gdown์ผ๋ก ๋ค์ด๋ก๋ ์ฑ๊ณต!")
|
147 |
+
|
148 |
+
# 2. requests ๊ธฐ๋ฐ ๋ค์ด๋ก๋ ์๋
|
149 |
+
elif download_from_google_drive(GDRIVE_FILE_ID, zip_path):
|
150 |
+
download_success = True
|
151 |
+
st.success("โ
requests๋ก ๋ค์ด๋ก๋ ์ฑ๊ณต!")
|
152 |
+
|
153 |
+
if not download_success:
|
154 |
+
raise Exception("๋ชจ๋ ๋ค์ด๋ก๋ ๋ฐฉ๋ฒ์ด ์คํจํ์ต๋๋ค.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
# ํ์ผ ๋ค์ด๋ก๋ ํ์ธ
|
157 |
+
if not os.path.exists(zip_path) or os.path.getsize(zip_path) < 1024:
|
158 |
+
raise FileNotFoundError("๋ค์ด๋ก๋๋ ํ์ผ์ด ์ ํจํ์ง ์์ต๋๋ค.")
|
159 |
|
160 |
st.success(f"โ
๋ค์ด๋ก๋ ์๋ฃ: {os.path.getsize(zip_path) / 1024**3:.2f}GB")
|
161 |
|
|
|
282 |
1. ๊ตฌ๊ธ ๋๋ผ์ด๋ธ ํ์ผ ๊ถํ์ '๋งํฌ๊ฐ ์๋ ๋ชจ๋ ์ฌ์ฉ์'๋ก ์ค์
|
283 |
2. ํ์ผ ID๊ฐ ์ ํํ์ง ํ์ธ
|
284 |
3. ํ์ผ ํฌ๊ธฐ๊ฐ ๋๋ฌด ํฐ ๊ฒฝ์ฐ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ์ ์์
|
285 |
+
4. gdown ์ฟ ํค ๋ฌธ์ ๋ ์๋์ผ๋ก ํด๊ฒฐ๋ฉ๋๋ค
|
286 |
""")
|
287 |
return
|
288 |
|