Spaces:
Sleeping
Sleeping
Update utils/llm.py
Browse files- utils/llm.py +24 -19
utils/llm.py
CHANGED
|
@@ -1,45 +1,50 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# 1. ๋ฌด๋ฃ ๋ชจ๋ธ ์ค์ (Qwen2.5-72B๊ฐ ํ๊ตญ์ด ์ฑ๋ฅ ๋ฏธ์ณค๊ณ ๊ณต์ง์)
|
| 5 |
REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Secrets์์ HF ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ (์์ผ๋ฉด ๊ณต์ฉ API๋ก ์๋ํ์ง๋ง, ํ ํฐ ์๋ ๊ฒ ์์ ์ )
|
| 13 |
hf_token = st.secrets.get("HF_TOKEN", None)
|
| 14 |
-
|
| 15 |
-
# ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 16 |
client = InferenceClient(model=REPO_ID, token=hf_token)
|
| 17 |
|
| 18 |
-
# 2. ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 19 |
system_prompt = persona['system_prompt']
|
| 20 |
|
| 21 |
-
#
|
| 22 |
context_str = ""
|
| 23 |
if context_data:
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# AI์๊ฒ ๋ณด๋ผ ๋ฉ์์ง (์ฑํ
ํฌ๋งท)
|
| 28 |
messages = [
|
| 29 |
{"role": "system", "content": system_prompt},
|
| 30 |
-
{"role": "user", "content": f"{context_str}\
|
| 31 |
]
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
# 3. ๋ฌด๋ฃ ์ถ๋ก ์์ฒญ (์คํธ๋ฆฌ๋ฐ ์์ด ํ๋ฐฉ์ ๋ฐ๊ธฐ)
|
| 35 |
response = client.chat_completion(
|
| 36 |
messages=messages,
|
| 37 |
-
max_tokens=
|
| 38 |
-
temperature=0.
|
| 39 |
top_p=0.9,
|
| 40 |
)
|
| 41 |
return response.choices[0].message.content
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
-
|
| 45 |
-
return f"๐ (์ ๋ น๋์ด ๋ฌด๋ฃ ์๋ฒ ๊ณผ๋ถํ๋ก ๊ธฐ์ ํ์
จ์ต๋๋ค... ๋ค์ ์๋ํด์ฃผ์ธ์.)\n์๋ฌ: {str(e)}"
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
|
| 5 |
|
| 6 |
+
# ์ธ์ด ์ฝ๋ ๋งคํ
|
| 7 |
+
LANG_CODE = {
|
| 8 |
+
"ํ๊ตญ์ด": "Korean",
|
| 9 |
+
"English": "English",
|
| 10 |
+
"ไธญๆ": "Traditional Chinese",
|
| 11 |
+
"ๆฅๆฌ่ช": "Japanese"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def get_ai_response(user_query, persona, context_data=None, user_lang="ํ๊ตญ์ด"): # user_lang ์ถ๊ฐ
|
| 15 |
|
|
|
|
| 16 |
hf_token = st.secrets.get("HF_TOKEN", None)
|
|
|
|
|
|
|
| 17 |
client = InferenceClient(model=REPO_ID, token=hf_token)
|
| 18 |
|
|
|
|
| 19 |
system_prompt = persona['system_prompt']
|
| 20 |
|
| 21 |
+
# ๋ฌธ๋งฅ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
| 22 |
context_str = ""
|
| 23 |
if context_data:
|
| 24 |
+
# ๋ฐ์ดํฐ๊ฐ ๋ฆฌ์คํธ(ํ๋ก)์ธ์ง ๋์
๋๋ฆฌ(ํ์/์ฌ์ฃผ)์ธ์ง ํ์ธ
|
| 25 |
+
if isinstance(context_data, list):
|
| 26 |
+
info = [f"{card['name']} ({card['desc']})" for card in context_data]
|
| 27 |
+
context_str = f"\n[Card Data]: {', '.join(info)}\n"
|
| 28 |
+
else: # dict
|
| 29 |
+
context_str = f"\n[Analysis Data]: {str(context_data)}\n"
|
| 30 |
+
|
| 31 |
+
# โญ ํต์ฌ: ๋ต๋ณ ์ธ์ด ๊ฐ์ ์ค์
|
| 32 |
+
target_language = LANG_CODE.get(user_lang, "Korean")
|
| 33 |
+
instruction = f"\n(IMPORTANT: You MUST answer in {target_language}. Maintain the persona's tone in that language.)"
|
| 34 |
|
|
|
|
| 35 |
messages = [
|
| 36 |
{"role": "system", "content": system_prompt},
|
| 37 |
+
{"role": "user", "content": f"{context_str}\nUser Query: {user_query}\n{instruction}"}
|
| 38 |
]
|
| 39 |
|
| 40 |
try:
|
|
|
|
| 41 |
response = client.chat_completion(
|
| 42 |
messages=messages,
|
| 43 |
+
max_tokens=1000,
|
| 44 |
+
temperature=0.7,
|
| 45 |
top_p=0.9,
|
| 46 |
)
|
| 47 |
return response.choices[0].message.content
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
+
return f"Error: {str(e)}"
|
|
|