Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,40 @@
|
|
| 1 |
-
# app.py โ Streamlit + Hugging Face InferenceClient (chat ์ฐ์ , text_generation ํด๋ฐฑ)
|
| 2 |
import os
|
| 3 |
-
import traceback
|
| 4 |
import streamlit as st
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
-
from huggingface_hub.errors import HfHubHTTPError
|
| 7 |
-
|
| 8 |
-
# (์ ํ) .env ์ง์
|
| 9 |
-
try:
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
-
load_dotenv()
|
| 12 |
-
except Exception:
|
| 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 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
st.stop()
|
| 38 |
|
| 39 |
-
# ๋ชจ๋ธ
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
return result.choices[0].message["content"]
|
| 56 |
except Exception:
|
| 57 |
-
|
| 58 |
-
|
| 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 |
-
|
| 97 |
submitted = st.form_submit_button("์ ์ก")
|
| 98 |
-
if submitted and
|
| 99 |
-
|
| 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())
|
|
|
|
|
|
|
| 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 |
+
# HF ํ ํฐ: Spaces์์๋ Secrets์ ๋ฃ์ผ๋ฉด ์๋์ผ๋ก ํ๊ฒฝ๋ณ์๋ก ์ฃผ์
๋ฉ๋๋ค.
|
| 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 ํ ํฐ์ด ์์ต๋๋ค. ํ๊ฒฝ๋ณ์ HUGGINGFACEHUB_API_TOKEN=hf_xxx ๋ฅผ ์ค์ ํ์ธ์.")
|
| 12 |
st.stop()
|
| 13 |
|
| 14 |
+
# ์ฑํ
์ด ์ ๋๋ ๋ชจ๋ธ ํ๋๋ง ๊ณ ์ (๋ณต์ก์ฑ ์ต์ํ)
|
| 15 |
+
MODEL_ID = "HuggingFaceH4/zephyr-7b-beta"
|
| 16 |
+
|
| 17 |
+
# ํด๋ผ์ด์ธํธ 1ํ ์์ฑ
|
| 18 |
+
client = InferenceClient(token=HF_TOKEN)
|
| 19 |
+
|
| 20 |
+
def generate_response(input_text: str) -> None:
|
| 21 |
+
# LangChain ์์ด ChatOpenAI.predict์ ๋์ผํ ํํ๋ก ๋ฉ์์ง ํ ํด ์ ์ก
|
| 22 |
+
res = client.chat.completions.create(
|
| 23 |
+
model=MODEL_ID,
|
| 24 |
+
messages=[{"role": "user", "content": input_text}],
|
| 25 |
+
temperature=0.7,
|
| 26 |
+
max_tokens=200,
|
| 27 |
+
top_p=0.9,
|
| 28 |
+
)
|
| 29 |
+
# ๊ฒฐ๊ณผ ๊บผ๋ด๊ธฐ
|
| 30 |
try:
|
| 31 |
+
content = res.choices[0].message.content
|
|
|
|
| 32 |
except Exception:
|
| 33 |
+
content = str(res)
|
| 34 |
+
st.info(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
with st.form("Question"):
|
| 37 |
+
text = st.text_area("์ง๋ฌธ์ ์
๋ ฅํ์ธ์:")
|
| 38 |
submitted = st.form_submit_button("์ ์ก")
|
| 39 |
+
if submitted and text.strip():
|
| 40 |
+
generate_response(text.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|