eclipse2 commited on
Commit
2f49c14
ยท
verified ยท
1 Parent(s): a2e1bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -94
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
- 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())
 
 
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())