Update app-backup.py
Browse files- app-backup.py +64 -33
app-backup.py
CHANGED
@@ -1,62 +1,88 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
-
import
|
|
|
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
|
11 |
try:
|
12 |
-
with open(
|
13 |
return file.read()
|
14 |
except FileNotFoundError:
|
15 |
-
return "
|
16 |
except Exception as e:
|
17 |
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def respond(
|
22 |
message,
|
23 |
-
history:
|
24 |
system_message="", # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
25 |
max_tokens=1024, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
26 |
temperature=0.7, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
27 |
-
top_p=0.9,
|
28 |
):
|
29 |
-
global fashion_code
|
30 |
-
|
|
|
31 |
|
32 |
if message.lower() == "ํจ์
์ฝ๋ ์คํ":
|
33 |
-
system_message
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
60 |
|
61 |
# Gradio ์ธํฐํ์ด์ค ์ค์ ๋ถ๋ถ๋ ์์
|
62 |
demo = gr.ChatInterface(
|
@@ -68,15 +94,20 @@ demo = gr.ChatInterface(
|
|
68 |
gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"),
|
69 |
],
|
70 |
examples=[
|
71 |
-
["ํจ์
์ฝ๋ ์คํ"],
|
72 |
-
["
|
73 |
-
["
|
74 |
-
["
|
|
|
|
|
|
|
|
|
|
|
75 |
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"],
|
76 |
],
|
|
|
77 |
cache_examples=False, # ์บ์ฑ ๋นํ์ฑํ ์ค์
|
78 |
-
# css="""footer {visibility: hidden}""", # ์ด๊ณณ์ CSS๋ฅผ ์ถ๊ฐ
|
79 |
)
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
+
import pandas as pd
|
5 |
+
from typing import List, Tuple
|
6 |
|
7 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
8 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
9 |
+
# hf_client = InferenceClient("CohereForAI/aya-23-35B", token=os.getenv("HF_TOKEN"))
|
10 |
|
11 |
+
def load_code(filename):
|
12 |
try:
|
13 |
+
with open(filename, 'r', encoding='utf-8') as file:
|
14 |
return file.read()
|
15 |
except FileNotFoundError:
|
16 |
+
return f"{filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
17 |
except Exception as e:
|
18 |
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
19 |
|
20 |
+
def load_parquet(filename):
|
21 |
+
try:
|
22 |
+
df = pd.read_parquet(filename, engine='pyarrow')
|
23 |
+
# ๋ฐ์ดํฐํ๋ ์์ ์ฒซ ๋ช ํ์ ๋ฌธ์์ด๋ก ๋ณํ (Markdown ํ ํ์)
|
24 |
+
return df.head(10).to_markdown(index=False)
|
25 |
+
except FileNotFoundError:
|
26 |
+
return f"{filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
27 |
+
except Exception as e:
|
28 |
+
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
29 |
+
|
30 |
+
# ์ฝ๋ ํ์ผ ๋ก๋
|
31 |
+
fashion_code = load_code('fashion.cod')
|
32 |
+
uhdimage_code = load_code('uhdimage.cod')
|
33 |
+
MixGEN_code = load_code('mgen.cod')
|
34 |
+
|
35 |
+
# Parquet ํ์ผ ๋ก๋
|
36 |
+
test_parquet_content = load_parquet('test.parquet')
|
37 |
|
38 |
def respond(
|
39 |
message,
|
40 |
+
history: List[Tuple[str, str]],
|
41 |
system_message="", # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
42 |
max_tokens=1024, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
43 |
temperature=0.7, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
44 |
+
top_p=0.9, # ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ
|
45 |
):
|
46 |
+
global fashion_code, uhdimage_code, MixGEN_code, test_parquet_content
|
47 |
+
system_message = system_message or ""
|
48 |
+
system_prefix = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ. ๋๋ ์ฃผ์ด์ง ์์ค์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก "์๋น์ค ์ฌ์ฉ ์ค๋ช
๋ฐ ์๋ด, Q&A๋ฅผ ํ๋ ์ญํ ์ด๋ค". ์์ฃผ ์น์ ํ๊ณ ์์ธํ๊ฒ 4000ํ ํฐ ์ด์ Markdown ํ์์ผ๋ก ์์ฑํ๋ผ. ๋๋ ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉ ์ค๋ช
๋ฐ ์ง์ ์๋ต์ ์งํํ๋ฉฐ, ์ด์ฉ์์๊ฒ ๋์์ ์ฃผ์ด์ผ ํ๋ค. ์ด์ฉ์๊ฐ ๊ถ๊ธํด ํ ๋งํ ๋ด์ฉ์ ์น์ ํ๊ฒ ์๋ ค์ฃผ๋๋ก ํ๋ผ. ์ฝ๋ ์ ์ฒด ๋ด์ฉ์ ๋ํด์๋ ๋ณด์์ ์ ์งํ๊ณ , ํค ๊ฐ ๋ฐ ์๋ํฌ์ธํธ์ ๊ตฌ์ฒด์ ์ธ ๋ชจ๋ธ์ ๊ณต๊ฐํ์ง ๋ง๋ผ."""
|
49 |
|
50 |
if message.lower() == "ํจ์
์ฝ๋ ์คํ":
|
51 |
+
system_message += f"\n\nํจ์
์ฝ๋ ๋ด์ฉ:\n```python\n{fashion_code}\n```"
|
52 |
+
message = "ํจ์
๊ฐ์ํผํ
์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ์ค๋ช
ํ ์ค๋น๊ฐ ๋์ด์๋ค๊ณ ์๋ฆฌ๊ณ ์๋น์ค URL(https://aiqcamp-fash.hf.space)์ ํตํด ํ
์คํธ ํด๋ณด๋ผ๊ณ ์ถ๋ ฅํ๋ผ."
|
53 |
+
elif message.lower() == "uhd ์ด๋ฏธ์ง ์ฝ๋ ์คํ":
|
54 |
+
system_message += f"\n\nUHD ์ด๋ฏธ์ง ์ฝ๋ ๋ด์ฉ:\n```python\n{uhdimage_code}\n```"
|
55 |
+
message = "UHD ์ด๋ฏธ์ง ์์ฑ์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ์ค๋ช
ํ ์ค๋น๊ฐ ๋์ด์๋ค๊ณ ์๋ฆฌ๊ณ ์๋น์ค URL(https://openfree-ultpixgen.hf.space)์ ํตํด ํ
์คํธ ํด๋ณด๋ผ๊ณ ์ถ๋ ฅํ๋ผ."
|
56 |
+
elif message.lower() == "mixgen ์ฝ๋ ์คํ":
|
57 |
+
system_message += f"\n\nMixGEN ์ฝ๋ ๋ด์ฉ:\n```python\n{MixGEN_code}\n```"
|
58 |
+
message = "MixGEN3 ์ด๋ฏธ์ง ์์ฑ์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ์ค๋ช
ํ ์ค๋น๊ฐ ๋์ด์๋ค๊ณ ์๋ฆฌ๊ณ ์๋น์ค URL(https://openfree-mixgen3.hf.space)์ ํตํด ํ
์คํธ ํด๋ณด๋ผ๊ณ ์ถ๋ ฅํ๋ผ."
|
59 |
+
elif message.lower() == "test.parquet ์คํ":
|
60 |
+
system_message += f"\n\ntest.parquet ํ์ผ ๋ด์ฉ:\n```markdown\n{test_parquet_content}\n```"
|
61 |
+
message = "test.parquet ํ์ผ์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ๊ด๋ จ ์ค๋ช
๋ฐ Q&A๋ฅผ ์งํํ ์ค๋น๊ฐ ๋์ด์๋ค. ๊ถ๊ธํ ์ ์ด ์์ผ๋ฉด ๋ฌผ์ด๋ณด๋ผ."
|
62 |
|
63 |
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
|
|
|
|
64 |
for val in history:
|
65 |
if val[0]:
|
66 |
messages.append({"role": "user", "content": val[0]})
|
67 |
if val[1]:
|
68 |
messages.append({"role": "assistant", "content": val[1]})
|
|
|
69 |
messages.append({"role": "user", "content": message})
|
70 |
|
71 |
response = ""
|
72 |
+
try:
|
73 |
+
for message in hf_client.chat_completion(
|
74 |
+
messages,
|
75 |
+
max_tokens=max_tokens,
|
76 |
+
stream=True,
|
77 |
+
temperature=temperature,
|
78 |
+
top_p=top_p,
|
79 |
+
):
|
80 |
+
token = message.choices[0].delta.get('content', None)
|
81 |
+
if token:
|
82 |
+
response += token
|
83 |
+
yield response
|
84 |
+
except Exception as e:
|
85 |
+
yield f"์ถ๋ก ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
86 |
|
87 |
# Gradio ์ธํฐํ์ด์ค ์ค์ ๋ถ๋ถ๋ ์์
|
88 |
demo = gr.ChatInterface(
|
|
|
94 |
gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"),
|
95 |
],
|
96 |
examples=[
|
97 |
+
["ํจ์
์ฝ๋ ์คํ"],
|
98 |
+
["UHD ์ด๋ฏธ์ง ์ฝ๋ ์คํ"],
|
99 |
+
["MixGEN ์ฝ๋ ์คํ"],
|
100 |
+
["test.parquet ์คํ"], # ์๋ก์ด ์์ ์ถ๊ฐ
|
101 |
+
["์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ง์น ํ๋ฉด์ ๋ณด๋ฉด์ ์ค๋ช
ํ๋ฏ์ด 4000 ํ ํฐ ์ด์ ์์ธํ ์ค๋ช
ํ๋ผ"],
|
102 |
+
["FAQ 20๊ฑด์ ์์ธํ๊ฒ ์์ฑํ๋ผ. 4000ํ ํฐ ์ด์ ์ฌ์ฉํ๋ผ."],
|
103 |
+
["์ฌ์ฉ ๋ฐฉ๋ฒ๊ณผ ์ฐจ๋ณ์ , ํน์ง, ๊ฐ์ ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์ ํ๋ธ ์์ ์คํฌ๋ฆฝํธ ํํ๋ก ์์ฑํ๋ผ"],
|
104 |
+
["๋ณธ ์๋น์ค๋ฅผ SEO ์ต์ ํํ์ฌ ๋ธ๋ก๊ทธ ํฌ์คํธ(๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ, ๊ธฐ์กด ์ ์ฌ ์๋น์ค์ ๋น๊ตํ์ฌ ํน์ฅ์ , ํ์ฉ์ฒ, ๊ฐ์น, ๊ธฐ๋ํจ๊ณผ, ๊ฒฐ๋ก ์ ํฌํจ)๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ"],
|
105 |
+
["ํนํ ์ถ์์ ํ์ฉํ ๊ธฐ์ ๋ฐ ๋น์ฆ๋์ค๋ชจ๋ธ ์ธก๋ฉด์ ํฌํจํ์ฌ ํนํ ์ถ์์ ๊ตฌ์ฑ์ ๋ง๊ฒ ํ์ ์ ์ธ ์ฐฝ์ ๋ฐ๋ช
๋ด์ฉ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ."],
|
106 |
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"],
|
107 |
],
|
108 |
+
theme="Nymbo/Nymbo_Theme",
|
109 |
cache_examples=False, # ์บ์ฑ ๋นํ์ฑํ ์ค์
|
|
|
110 |
)
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
+
demo.launch()
|