File size: 3,444 Bytes
7d6f12c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import gradio as gr
from huggingface_hub import InferenceClient
import os
import requests
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
#hf_client = InferenceClient("CohereForAI/aya-23-35B", token=os.getenv("HF_TOKEN"))
def load_fashion_code():
try:
with open('fashion.cod', 'r', encoding='utf-8') as file:
return file.read()
except FileNotFoundError:
return "fashion.cod ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."
except Exception as e:
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
fashion_code = load_fashion_code()
def respond(
message,
history: list[tuple[str, str]],
system_message="", # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
max_tokens=1024, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
temperature=0.7, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
top_p=0.9, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
):
global fashion_code
system_prefix = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ. ๋๋ ์ฃผ์ด์ง ์์ค์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก "์๋น์ค ์ฌ์ฉ ์ค๋ช
๋ฐ ์๋ด, qna๋ฅผ ํ๋ ์ญํ ์ด๋ค". ์์ฃผ ์น์ ํ๊ณ ์์ธํ๊ฒ 4000ํ ํฐ ์ด์ ์์ฑํ๋ผ. ๋๋ ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉ ์ค๋ช
๋ฐ ์ง์ ์๋ต์ ์งํํ๋ฉฐ, ์ด์ฉ์์๊ฒ ๋์์ ์ฃผ์ด์ผ ํ๋ค. ์ด์ฉ์๊ฐ ๊ถ๊ธํด ํ ๋ง ํ ๋ด์ฉ์ ์น์ ํ๊ฒ ์๋ ค์ฃผ๋๋ก ํ๋ผ. ์ฝ๋ ์ ์ฒด ๋ด์ฉ์ ๋ํด์๋ ๋ณด์์ ์ ์งํ๊ณ , ํค ๊ฐ ๋ฐ ์๋ํฌ์ธํธ์ ๊ตฌ์ฒด์ ์ธ ๋ชจ๋ธ์ ๊ณต๊ฐํ์ง ๋ง๋ผ. """
if message.lower() == "ํจ์
์ฝ๋ ์คํ":
system_message = system_message or "" # None์ธ ๊ฒฝ์ฐ ๋น ๋ฌธ์์ด๋ก ์ด๊ธฐํ
system_message += f"\n\nํจ์
์ฝ๋ ๋ด์ฉ:\n{fashion_code}"
message = "ํจ์
์ฝ๋์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์."
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
# prefix ์ถ๊ฐ
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 hf_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("") # ํ ํฐ ์ ๊ฑฐ
yield response
# Gradio ์ธํฐํ์ด์ค ์ค์ ๋ถ๋ถ๋ ์์
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(label="System Message", value=""),
gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens"),
gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature"),
gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"),
],
examples=[
["ํจ์
์ฝ๋ ์คํ"],
["์ฌ์ฉ ๋ฐฉ๋ฒ์ ์์ธํ ์ค๋ช
ํ๋ผ"],
["์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ ํ๋ธ ์์ ์คํฌ๋ฆฝํธ ํํ๋ก ์์ฑํ๋ผ"],
["์ฌ์ฉ ๋ฐฉ๋ฒ์ SEO ์ต์ ํํ์ฌ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ"],
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"],
],
cache_examples=False, # ์บ์ฑ ๋นํ์ฑํ ์ค์
# css="""footer {visibility: hidden}""", # ์ด๊ณณ์ CSS๋ฅผ ์ถ๊ฐ
)
if __name__ == "__main__":
demo.launch(auth=("gini","pick")) |