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