Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,17 @@ import requests
|
|
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 respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -15,24 +26,25 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] # prefix μΆκ°
|
25 |
|
|
|
26 |
for val in history:
|
27 |
if val[0]:
|
28 |
messages.append({"role": "user", "content": val[0]})
|
29 |
if val[1]:
|
30 |
messages.append({"role": "assistant", "content": val[1]})
|
31 |
-
|
32 |
messages.append({"role": "user", "content": message})
|
33 |
|
34 |
response = ""
|
35 |
-
|
36 |
for message in hf_client.chat_completion(
|
37 |
messages,
|
38 |
max_tokens=max_tokens,
|
@@ -43,19 +55,20 @@ def respond(
|
|
43 |
token = message.choices[0].delta.content
|
44 |
if token is not None:
|
45 |
response += token.strip("") # ν ν° μ κ±°
|
46 |
-
|
47 |
|
48 |
demo = gr.ChatInterface(
|
49 |
respond,
|
50 |
-
|
51 |
examples=[
|
|
|
52 |
["μ¬μ© λ°©λ²μ μμΈν μ€λͺ
νλΌ"],
|
53 |
["μ¬μ© λ°©λ²μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
|
54 |
-
["μ¬μ© λ°©λ²μ SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈλ‘ 4000 ν ν° μ΄μ μμ±νλΌ"],
|
55 |
-
["κ³μ μ΄μ΄μ λ΅λ³νλΌ"],
|
|
|
56 |
],
|
57 |
cache_examples=False, # μΊμ± λΉνμ±ν μ€μ
|
58 |
-
#
|
59 |
)
|
60 |
|
61 |
if __name__ == "__main__":
|
|
|
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]],
|
|
|
26 |
temperature,
|
27 |
top_p,
|
28 |
):
|
29 |
+
global fashion_code
|
30 |
+
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ£Όμ΄μ§ μμ€μ½λλ₯Ό κΈ°λ°μΌλ‘ "μλΉμ€ μ¬μ© μ€λͺ
λ° μλ΄, qnaλ₯Ό νλ μν μ΄λ€". μ½λλ₯Ό κΈ°λ°μΌλ‘ μ¬μ© μ€λͺ
λ° μ§μ μλ΅μ μ§ννλ©°, μ΄μ©μμκ² λμμ μ£Όμ΄μΌ νλ€. μ΄μ©μκ° κΆκΈν΄ ν λ§ ν λ΄μ©μ μΉμ νκ² μλ €μ£Όλλ‘ νλΌ. μ½λ μ 체 λ΄μ©μ λν΄μλ 보μμ μ μ§νκ³ , ν€ κ° λ° μλν¬μΈνΈμ ꡬ체μ μΈ λͺ¨λΈμ 곡κ°νμ§ λ§λΌ. """
|
31 |
|
32 |
+
if message.lower() == "ν¨μ
μ½λ μ€ν":
|
33 |
+
system_message += f"\n\nν¨μ
μ½λ λ΄μ©:\n{fashion_code}"
|
34 |
+
message = "ν¨μ
μ½λμ λν΄ μ€λͺ
ν΄μ£ΌμΈμ."
|
35 |
+
|
36 |
+
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
|
|
37 |
|
38 |
+
# prefix μΆκ°
|
39 |
for val in history:
|
40 |
if val[0]:
|
41 |
messages.append({"role": "user", "content": val[0]})
|
42 |
if val[1]:
|
43 |
messages.append({"role": "assistant", "content": val[1]})
|
44 |
+
|
45 |
messages.append({"role": "user", "content": message})
|
46 |
|
47 |
response = ""
|
|
|
48 |
for message in hf_client.chat_completion(
|
49 |
messages,
|
50 |
max_tokens=max_tokens,
|
|
|
55 |
token = message.choices[0].delta.content
|
56 |
if token is not None:
|
57 |
response += token.strip("") # ν ν° μ κ±°
|
58 |
+
yield response
|
59 |
|
60 |
demo = gr.ChatInterface(
|
61 |
respond,
|
|
|
62 |
examples=[
|
63 |
+
["ν¨μ
μ½λ μ€ν"],
|
64 |
["μ¬μ© λ°©λ²μ μμΈν μ€λͺ
νλΌ"],
|
65 |
["μ¬μ© λ°©λ²μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
|
66 |
+
["μ¬μ© λ°©λ²μ SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈλ‘ 4000 ν ν° μ΄μ μμ±νλΌ"],
|
67 |
+
["κ³μ μ΄μ΄μ λ΅λ³νλΌ"],
|
68 |
+
["ν¨μ
μ½λ μ€ν"],
|
69 |
],
|
70 |
cache_examples=False, # μΊμ± λΉνμ±ν μ€μ
|
71 |
+
# css="""footer {visibility: hidden}""", # μ΄κ³³μ CSSλ₯Ό μΆκ°
|
72 |
)
|
73 |
|
74 |
if __name__ == "__main__":
|