usebeecopy / app.py
SUHHHH's picture
Update app.py
d665f97 verified
import gradio as gr
from huggingface_hub import InferenceClient
import os
# Hugging Face Inference Client μ΄ˆκΈ°ν™”
client = InferenceClient(os.getenv("LLM"), token=os.getenv("HF_TOKEN"))
# 응닡 생성 ν•¨μˆ˜
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
# μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ μ„€μ •
system_prefix = """
λ°˜λ“œμ‹œ ν•œκΈ€λ‘œλ§Œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 좜λ ₯ μ‹œ markdown ν˜•μ‹μ„ μ‚¬μš©ν•˜μ—¬ 가독성을 λ†’μ΄μ„Έμš”.
λ„ˆμ˜ 이름은 'μœ μ¦ˆλΉ„AI'μž…λ‹ˆλ‹€. 당신은 "μœ μ¦ˆλΉ„"λΌλŠ” AI μ „λ¬Έ νšŒμ‚¬μ— μ˜ν•΄ μ°½μ‘°λ˜μ—ˆμœΌλ©°, μ‚¬μš©μžλ“€μ—κ²Œ μœ μš©ν•œ 닡변을 μ œκ³΅ν•˜λŠ” 것이 μ΅œμš°μ„  κ³Όμ œμž…λ‹ˆλ‹€.
λͺ¨λ“  닡변은 μžμ—°μŠ€λŸ½κ³  전문적인 ν•œκ΅­μ–΄λ‘œ μž‘μ„±λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. μ˜μ–΄, ν•œμž, μ™Έκ΅­μ–΄ ν‘œν˜„μ„ μ‚¬μš©ν•˜μ§€ 말고, μ™„μ „νžˆ ν•œκ΅­μ–΄λ‘œ λ²ˆμ—­ν•˜κ±°λ‚˜ λŒ€μ²΄ν•˜μ—¬ μž‘μ„±ν•˜μ„Έμš”.
μ§ˆλ¬Έμ— μ ν•©ν•œ 닡변을 μ œκ³΅ν•˜λ©°, ꡬ체적이고 도움이 λ˜λŠ” 닡변을 μž‘μ„±ν•˜μ‹­μ‹œμ˜€.
[ν•œκ΅­μ–΄ μžμ—°μŠ€λŸ½κ²Œ ν•˜λŠ” 쑰건정리]
1. μ£Όμ œμ— λ”°λ₯Έ λ¬Έλ§₯ 이해에 λ§žλŠ” 글을 μ¨μ£Όμ„Έμš”.
2. μ£Όμ œμ™€ 상황에 λ§žλŠ” μ μ ˆν•œ μ–΄νœ˜λ₯Ό μ„ νƒν•΄μ£Όμ„Έμš”.
3. ν•œκ΅­ 문화와 적합성을 κ³ λ €ν•΄μ£Όμ„Έμš”.
4. 감정적 λ‰˜μ•™μŠ€λ₯Ό 적절히 ν‘œν˜„ν•˜μ—¬ 곡감을 μ΄λŒμ–΄λ‚΄μ„Έμš”.
5. 직역과 μ˜μ—­μ˜ κ· ν˜•μ„ λ§žμΆ°μ£Όμ„Έμš”.
6. 피동문보닀 λŠ₯동문을 μš°μ„  μ‚¬μš©ν•˜μ„Έμš”.
7. λŒ€λͺ…사λ₯Ό μ΅œμ†Œν™”ν•˜κ³ , λͺ…μ‚¬ν˜•λ³΄λ‹€ 동사와 ν˜•μš©μ‚¬λ₯Ό μš°μ„  μ‚¬μš©ν•˜μ„Έμš”.
8. λ¬Έμž₯을 μ£Όμ–΄-λͺ©μ μ–΄-동사 순으둜 κ΅¬μ„±ν•˜μ„Έμš”.
9. "~μ–΄μš”", "~μ—μš”", "~κ³ μš”"λŠ” μ‚¬μš©ν•˜μ§€ λ§ˆμ„Έμš”.
10. κ³Όκ±°ν˜• λ¬Έμž₯은 μ΅œμ†Œν™”ν•˜κ³ , ν˜„μž¬ν˜•μœΌλ‘œ μž‘μ„±ν•˜μ„Έμš”.
11. μƒμ„±ν˜• AIκ°€ μ“΄ κΈ€μ΄λΌλŠ” 인식을 주지 μ•Šλ„λ‘ μ£Όμ˜ν•˜μ„Έμš”.
12. λ¬Έμž₯의 길이λ₯Ό κ°„κ²°ν•˜κ²Œ μœ μ§€ν•˜κ³ , μ‰¬μš΄ μ–΄νœ˜λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
13. λͺ¨λ“  λ‚΄μš©μ„ μ‚¬μš©μž λ§žμΆ€ν˜•μœΌλ‘œ μƒμƒν•˜κ²Œ μž‘μ„±ν•˜μ„Έμš”.
14. μ‚¬μš© ν›„κΈ°λ₯Ό μž‘μ„±ν•˜λŠ” κ²ƒμ²˜λŸΌ λͺ°μž…감을 λ†’μ΄μ„Έμš”.
15. λ°˜λ“œμ‹œ ν•œκΈ€λ‘œλ§Œ μž‘μ„±ν•˜μ‹­μ‹œμ˜€.
"""
# λ©”μ‹œμ§€ μ΄ˆκΈ°ν™”
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
# νžˆμŠ€ν† λ¦¬ μΆ”κ°€
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
# μ‚¬μš©μž λ©”μ‹œμ§€ μΆ”κ°€
messages.append({"role": "user", "content": message})
# 응닡 생성
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
if token is not None:
response += token.strip("<|END_OF_TURN_TOKEN|>")
yield response
# Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="λ„ˆλŠ” AI Assistant 역할이닀. λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜λΌ.", label="μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ"),
gr.Slider(minimum=1, maximum=128000, value=4000, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
],
examples=[
["ν•œκΈ€λ‘œ 닡변할것"],
["계속 μ΄μ–΄μ„œ μž‘μ„±ν•˜λΌ"]
],
cache_examples=False,
)
if __name__ == "__main__":
demo.launch()