Update app.py
Browse files
app.py
CHANGED
@@ -1,222 +1,115 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
|
|
4 |
from typing import List, Tuple
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
# Available LLM models
|
10 |
-
LLM_MODELS = {
|
11 |
-
"Mistral": "mistralai/Mistral-7B-Instruct-v0.2",
|
12 |
-
"Zephyr": "HuggingFaceH4/zephyr-7b-beta",
|
13 |
-
"OpenChat": "openchat/openchat-3.5",
|
14 |
-
"Llama2": "meta-llama/Llama-2-7b-chat-hf",
|
15 |
-
"Phi": "microsoft/phi-2",
|
16 |
-
"Neural": "nvidia/neural-chat-7b-v3-1",
|
17 |
-
"Starling": "HuggingFaceH4/starling-lm-7b-alpha"
|
18 |
-
}
|
19 |
-
|
20 |
-
# Default selected models
|
21 |
-
DEFAULT_MODELS = [
|
22 |
-
"mistralai/Mistral-7B-Instruct-v0.2",
|
23 |
-
"HuggingFaceH4/zephyr-7b-beta",
|
24 |
-
"openchat/openchat-3.5"
|
25 |
-
]
|
26 |
|
27 |
-
|
28 |
-
clients = {
|
29 |
-
model: InferenceClient(model, token=HF_TOKEN)
|
30 |
-
for model in LLM_MODELS.values()
|
31 |
-
}
|
32 |
-
|
33 |
-
def process_file(file) -> str:
|
34 |
if file is None:
|
35 |
return ""
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
def
|
41 |
-
|
42 |
-
message: str,
|
43 |
history: List[Tuple[str, str]],
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
):
|
49 |
-
system_prefix = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ. ๋๋ ์ฃผ์ด์ง
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
messages.append({"role": "assistant", "content": assistant})
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
messages.append({"role": "user", "content": message})
|
61 |
-
|
62 |
response = ""
|
63 |
try:
|
64 |
-
for
|
65 |
messages,
|
66 |
max_tokens=max_tokens,
|
67 |
stream=True,
|
68 |
temperature=temperature,
|
69 |
top_p=top_p,
|
70 |
):
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
yield response
|
76 |
except Exception as e:
|
77 |
-
yield f"
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
)
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
def generate(client, history):
|
99 |
-
return respond_single(
|
100 |
-
client,
|
101 |
-
message,
|
102 |
-
history,
|
103 |
-
system_message,
|
104 |
-
max_tokens,
|
105 |
-
temperature,
|
106 |
-
top_p,
|
107 |
-
)
|
108 |
-
|
109 |
-
return (
|
110 |
-
generate(clients[selected_models[0]], history1),
|
111 |
-
generate(clients[selected_models[1]], history2),
|
112 |
-
generate(clients[selected_models[2]], history3),
|
113 |
-
)
|
114 |
-
|
115 |
-
css = """
|
116 |
-
footer {visibility: hidden}
|
117 |
-
"""
|
118 |
-
|
119 |
-
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
120 |
-
with gr.Row():
|
121 |
-
model_choices = gr.Checkboxgroup(
|
122 |
-
choices=list(LLM_MODELS.values()),
|
123 |
-
value=DEFAULT_MODELS,
|
124 |
-
label="Select Models (Choose up to 3)",
|
125 |
-
interactive=True
|
126 |
-
)
|
127 |
-
|
128 |
-
with gr.Row():
|
129 |
-
with gr.Column():
|
130 |
-
chat1 = gr.ChatInterface(
|
131 |
-
lambda message, history: None,
|
132 |
-
chatbot=gr.Chatbot(height=400, label="Chat 1"),
|
133 |
-
textbox=False,
|
134 |
-
)
|
135 |
-
with gr.Column():
|
136 |
-
chat2 = gr.ChatInterface(
|
137 |
-
lambda message, history: None,
|
138 |
-
chatbot=gr.Chatbot(height=400, label="Chat 2"),
|
139 |
-
textbox=False,
|
140 |
-
)
|
141 |
-
with gr.Column():
|
142 |
-
chat3 = gr.ChatInterface(
|
143 |
-
lambda message, history: None,
|
144 |
-
chatbot=gr.Chatbot(height=400, label="Chat 3"),
|
145 |
-
textbox=False,
|
146 |
-
)
|
147 |
-
|
148 |
-
with gr.Row():
|
149 |
-
with gr.Column():
|
150 |
-
system_message = gr.Textbox(
|
151 |
-
value="๋น์ ์ ์น์ ํ AI ์ด์์คํดํธ์
๋๋ค.",
|
152 |
-
label="System message"
|
153 |
-
)
|
154 |
-
max_tokens = gr.Slider(
|
155 |
-
minimum=1,
|
156 |
-
maximum=8000,
|
157 |
-
value=4000,
|
158 |
-
step=1,
|
159 |
-
label="Max new tokens"
|
160 |
-
)
|
161 |
-
temperature = gr.Slider(
|
162 |
-
minimum=0,
|
163 |
-
maximum=1,
|
164 |
-
value=0.7,
|
165 |
-
step=0.1,
|
166 |
-
label="Temperature"
|
167 |
-
)
|
168 |
-
top_p = gr.Slider(
|
169 |
-
minimum=0,
|
170 |
-
maximum=1,
|
171 |
-
value=0.9,
|
172 |
-
step=0.05,
|
173 |
-
label="Top-p"
|
174 |
-
)
|
175 |
-
|
176 |
-
with gr.Row():
|
177 |
-
file_input = gr.File(label="Upload File (optional)")
|
178 |
-
msg_input = gr.Textbox(
|
179 |
-
show_label=False,
|
180 |
-
placeholder="Enter text and press enter",
|
181 |
-
container=False
|
182 |
-
)
|
183 |
-
|
184 |
-
examples = [
|
185 |
["์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ง์น ํ๋ฉด์ ๋ณด๋ฉด์ ์ค๋ช
ํ๋ฏ์ด 4000 ํ ํฐ ์ด์ ์์ธํ ์ค๋ช
ํ๋ผ"],
|
186 |
["FAQ 20๊ฑด์ ์์ธํ๊ฒ ์์ฑํ๋ผ. 4000ํ ํฐ ์ด์ ์ฌ์ฉํ๋ผ."],
|
187 |
["์ฌ์ฉ ๋ฐฉ๋ฒ๊ณผ ์ฐจ๋ณ์ , ํน์ง, ๊ฐ์ ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์ ํ๋ธ ์์ ์คํฌ๋ฆฝํธ ํํ๋ก ์์ฑํ๋ผ"],
|
188 |
["๋ณธ ์๋น์ค๋ฅผ SEO ์ต์ ํํ์ฌ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ"],
|
|
|
189 |
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"],
|
190 |
-
]
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
inputs=msg_input,
|
195 |
-
cache_examples=False
|
196 |
-
)
|
197 |
-
|
198 |
-
def submit_message(message, file):
|
199 |
-
return respond_all(
|
200 |
-
message,
|
201 |
-
file,
|
202 |
-
chat1.chatbot.value,
|
203 |
-
chat2.chatbot.value,
|
204 |
-
chat3.chatbot.value,
|
205 |
-
model_choices.value,
|
206 |
-
system_message.value,
|
207 |
-
max_tokens.value,
|
208 |
-
temperature.value,
|
209 |
-
top_p.value,
|
210 |
-
)
|
211 |
-
|
212 |
-
msg_input.submit(
|
213 |
-
submit_message,
|
214 |
-
[msg_input, file_input],
|
215 |
-
[chat1.chatbot, chat2.chatbot, chat3.chatbot],
|
216 |
-
api_name="submit"
|
217 |
-
)
|
218 |
|
219 |
if __name__ == "__main__":
|
220 |
-
|
221 |
-
print("Warning: HF_TOKEN environment variable is not set")
|
222 |
-
demo.launch()
|
|
|
1 |
+
|
2 |
+
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import os
|
6 |
+
import pandas as pd
|
7 |
from typing import List, Tuple
|
8 |
|
9 |
+
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
10 |
+
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def read_uploaded_file(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
if file is None:
|
14 |
return ""
|
15 |
+
try:
|
16 |
+
if file.name.endswith('.parquet'):
|
17 |
+
df = pd.read_parquet(file.name, engine='pyarrow')
|
18 |
+
return df.head(10).to_markdown(index=False)
|
19 |
+
else:
|
20 |
+
content = file.read()
|
21 |
+
if isinstance(content, bytes):
|
22 |
+
return content.decode('utf-8')
|
23 |
+
return content
|
24 |
+
except Exception as e:
|
25 |
+
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
26 |
|
27 |
+
def respond(
|
28 |
+
message,
|
|
|
29 |
history: List[Tuple[str, str]],
|
30 |
+
fashion_file, # ํ์ผ ์
๋ก๋ ์
๋ ฅ
|
31 |
+
uhd_file, # ํ์ผ ์
๋ก๋ ์
๋ ฅ
|
32 |
+
mixgen_file, # ํ์ผ ์
๋ก๋ ์
๋ ฅ
|
33 |
+
parquet_file, # ํ์ผ ์
๋ก๋ ์
๋ ฅ
|
34 |
+
system_message="",
|
35 |
+
max_tokens=1024,
|
36 |
+
temperature=0.7,
|
37 |
+
top_p=0.9,
|
38 |
):
|
39 |
+
system_prefix = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ. ๋๋ ์ฃผ์ด์ง ์์ค์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก "์๋น์ค ์ฌ์ฉ ์ค๋ช
๋ฐ ์๋ด, Q&A๋ฅผ ํ๋ ์ญํ ์ด๋ค". ์์ฃผ ์น์ ํ๊ณ ์์ธํ๊ฒ 4000ํ ํฐ ์ด์ Markdown ํ์์ผ๋ก ์์ฑํ๋ผ. ๋๋ ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉ ์ค๋ช
๋ฐ ์ง์ ์๋ต์ ์งํํ๋ฉฐ, ์ด์ฉ์์๊ฒ ๋์์ ์ฃผ์ด์ผ ํ๋ค. ์ด์ฉ์๊ฐ ๊ถ๊ธํด ํ ๋งํ ๋ด์ฉ์ ์น์ ํ๊ฒ ์๋ ค์ฃผ๋๋ก ํ๋ผ. ์ฝ๋ ์ ์ฒด ๋ด์ฉ์ ๋ํด์๋ ๋ณด์์ ์ ์งํ๊ณ , ํค ๊ฐ ๋ฐ ์๋ํฌ์ธํธ์ ๊ตฌ์ฒด์ ์ธ ๋ชจ๋ธ์ ๊ณต๊ฐํ์ง ๋ง๋ผ."""
|
40 |
+
|
41 |
+
if message.lower() == "ํจ์
์ฝ๋ ์คํ" and fashion_file is not None:
|
42 |
+
fashion_content = read_uploaded_file(fashion_file)
|
43 |
+
system_message += f"\n\nํจ์
์ฝ๋ ๋ด์ฉ:\n```python\n{fashion_content}\n```"
|
44 |
+
message = "ํจ์
๊ฐ์ํผํ
์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ์ค๋ช
ํ ์ค๋น๊ฐ ๋์ด์๋ค๊ณ ์๋ฆฌ๊ณ ์๋น์ค URL(https://aiqcamp-fash.hf.space)์ ํตํด ํ
์คํธ ํด๋ณด๋ผ๊ณ ์ถ๋ ฅํ๋ผ."
|
45 |
|
46 |
+
elif message.lower() == "uhd ์ด๋ฏธ์ง ์ฝ๋ ์คํ" and uhd_file is not None:
|
47 |
+
uhd_content = read_uploaded_file(uhd_file)
|
48 |
+
system_message += f"\n\nUHD ์ด๋ฏธ์ง ์ฝ๋ ๋ด์ฉ:\n```python\n{uhd_content}\n```"
|
49 |
+
message = "UHD ์ด๋ฏธ์ง ์์ฑ์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ์ค๋ช
ํ ์ค๋น๊ฐ ๋์ด์๋ค๊ณ ์๋ฆฌ๊ณ ์๋น์ค URL(https://openfree-ultpixgen.hf.space)์ ํตํด ํ
์คํธ ํด๋ณด๋ผ๊ณ ์ถ๋ ฅํ๋ผ."
|
50 |
|
51 |
+
elif message.lower() == "mixgen ์ฝ๋ ์คํ" and mixgen_file is not None:
|
52 |
+
mixgen_content = read_uploaded_file(mixgen_file)
|
53 |
+
system_message += f"\n\nMixGEN ์ฝ๋ ๋ด์ฉ:\n```python\n{mixgen_content}\n```"
|
54 |
+
message = "MixGEN3 ์ด๋ฏธ์ง ์์ฑ์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ์ค๋ช
ํ ์ค๋น๊ฐ ๋์ด์๋ค๊ณ ์๋ฆฌ๊ณ ์๋น์ค URL(https://openfree-mixgen3.hf.space)์ ํตํด ํ
์คํธ ํด๋ณด๋ผ๊ณ ์ถ๋ ฅํ๋ผ."
|
|
|
55 |
|
56 |
+
elif message.lower() == "test.parquet ์คํ" and parquet_file is not None:
|
57 |
+
parquet_content = read_uploaded_file(parquet_file)
|
58 |
+
system_message += f"\n\ntest.parquet ํ์ผ ๋ด์ฉ:\n```markdown\n{parquet_content}\n```"
|
59 |
+
message = "test.parquet ํ์ผ์ ๋ํ ๋ด์ฉ์ ํ์ตํ์๊ณ , ๊ด๋ จ ์ค๋ช
๋ฐ Q&A๋ฅผ ์งํํ ์ค๋น๊ฐ ๋์ด์๋ค. ๊ถ๊ธํ ์ ์ด ์์ผ๋ฉด ๋ฌผ์ด๋ณด๋ผ."
|
60 |
+
|
61 |
+
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
62 |
+
for val in history:
|
63 |
+
if val[0]:
|
64 |
+
messages.append({"role": "user", "content": val[0]})
|
65 |
+
if val[1]:
|
66 |
+
messages.append({"role": "assistant", "content": val[1]})
|
67 |
messages.append({"role": "user", "content": message})
|
68 |
+
|
69 |
response = ""
|
70 |
try:
|
71 |
+
for message in hf_client.chat_completion(
|
72 |
messages,
|
73 |
max_tokens=max_tokens,
|
74 |
stream=True,
|
75 |
temperature=temperature,
|
76 |
top_p=top_p,
|
77 |
):
|
78 |
+
token = message.choices[0].delta.get('content', None)
|
79 |
+
if token:
|
80 |
+
response += token
|
81 |
+
yield response
|
|
|
82 |
except Exception as e:
|
83 |
+
yield f"์ถ๋ก ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
84 |
+
|
85 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
86 |
+
demo = gr.ChatInterface(
|
87 |
+
respond,
|
88 |
+
additional_inputs=[
|
89 |
+
gr.File(label="Fashion Code File", file_types=[".cod", ".txt", ".py"]),
|
90 |
+
gr.File(label="UHD Image Code File", file_types=[".cod", ".txt", ".py"]),
|
91 |
+
gr.File(label="MixGEN Code File", file_types=[".cod", ".txt", ".py"]),
|
92 |
+
gr.File(label="Parquet File", file_types=[".parquet"]),
|
93 |
+
gr.Textbox(label="System Message", value=""),
|
94 |
+
gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens"),
|
95 |
+
gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature"),
|
96 |
+
gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"),
|
97 |
+
],
|
98 |
+
examples=[
|
99 |
+
["ํจ์
์ฝ๋ ์คํ"],
|
100 |
+
["UHD ์ด๋ฏธ์ง ์ฝ๋ ์คํ"],
|
101 |
+
["MixGEN ์ฝ๋ ์คํ"],
|
102 |
+
["test.parquet ์คํ"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
["์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ง์น ํ๋ฉด์ ๋ณด๋ฉด์ ์ค๋ช
ํ๋ฏ์ด 4000 ํ ํฐ ์ด์ ์์ธํ ์ค๋ช
ํ๋ผ"],
|
104 |
["FAQ 20๊ฑด์ ์์ธํ๊ฒ ์์ฑํ๋ผ. 4000ํ ํฐ ์ด์ ์ฌ์ฉํ๋ผ."],
|
105 |
["์ฌ์ฉ ๋ฐฉ๋ฒ๊ณผ ์ฐจ๋ณ์ , ํน์ง, ๊ฐ์ ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์ ํ๋ธ ์์ ์คํฌ๋ฆฝํธ ํํ๋ก ์์ฑํ๋ผ"],
|
106 |
["๋ณธ ์๋น์ค๋ฅผ SEO ์ต์ ํํ์ฌ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ"],
|
107 |
+
["ํนํ ์ถ์์ ํ์ฉํ ๊ธฐ์ ๋ฐ ๋น์ฆ๋์ค๋ชจ๋ธ ์ธก๋ฉด์ ํฌํจํ์ฌ ํนํ ์ถ์์ ๊ตฌ์ฑ์ ๋ง๊ฒ ์์ฑํ๋ผ"],
|
108 |
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"],
|
109 |
+
],
|
110 |
+
theme="Nymbo/Nymbo_Theme",
|
111 |
+
cache_examples=False,
|
112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
+
demo.launch()
|
|
|
|