Spaces:
Runtime error
Runtime error
Download app.py from eclipse2/flan-t5-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/spaces/eclipse2/flan-t5-chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/eclipse2/flan-t5-chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/eclipse2/flan-t5-chatbot/resolve/main/app.py
2.55 kB
| # app.py โ Streamlit + Hugging Face InferenceClient (chat API, ์์ ๋ชจ๋ธ ํด๋ฐฑ) | |
| import os | |
| import traceback | |
| import streamlit as st | |
| from huggingface_hub import InferenceClient | |
| from huggingface_hub.errors import HfHubHTTPError | |
| st.set_page_config(page_title="๊ฐ๋จํ ์ฑ๋ด") | |
| st.title("๋ญ๋ ์ง ๋ฌผ์ด๋ณด์ธ์~") | |
| # 1) ํ ํฐ: ํ๊ฒฝ๋ณ์๋ง ์ฌ์ฉ (Spaces Secrets โ ์๋ ์ฃผ์ ) | |
| HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("HF_TOKEN") | |
| if not HF_TOKEN: | |
| st.error("Hugging Face ํ ํฐ์ด ์์ต๋๋ค. HUGGINGFACEHUB_API_TOKEN=hf_xxx ๋ฅผ ์ค์ ํ์ธ์.") | |
| st.stop() | |
| # 2) ์ฑํ ์ด ์ ๋ถ๋ ๊ฒฝ๋ ๋ชจ๋ธ ํ๋ณด (ํ๋ก๋ฐ์ด๋ ํธํ์ฑโ ์์ผ๋ก ๋์ด) | |
| # ํ์์ HF_MODEL ํ๊ฒฝ๋ณ์๋ก ์ฒซ ํ๋ณด๋ฅผ ๋ฐ๊ฟ ์ ์์ต๋๋ค. | |
| DEFAULT_CANDIDATES = [ | |
| os.environ.get("HF_MODEL", "Qwen/Qwen2.5-0.5B-Instruct"), | |
| "TinyLlama/TinyLlama-1.1B-Chat-v1.0", | |
| "Qwen/Qwen2.5-1.5B-Instruct", | |
| "google/gemma-2-2b-it", | |
| ] | |
| # 3) ํด๋ผ์ด์ธํธ (๋ชจ๋ธ ๋ฏธ์ง์ ) โ ํธ์ถ ์ model=... ์ ๋ฌ | |
| client = InferenceClient(token=HF_TOKEN) | |
| def chat_once(model_id: str, prompt: str) -> str: | |
| """chat-completions ์ฐ์ . ์คํจ ์ ์์ธ.""" | |
| res = client.chat.completions.create( | |
| model=model_id, | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=200, | |
| temperature=0.7, | |
| top_p=0.9, | |
| ) | |
| try: | |
| return res.choices[0].message.content | |
| except Exception: | |
| return str(res) | |
| def generate_response(prompt: str) -> str: | |
| errors = [] | |
| for mid in DEFAULT_CANDIDATES: | |
| try: | |
| out = chat_once(mid, prompt) | |
| if mid != DEFAULT_CANDIDATES[0]: | |
| st.info(f"(์๋ ํด๋ฐฑ ๋ชจ๋ธ ์ฌ์ฉ: {mid})") | |
| return out | |
| except Exception as e: | |
| errors.append(f"{mid}: {e}") | |
| continue | |
| # ๋ชจ๋ ํ๋ณด ์คํจ ์ ์์ธ ๋ก๊ทธ ๋ ธ์ถ | |
| raise RuntimeError("๋ชจ๋ ๋ชจ๋ธ ์๋๊ฐ ์คํจํ์ต๋๋ค:\n" + "\n".join(errors)) | |
| with st.form("Question"): | |
| text = st.text_area("์ง๋ฌธ์ ์ ๋ ฅํ์ธ์:") | |
| submitted = st.form_submit_button("์ ์ก") | |
| if submitted and text.strip(): | |
| try: | |
| answer = generate_response(text.strip()) | |
| st.success(answer) | |
| except HfHubHTTPError as e: | |
| st.error(f"Hugging Face API ์ค๋ฅ: {e}") | |
| st.code(traceback.format_exc()) | |
| except Exception: | |
| st.error("์์ฒญ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.") | |
| st.code(traceback.format_exc()) | |