Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,70 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from huggingface_hub import InferenceClient
|
|
|
|
| 4 |
|
| 5 |
st.set_page_config(page_title="๊ฐ๋จํ ์ฑ๋ด")
|
| 6 |
-
st.title("
|
| 7 |
|
| 8 |
-
#
|
| 9 |
HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN")
|
| 10 |
if not HF_TOKEN:
|
| 11 |
-
st.error("Hugging Face ํ ํฐ์ด ์์ต๋๋ค.
|
| 12 |
st.stop()
|
| 13 |
|
| 14 |
-
# ์ฑํ
์ด ์
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# ํด๋ผ์ด์ธํธ
|
| 18 |
client = InferenceClient(token=HF_TOKEN)
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
res = client.chat.completions.create(
|
| 23 |
-
model=
|
| 24 |
-
messages=[{"role": "user", "content":
|
| 25 |
-
temperature=0.7,
|
| 26 |
max_tokens=200,
|
|
|
|
| 27 |
top_p=0.9,
|
| 28 |
)
|
| 29 |
-
# ๊ฒฐ๊ณผ ๊บผ๋ด๊ธฐ
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
except Exception:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
with st.form("Question"):
|
| 37 |
text = st.text_area("์ง๋ฌธ์ ์
๋ ฅํ์ธ์:")
|
| 38 |
submitted = st.form_submit_button("์ ์ก")
|
| 39 |
if submitted and text.strip():
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py โ Streamlit + Hugging Face InferenceClient (chat API, ์์ ๋ชจ๋ธ ํด๋ฐฑ)
|
| 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 |
st.set_page_config(page_title="๊ฐ๋จํ ์ฑ๋ด")
|
| 10 |
+
st.title("๋ญ๋ ์ง ๋ฌผ์ด๋ณด์ธ์~")
|
| 11 |
|
| 12 |
+
# 1) ํ ํฐ: ํ๊ฒฝ๋ณ์๋ง ์ฌ์ฉ (Spaces Secrets โ ์๋ ์ฃผ์
)
|
| 13 |
HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN")
|
| 14 |
if not HF_TOKEN:
|
| 15 |
+
st.error("Hugging Face ํ ํฐ์ด ์์ต๋๋ค. HUGGINGFACEHUB_API_TOKEN=hf_xxx ๋ฅผ ์ค์ ํ์ธ์.")
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
+
# 2) ์ฑํ
์ด ์ ๋ถ๋ ๊ฒฝ๋ ๋ชจ๋ธ ํ๋ณด (ํ๋ก๋ฐ์ด๋ ํธํ์ฑโ ์์ผ๋ก ๋์ด)
|
| 19 |
+
# ํ์์ HF_MODEL ํ๊ฒฝ๋ณ์๋ก ์ฒซ ํ๋ณด๋ฅผ ๋ฐ๊ฟ ์ ์์ต๋๋ค.
|
| 20 |
+
DEFAULT_CANDIDATES = [
|
| 21 |
+
os.environ.get("HF_MODEL", "Qwen/Qwen2.5-0.5B-Instruct"),
|
| 22 |
+
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 23 |
+
"Qwen/Qwen2.5-1.5B-Instruct",
|
| 24 |
+
"google/gemma-2-2b-it",
|
| 25 |
+
]
|
| 26 |
|
| 27 |
+
# 3) ํด๋ผ์ด์ธํธ (๋ชจ๋ธ ๋ฏธ์ง์ ) โ ํธ์ถ ์ model=... ์ ๋ฌ
|
| 28 |
client = InferenceClient(token=HF_TOKEN)
|
| 29 |
|
| 30 |
+
def chat_once(model_id: str, prompt: str) -> str:
|
| 31 |
+
"""chat-completions ์ฐ์ . ์คํจ ์ ์์ธ."""
|
| 32 |
res = client.chat.completions.create(
|
| 33 |
+
model=model_id,
|
| 34 |
+
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 35 |
max_tokens=200,
|
| 36 |
+
temperature=0.7,
|
| 37 |
top_p=0.9,
|
| 38 |
)
|
|
|
|
| 39 |
try:
|
| 40 |
+
return res.choices[0].message.content
|
| 41 |
except Exception:
|
| 42 |
+
return str(res)
|
| 43 |
+
|
| 44 |
+
def generate_response(prompt: str) -> str:
|
| 45 |
+
errors = []
|
| 46 |
+
for mid in DEFAULT_CANDIDATES:
|
| 47 |
+
try:
|
| 48 |
+
out = chat_once(mid, prompt)
|
| 49 |
+
if mid != DEFAULT_CANDIDATES[0]:
|
| 50 |
+
st.info(f"(์๋ ํด๋ฐฑ ๋ชจ๋ธ ์ฌ์ฉ: {mid})")
|
| 51 |
+
return out
|
| 52 |
+
except Exception as e:
|
| 53 |
+
errors.append(f"{mid}: {e}")
|
| 54 |
+
continue
|
| 55 |
+
# ๋ชจ๋ ํ๋ณด ์คํจ ์ ์์ธ ๋ก๊ทธ ๋
ธ์ถ
|
| 56 |
+
raise RuntimeError("๋ชจ๋ ๋ชจ๋ธ ์๋๊ฐ ์คํจํ์ต๋๋ค:\n" + "\n".join(errors))
|
| 57 |
|
| 58 |
with st.form("Question"):
|
| 59 |
text = st.text_area("์ง๋ฌธ์ ์
๋ ฅํ์ธ์:")
|
| 60 |
submitted = st.form_submit_button("์ ์ก")
|
| 61 |
if submitted and text.strip():
|
| 62 |
+
try:
|
| 63 |
+
answer = generate_response(text.strip())
|
| 64 |
+
st.success(answer)
|
| 65 |
+
except HfHubHTTPError as e:
|
| 66 |
+
st.error(f"Hugging Face API ์ค๋ฅ: {e}")
|
| 67 |
+
st.code(traceback.format_exc())
|
| 68 |
+
except Exception:
|
| 69 |
+
st.error("์์ฒญ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.")
|
| 70 |
+
st.code(traceback.format_exc())
|