eclipse2 commited on
Commit
11ece3a
ยท
verified ยท
1 Parent(s): 6cc049e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -35
app.py CHANGED
@@ -1,51 +1,97 @@
 
 
1
  import os
2
  import traceback
3
  import streamlit as st
4
  from huggingface_hub import InferenceClient
5
  from huggingface_hub.errors import HfHubHTTPError
6
 
 
 
 
 
 
 
 
7
  st.set_page_config(page_title="๊ฐ„๋‹จํ•œ ์ฑ—๋ด‡")
8
- st.title("๐Ÿค– ๋ญ๋“ ์ง€ ๋ฌผ์–ด๋ณด์„ธ์š”~")
9
 
10
- # โ”€โ”€ Token: Spaces Secrets์— ๋„ฃ์œผ๋ฉด ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์ž๋™ ์ฃผ์ž…๋ฉ๋‹ˆ๋‹ค.
11
- # Settings โ†’ Variables and secrets โ†’ New secret:
12
- # Name: HUGGINGFACEHUB_API_TOKEN Value: hf_xxx...
13
- TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN")
14
 
15
- # โ”€โ”€ ๋ชจ๋ธ: text_generation ๊ฐ€๋Šฅํ•œ causal LM ์‚ฌ์šฉ
16
- MODEL_ID = os.environ.get("HF_MODEL", "microsoft/Phi-3-mini-4k-instruct")
 
 
 
 
 
17
 
18
  with st.sidebar:
19
- st.subheader("๐Ÿ” Token")
20
- tok_in = st.text_input("Hugging Face Token (hf_โ€ฆ)", type="password", value=TOKEN or "")
21
- if tok_in.strip():
22
- TOKEN = tok_in.strip()
23
- st.caption("Secrets์— ๋“ฑ๋กํ–ˆ์œผ๋ฉด ๋น„์›Œ๋‘ฌ๋„ ๋ฉ๋‹ˆ๋‹ค.")
24
-
25
- if not TOKEN:
26
- st.error("Hugging Face ํ† ํฐ์ด ์—†์Šต๋‹ˆ๋‹ค. ์‚ฌ์ด๋“œ๋ฐ”์— ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ Secrets๋กœ ๋“ฑ๋กํ•˜์„ธ์š”.")
27
  st.stop()
28
 
29
- client = InferenceClient(model=MODEL_ID, token=TOKEN)
30
-
31
- def generate_response(input_text: str):
32
- try:
33
- out = client.text_generation(
34
- input_text,
35
- max_new_tokens=200,
36
- temperature=0.7,
37
- top_p=0.9,
38
- )
39
- st.info(out)
40
- except HfHubHTTPError as e:
41
- st.error(f"Hugging Face API ์˜ค๋ฅ˜: {e}")
42
- st.code(traceback.format_exc())
43
- except Exception:
44
- st.error("์˜ˆ์™ธ ๋ฐœ์ƒ")
45
- st.code(traceback.format_exc())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  with st.form("Question"):
48
- text = st.text_area("์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”:")
49
  submitted = st.form_submit_button("์ „์†ก")
50
- if submitted and text.strip():
51
- generate_response(text.strip())
 
 
 
 
 
 
 
 
 
1
+ # app.py โ€” Streamlit + InferenceClient (robust, no torch/transformers)
2
+
3
  import os
4
  import traceback
5
  import streamlit as st
6
  from huggingface_hub import InferenceClient
7
  from huggingface_hub.errors import HfHubHTTPError
8
 
9
+ # (์„ ํƒ) .env ์ง€์›: ํ•„์š”์‹œ ์‚ฌ์šฉ
10
+ try:
11
+ from dotenv import load_dotenv
12
+ load_dotenv()
13
+ except Exception:
14
+ pass
15
+
16
  st.set_page_config(page_title="๊ฐ„๋‹จํ•œ ์ฑ—๋ด‡")
17
+ st.title("๋ฌด์—‡์ด๋“  ๋ฌผ์–ด๋ณด์„ธ์š”")
18
 
19
+ # โ”€โ”€ 1) ํ† ํฐ/๋ชจ๋ธ ์ฝ๊ธฐ
20
+ token = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN")
21
+ user_default_model = os.environ.get("HF_MODEL", "microsoft/Phi-3-mini-4k-instruct")
 
22
 
23
+ # text-generation ๊ฐ€๋Šฅํ•œ ํ›„๋ณด(๋ผ์šฐํ„ฐ์—์„œ ์ž์ฃผ ๋™์ž‘ํ•˜๋Š” ๋ชจ๋ธ)
24
+ FALLBACK_MODELS = [
25
+ user_default_model,
26
+ "microsoft/Phi-3-mini-4k-instruct",
27
+ "google/gemma-2-2b-it",
28
+ "HuggingFaceH4/zephyr-7b-beta",
29
+ ]
30
 
