ginipick commited on
Commit
7d6f12c
ยท
verified ยท
1 Parent(s): fe59084

Create app-backup.py

Browse files
Files changed (1) hide show
  1. 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"))