Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
# app.py β Streamlit + InferenceClient (
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import traceback
|
| 5 |
import streamlit as st
|
|
@@ -14,40 +13,95 @@ except Exception:
|
|
| 14 |
pass
|
| 15 |
|
| 16 |
st.set_page_config(page_title="κ°λ¨ν μ±λ΄")
|
| 17 |
-
st.title("
|
| 18 |
|
| 19 |
-
# ββ
|
| 20 |
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if not token:
|
| 22 |
-
st.error("Hugging Face ν ν°μ΄ μμ΅λλ€. νκ²½λ³μ(HUGGINGFACEHUB_API_TOKEN)
|
| 23 |
st.stop()
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
max_new_tokens=200,
|
| 36 |
temperature=0.7,
|
| 37 |
top_p=0.9,
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
raise RuntimeError(f"Hugging Face API μ€λ₯: {e}") from e
|
| 41 |
except Exception as e:
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with st.form("Question"):
|
| 45 |
user_input = st.text_area("μ§λ¬Έμ μ
λ ₯νμΈμ:")
|
| 46 |
submitted = st.form_submit_button("μ μ‘")
|
| 47 |
if submitted and user_input.strip():
|
| 48 |
try:
|
| 49 |
-
out =
|
| 50 |
st.success(out)
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception:
|
| 52 |
st.error("μμ² μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€.")
|
| 53 |
st.code(traceback.format_exc())
|
|
|
|
| 1 |
+
# app.py β Streamlit + Hugging Face InferenceClient (chat μ°μ , text_generation ν΄λ°±)
|
|
|
|
| 2 |
import os
|
| 3 |
import traceback
|
| 4 |
import streamlit as st
|
|
|
|
| 13 |
pass
|
| 14 |
|
| 15 |
st.set_page_config(page_title="κ°λ¨ν μ±λ΄")
|
| 16 |
+
st.title("무μμ΄λ λ¬Όμ΄λ³΄μΈμ")
|
| 17 |
|
| 18 |
+
# ββ Token/Model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN")
|
| 20 |
+
# λλ€μ νλ‘λ°μ΄λμμ chat(conversational)λ‘ μ λμνλ λͺ¨λΈλ€
|
| 21 |
+
DEFAULT_MODELS = [
|
| 22 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 23 |
+
"google/gemma-2-2b-it",
|
| 24 |
+
"microsoft/Phi-3-mini-4k-instruct",
|
| 25 |
+
]
|
| 26 |
+
default_model = os.environ.get("HF_MODEL", DEFAULT_MODELS[0])
|
| 27 |
+
|
| 28 |
+
with st.sidebar:
|
| 29 |
+
st.subheader("Settings")
|
| 30 |
+
model_id = st.selectbox("Model", DEFAULT_MODELS, index=DEFAULT_MODELS.index(default_model) if default_model in DEFAULT_MODELS else 0)
|
| 31 |
+
tok_in = st.text_input("Hugging Face Token (hf_β¦)", type="password", value=token or "", help="Spaces Secretsμ λ£μΌλ©΄ μλμΌλ‘ νκ²½λ³μλ‘ μ£Όμ
λ©λλ€.")
|
| 32 |
+
if tok_in.strip():
|
| 33 |
+
token = tok_in.strip()
|
| 34 |
+
|
| 35 |
if not token:
|
| 36 |
+
st.error("Hugging Face ν ν°μ΄ μμ΅λλ€. νκ²½λ³μ(HUGGINGFACEHUB_API_TOKEN) μ§μ νκ±°λ μ¬μ΄λλ°μ μ
λ ₯νμΈμ.")
|
| 37 |
st.stop()
|
| 38 |
|
| 39 |
+
# λͺ¨λΈ λ―Έμ§μ ν΄λΌμ΄μΈνΈ (νΈμΆ μ model=... μ λ¬)
|
| 40 |
+
client = InferenceClient(token=token)
|
| 41 |
+
|
| 42 |
+
def _extract_chat_content(result) -> str:
|
| 43 |
+
"""
|
| 44 |
+
HF chat-completionsμ λ€μν λ¦¬ν΄ νμμ μμ νκ² νμ±
|
| 45 |
+
"""
|
| 46 |
+
# dict μ€νμΌ
|
| 47 |
+
if isinstance(result, dict):
|
| 48 |
+
try:
|
| 49 |
+
return result["choices"][0]["message"]["content"]
|
| 50 |
+
except Exception:
|
| 51 |
+
pass
|
| 52 |
+
# κ°μ²΄ μ€νμΌ(μ νκΈ°)
|
| 53 |
+
try:
|
| 54 |
+
# pydantic-like
|
| 55 |
+
return result.choices[0].message["content"]
|
| 56 |
+
except Exception:
|
| 57 |
+
try:
|
| 58 |
+
return result.choices[0].message.content
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
| 61 |
+
# λ§μ§λ§ ν΄λ°±
|
| 62 |
+
return str(result)
|
| 63 |
|
| 64 |
+
def chat_then_generate(prompt: str) -> str:
|
| 65 |
+
errors = []
|
| 66 |
|
| 67 |
+
# 1) chat-completions (conversational) μλ
|
| 68 |
try:
|
| 69 |
+
chat_res = client.chat.completions.create( # huggingface_hub >= 0.24 λ°©μ
|
| 70 |
+
model=model_id,
|
| 71 |
+
messages=[{"role": "user", "content": prompt}],
|
| 72 |
+
max_tokens=200,
|
| 73 |
+
temperature=0.7,
|
| 74 |
+
top_p=0.9,
|
| 75 |
+
)
|
| 76 |
+
return _extract_chat_content(chat_res)
|
| 77 |
+
except Exception as e:
|
| 78 |
+
errors.append(f"chat({model_id}): {e}")
|
| 79 |
+
|
| 80 |
+
# 2) μκ° μ€ν¨νλ©΄ text_generation ν΄λ°± (μΌλΆ νλ‘λ°μ΄λ/λͺ¨λΈμ λ λ€ λ¨)
|
| 81 |
+
try:
|
| 82 |
+
gen_res = client.text_generation(
|
| 83 |
+
prompt,
|
| 84 |
+
model=model_id,
|
| 85 |
max_new_tokens=200,
|
| 86 |
temperature=0.7,
|
| 87 |
top_p=0.9,
|
| 88 |
)
|
| 89 |
+
return gen_res
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
+
errors.append(f"text_generation({model_id}): {e}")
|
| 92 |
+
|
| 93 |
+
raise RuntimeError("λͺ¨λ μλκ° μ€ν¨νμ΅λλ€:\n" + "\n".join(errors))
|
| 94 |
|
| 95 |
with st.form("Question"):
|
| 96 |
user_input = st.text_area("μ§λ¬Έμ μ
λ ₯νμΈμ:")
|
| 97 |
submitted = st.form_submit_button("μ μ‘")
|
| 98 |
if submitted and user_input.strip():
|
| 99 |
try:
|
| 100 |
+
out = chat_then_generate(user_input.strip())
|
| 101 |
st.success(out)
|
| 102 |
+
except HfHubHTTPError as e:
|
| 103 |
+
st.error(f"Hugging Face API μ€λ₯: {e}")
|
| 104 |
+
st.code(traceback.format_exc())
|
| 105 |
except Exception:
|
| 106 |
st.error("μμ² μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€.")
|
| 107 |
st.code(traceback.format_exc())
|