31
  with st.sidebar:
32
+ st.subheader("Settings")
33
+ model_id = st.text_input("Model ID", value=user_default_model)
34
+ token = st.text_input("Hugging Face Token (hf_โ€ฆ)", type="password", value=token or "")
35
+ st.caption("Spaces Secrets์— ๋„ฃ์œผ๋ฉด ์ž๋™์œผ๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์ฃผ์ž…๋ฉ๋‹ˆ๋‹ค.")
36
+
37
+ if not token:
38
+ st.error("Hugging Face ํ† ํฐ์ด ์—†์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ๋ณ€์ˆ˜(HUGGINGFACEHUB_API_TOKEN) ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ์‚ฌ์ด๋“œ๋ฐ”์— ์ž…๋ ฅํ•˜์„ธ์š”.")
 
39
  st.stop()
40
 
41
+ # โ”€โ”€ 2) ํด๋ผ์ด์–ธํŠธ: ๋ชจ๋ธ ๋ฏธ์ง€์ •์œผ๋กœ ์ƒ์„ฑ(ํ˜ธ์ถœ ์‹œ model=... ์ „๋‹ฌ)
42
+ client = InferenceClient(token=token)
43
+
44
+ def is_seq2seq_like(mid: str) -> bool:
45
+ mid = (mid or "").lower()
46
+ return any(k in mid for k in ("t5", "flan", "mt5", "bart", "ul2"))
47
+
48
+ def generate_response(text: str) -> str:
49
+ # ์šฐ์„  ์‚ฌ์šฉ์ž ์ง€์ • ๋ชจ๋ธ ์‹œ๋„ โ†’ ์‹คํŒจ ์‹œ ํด๋ฐฑ
50
+ tried = []
51
+ candidates = []
52
+ if model_id:
53
+ candidates.append(model_id)
54
+ for m in FALLBACK_MODELS:
55
+ if m and m not in candidates:
56
+ candidates.append(m)
57
+
58
+ # ๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ seq2seq(T5/FLAN ๋“ฑ)๋ฅผ ๋„ฃ์—ˆ๋‹ค๋ฉด ๊ฒฝ๊ณ  ํ›„ ํด๋ฐฑ ๋ชจ๋ธ๋ถ€ํ„ฐ ์‹œ๋„
59
+ if is_seq2seq_like(model_id):
60
+ st.warning("์„ ํƒํ•œ ๋ชจ๋ธ์ด seq2seq ๊ณ„์—ด๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. text-generation์ด ์•„๋‹Œ text2text๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์–ด "
61
+ "๋Œ€์‹  ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•œ causal LM์œผ๋กœ ํด๋ฐฑํ•ฉ๋‹ˆ๋‹ค.")
62
+ # ํด๋ฐฑ ๋ชจ๋ธ๋“ค๋งŒ ์‚ฌ์šฉ
63
+ candidates = [m for m in candidates if not is_seq2seq_like(m)]
64
+
65
+ last_err = None
66
+ for mid in candidates:
67
+ try:
68
+ out = client.text_generation(
69
+ text,
70
+ model=mid, # โ† ํ˜ธ์ถœ ์‹œ ๋ชจ๋ธ ์ง€์ •
71
+ max_new_tokens=200,
72
+ temperature=0.7,
73
+ top_p=0.9,
74
+ )
75
+ if mid != model_id:
76
+ st.info(f"(์ž๋™ ํด๋ฐฑ: {mid})")
77
+ return out
78
+ except Exception as e:
79
+ tried.append(f"{mid}: {e}")
80
+ last_err = e
81
+ continue
82
+
83
+ raise RuntimeError("๋ชจ๋“  ์›๊ฒฉ ์‹œ๋„๊ฐ€ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค:\n" + "\n".join(tried)) from last_err
84
 
85
  with st.form("Question"):
86
+ user_input = st.text_area("์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”:")
87
  submitted = st.form_submit_button("์ „์†ก")
88
+ if submitted and user_input.strip():
89
+ try:
90
+ out = generate_response(user_input.strip())
91
+ st.success(out)
92
+ except HfHubHTTPError as e:
93
+ st.error(f"Hugging Face API ์˜ค๋ฅ˜: {e}")
94
+ st.code(traceback.format_exc())
95
+ except Exception:
96
+ st.error("์š”์ฒญ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.")
97
+ st.code(traceback.format_exc())