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

Update app.py

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