File size: 13,483 Bytes
747ccea fe67895 90e9b67 da419ba f779047 54a4802 e74c3bc 90e9b67 0e5afe0 c36dc6b 6da265e c36dc6b 6da265e c36dc6b 6da265e 90e9b67 c3ae439 90e9b67 c36dc6b de57dc5 6da265e 4f42976 90e9b67 747ccea da419ba ff1c374 71edfae ca69132 c3ae439 ca69132 4f42976 6da265e c3ae439 54e6271 c36dc6b c3ae439 54e6271 da419ba c3ae439 c3d05ea 90e9b67 ca69132 884361c 90e9b67 4f42976 884361c ea2c4cf 4f42976 da419ba 747ccea 91c1d45 90e9b67 4f42976 90e9b67 4f42976 90e9b67 c3ae439 90e9b67 8321675 4f42976 71edfae ea2c4cf 884361c ca69132 884361c 4f42976 ea2c4cf 4f42976 ea2c4cf 4f42976 ea2c4cf 4f42976 ea2c4cf 4f42976 ea2c4cf ca69132 884361c ca69132 4f42976 ca69132 4f42976 71edfae 4f42976 884361c 71edfae 884361c 71edfae 884361c ca69132 884361c 71edfae 884361c ca69132 884361c ca69132 884361c ca69132 884361c ca69132 884361c ca69132 884361c 4f42976 884361c ca69132 4f42976 71edfae 4f42976 ca69132 4f42976 884361c 4f42976 747ccea 90e9b67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
import gradio as gr
from huggingface_hub import InferenceClient
import os
import pandas as pd
from typing import List, Tuple
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
# hf_client = InferenceClient("CohereForAI/aya-23-35B", token=os.getenv("HF_TOKEN"))
def load_code(filename):
try:
with open(filename, 'r', encoding='utf-8') as file:
return file.read()
except FileNotFoundError:
return f"{filename} νμΌμ μ°Ύμ μ μμ΅λλ€."
except Exception as e:
return f"νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
def load_parquet(filename):
try:
df = pd.read_parquet(filename, engine='pyarrow')
# λ°μ΄ν°νλ μμ 첫 λͺ νμ λ¬Έμμ΄λ‘ λ³ν (Markdown ν νμ)
return df.head(10).to_markdown(index=False)
except FileNotFoundError:
return f"{filename} νμΌμ μ°Ύμ μ μμ΅λλ€."
except Exception as e:
return f"νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
# μ½λ νμΌ λ‘λ
fashion_code = load_code('fashion.cod')
uhdimage_code = load_code('uhdimage.cod')
MixGEN_code = load_code('mgen.cod')
# μ΄κΈ° Parquet νμΌ λ‘λ (κΈ°μ‘΄ test.parquet)
test_parquet_content = load_parquet('test.parquet')
def respond(
message,
history: List[Tuple[str, str]],
system_message="", # κΈ°λ³Έκ° μΆκ°
max_tokens=4000, # κΈ°λ³Έκ° λ³κ²½
temperature=0.7, # κΈ°λ³Έκ° μ μ§
top_p=0.9, # κΈ°λ³Έκ° μ μ§
):
# μμ€ν
ν둬ννΈ μ€μ
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ£Όμ΄μ§ μμ€μ½λλ₯Ό κΈ°λ°μΌλ‘ "μλΉμ€ μ¬μ© μ€λͺ
λ° μλ΄, Q&Aλ₯Ό νλ μν μ΄λ€". μμ£Ό μΉμ νκ³ μμΈνκ² 4000ν ν° μ΄μ Markdown νμμΌλ‘ μμ±νλΌ. λλ μ½λλ₯Ό κΈ°λ°μΌλ‘ μ¬μ© μ€λͺ
λ° μ§μ μλ΅μ μ§ννλ©°, μ΄μ©μμκ² λμμ μ£Όμ΄μΌ νλ€. μ΄μ©μκ° κΆκΈν΄ ν λ§ν λ΄μ©μ μΉμ νκ² μλ €μ£Όλλ‘ νλΌ. μ½λ μ 체 λ΄μ©μ λν΄μλ 보μμ μ μ§νκ³ , ν€ κ° λ° μλν¬μΈνΈμ ꡬ체μ μΈ λͺ¨λΈμ 곡κ°νμ§ λ§λΌ."""
# λͺ
λ Ήμ΄ μ²λ¦¬
if message.lower() == "ν¨μ
μ½λ μ€ν":
system_message += f"\n\nν¨μ
μ½λ λ΄μ©:\n```python\n{fashion_code}\n```"
message = "ν¨μ
κ°μνΌν
μ λν λ΄μ©μ νμ΅νμκ³ , μ€λͺ
ν μ€λΉκ° λμ΄μλ€κ³ μλ¦¬κ³ μλΉμ€ URL(https://aiqcamp-fash.hf.space)μ ν΅ν΄ ν
μ€νΈ ν΄λ³΄λΌκ³ μΆλ ₯νλΌ."
elif message.lower() == "uhd μ΄λ―Έμ§ μ½λ μ€ν":
system_message += f"\n\nUHD μ΄λ―Έμ§ μ½λ λ΄μ©:\n```python\n{uhdimage_code}\n```"
message = "UHD μ΄λ―Έμ§ μμ±μ λν λ΄μ©μ νμ΅νμκ³ , μ€λͺ
ν μ€λΉκ° λμ΄μλ€κ³ μλ¦¬κ³ μλΉμ€ URL(https://openfree-ultpixgen.hf.space)μ ν΅ν΄ ν
μ€νΈ ν΄λ³΄λΌκ³ μΆλ ₯νλΌ."
elif message.lower() == "mixgen μ½λ μ€ν":
system_message += f"\n\nMixGEN μ½λ λ΄μ©:\n```python\n{MixGEN_code}\n```"
message = "MixGEN3 μ΄λ―Έμ§ μμ±μ λν λ΄μ©μ νμ΅νμκ³ , μ€λͺ
ν μ€λΉκ° λμ΄μλ€κ³ μλ¦¬κ³ μλΉμ€ URL(https://openfree-mixgen3.hf.space)μ ν΅ν΄ ν
μ€νΈ ν΄λ³΄λΌκ³ μΆλ ₯νλΌ."
elif message.lower() == "test.parquet μ€ν":
# νμ¬ Parquet λ΄μ©μ μν λ³μμμ κ°μ ΈμμΌ ν¨
current_parquet_content = history.get('parquet_content', "")
system_message += f"\n\ntest.parquet νμΌ λ΄μ©:\n```markdown\n{current_parquet_content}\n```"
message = "test.parquet νμΌμ λν λ΄μ©μ νμ΅νμκ³ , κ΄λ ¨ μ€λͺ
λ° Q&Aλ₯Ό μ§νν μ€λΉκ° λμ΄μλ€. κΆκΈν μ μ΄ μμΌλ©΄ λ¬Όμ΄λ³΄λΌ."
elif message.lower() == "csv μ
λ‘λ":
message = "CSV νμΌμ μ
λ‘λνλ €λ©΄ λ λ²μ§Έ νμ μ¬μ©νμΈμ."
# μμ€ν
λ©μμ§μ μ¬μ©μ λ©μμ§ κ²°ν©
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
try:
for msg in hf_client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = msg.choices[0].delta.get('content', None)
if token:
response += token
yield response
except Exception as e:
yield f"μΆλ‘ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
def upload_csv(file):
try:
# CSV νμΌ μ½κΈ° (ꡬλΆμ μ½€λ§)
df = pd.read_csv(file, sep=',')
# CSV νμΌμ μ»¬λΌ νμΈ
required_columns = {'id', 'text', 'label', 'metadata'}
available_columns = set(df.columns)
missing_columns = required_columns - available_columns
if missing_columns:
return f"CSV νμΌμ λ€μ νμ 컬λΌμ΄ λλ½λμμ΅λλ€: {', '.join(missing_columns)}", None
# λ°μ΄ν° ν΄λ μ§
df.drop_duplicates(inplace=True)
df.fillna('', inplace=True)
# λ°μ΄ν° μ ν μ΅μ ν
df = df.astype({'id': 'int32', 'text': 'string', 'label': 'category', 'metadata': 'string'})
# Parquet νμΌλ‘ λ³ν
parquet_filename = os.path.splitext(os.path.basename(file))[0] + '.parquet'
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy')
# Parquet νμΌ λ‘λ
parquet_content = load_parquet(parquet_filename)
return f"{parquet_filename} νμΌμ΄ μ±κ³΅μ μΌλ‘ μ
λ‘λλκ³ λ³νλμμ΅λλ€.", parquet_filename
except Exception as e:
return f"CSV νμΌ μ
λ‘λ λ° λ³ν μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", None
def upload_parquet(file):
try:
# Parquet νμΌ μ½κΈ°
df = pd.read_parquet(file, engine='pyarrow')
# λ°μ΄ν°νλ μμ MarkdownμΌλ‘ λ³ν
parquet_content = df.to_markdown(index=False)
return "Parquet νμΌμ΄ μ±κ³΅μ μΌλ‘ μ
λ‘λλμμ΅λλ€.", parquet_content, df.to_json() # JSONμΌλ‘ λ°μ΄ν° μ μ₯
except Exception as e:
return f"Parquet νμΌ μ
λ‘λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", None, None
# Gradio Blocks μΈν°νμ΄μ€ μ€μ
with gr.Blocks() as demo:
gr.Markdown("# LLM μλΉμ€ μΈν°νμ΄μ€")
with gr.Tab("μ±λ΄"):
gr.Markdown("### LLMκ³Ό λννκΈ°")
chat = gr.ChatInterface(
fn=respond,
examples=[
["ν¨μ
μ½λ μ€ν"],
["UHD μ΄λ―Έμ§ μ½λ μ€ν"],
["MixGEN μ½λ μ€ν"],
["test.parquet μ€ν"],
["μμΈν μ¬μ© λ°©λ²μ λ§μΉ νλ©΄μ 보면μ μ€λͺ
νλ―μ΄ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"],
["FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."],
["μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
["λ³Έ μλΉμ€λ₯Ό SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈ(λ°°κ²½ λ° νμμ±, κΈ°μ‘΄ μ μ¬ μλΉμ€μ λΉκ΅νμ¬ νΉμ₯μ , νμ©μ², κ°μΉ, κΈ°λν¨κ³Ό, κ²°λ‘ μ ν¬ν¨)λ‘ 4000 ν ν° μ΄μ μμ±νλΌ"],
["νΉν μΆμμ νμ©ν κΈ°μ λ° λΉμ¦λμ€λͺ¨λΈ μΈ‘λ©΄μ ν¬ν¨νμ¬ νΉν μΆμμ ꡬμ±μ λ§κ² νμ μ μΈ μ°½μ λ°λͺ
λ΄μ©μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μμ±νλΌ."],
["κ³μ μ΄μ΄μ λ΅λ³νλΌ"],
],
theme="default", # μνλ ν
λ§λ‘ λ³κ²½ κ°λ₯
)
with gr.Accordion("μμ€ν
ν둬ννΈ λ° μ΅μ
μ€μ ", open=False):
system_message = gr.Textbox(label="System Message", value="")
max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens")
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
with gr.Tab("λ°μ΄ν° λ³ν"):
gr.Markdown("### CSV νμΌ μ
λ‘λ λ° Parquet λ³ν")
with gr.Row():
with gr.Column():
csv_file = gr.File(label="CSV νμΌ μ
λ‘λ", type="file")
upload_button = gr.Button("μ
λ‘λ λ° λ³ν")
upload_status = gr.Textbox(label="μ
λ‘λ μν", interactive=False)
parquet_preview = gr.Markdown(label="Parquet νμΌ λ―Έλ¦¬λ³΄κΈ°")
download_button = gr.File(label="Parquet νμΌ λ€μ΄λ‘λ", file=None, interactive=False)
# μ
λ‘λ λ²νΌ ν΄λ¦ μ μ€νν ν¨μ
def handle_csv_upload(file):
message, parquet_filename = upload_csv(file.name)
if parquet_filename:
# νμΌμ λ€μ΄λ‘λν μ μλλ‘ κ²½λ‘ μ€μ
with open(parquet_filename, "rb") as f:
data = f.read()
return message, parquet_preview.update(value=load_parquet(parquet_filename)), gr.File.update(value=(parquet_filename, data))
else:
return message, "", None
upload_button.click(
handle_csv_upload,
inputs=csv_file,
outputs=[upload_status, parquet_preview, download_button]
)
gr.Markdown("### κΈ°μ‘΄ Parquet νμΌ")
gr.Markdown(f"**test.parquet νμΌ λ΄μ©:**\n```markdown\n{test_parquet_content}\n```")
with gr.Tab("μ±λ΄"):
gr.Markdown("### Parquet νμΌ μ
λ‘λ λ° μ§λ¬ΈνκΈ°")
with gr.Row():
with gr.Column():
parquet_upload = gr.File(label="Parquet νμΌ μ
λ‘λ", type="file")
parquet_upload_button = gr.Button("μ
λ‘λ")
parquet_upload_status = gr.Textbox(label="μ
λ‘λ μν", interactive=False)
parquet_preview_chat = gr.Markdown(label="Parquet νμΌ λ―Έλ¦¬λ³΄κΈ°")
# μνλ₯Ό μ μ₯ν Hidden State
parquet_data_state = gr.State()
def handle_parquet_upload(file):
message, parquet_content, parquet_json = upload_parquet(file.name)
if parquet_json:
return message, parquet_preview_chat.update(value=parquet_content), parquet_data_state.update(value=parquet_json)
else:
return message, gr.Markdown.update(value=""), parquet_data_state.update(value=None)
parquet_upload_button.click(
handle_parquet_upload,
inputs=parquet_upload,
outputs=[parquet_upload_status, parquet_preview_chat, parquet_data_state]
)
gr.Markdown("### LLMκ³Ό λννκΈ°")
chat_interface = gr.ChatInterface(
fn=respond,
examples=[
["ν¨μ
μ½λ μ€ν"],
["UHD μ΄λ―Έμ§ μ½λ μ€ν"],
["MixGEN μ½λ μ€ν"],
["test.parquet μ€ν"],
["μμΈν μ¬μ© λ°©λ²μ λ§μΉ νλ©΄μ 보면μ μ€λͺ
νλ―μ΄ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"],
["FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."],
["μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
["λ³Έ μλΉμ€λ₯Ό SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈ(λ°°κ²½ λ° νμμ±, κΈ°μ‘΄ μ μ¬ μλΉμ€μ λΉκ΅νμ¬ νΉμ₯μ , νμ©μ², κ°μΉ, κΈ°λν¨κ³Ό, κ²°λ‘ μ ν¬ν¨)λ‘ 4000 ν ν° μ΄μ μμ±νλΌ"],
["νΉν μΆμμ νμ©ν κΈ°μ λ° λΉμ¦λμ€λͺ¨λΈ μΈ‘λ©΄μ ν¬ν¨νμ¬ νΉν μΆμμ ꡬμ±μ λ§κ² νμ μ μΈ μ°½μ λ°λͺ
λ΄μ©μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μμ±νλΌ."],
["κ³μ μ΄μ΄μ λ΅λ³νλΌ"],
],
theme="default", # μνλ ν
λ§λ‘ λ³κ²½ κ°λ₯
)
gr.Markdown("## μ£Όμ μ¬ν")
gr.Markdown("""
- **CSV μ
λ‘λ**: CSV νμΌμ μ
λ‘λνλ©΄ μλμΌλ‘ Parquet νμΌλ‘ λ³νλ©λλ€. CSV νμΌμ λ°λμ **μ½€λ§(`,`)**λ‘ κ΅¬λΆλμ΄μΌ ν©λλ€.
- **Parquet 미리보기**: μ
λ‘λλ Parquet νμΌμ 첫 10κ° νμ΄ λ―Έλ¦¬λ³΄κΈ°λ‘ νμλ©λλ€.
- **LLMκ³Όμ λν**: λ³νλ Parquet νμΌ λ΄μ©μ κΈ°λ°μΌλ‘ LLMμ΄ μλ΅μ μμ±ν©λλ€.
- **Parquet λ€μ΄λ‘λ**: λ³νλ Parquet νμΌμ λ€μ΄λ‘λνλ €λ©΄ λ³νλ νμΌ μμ λ€μ΄λ‘λ λ§ν¬λ₯Ό ν΄λ¦νμΈμ.
- **μ±λ΄ Parquet μ
λ‘λ**: μ±λ΄ νμμ Parquet νμΌμ μ
λ‘λνλ©΄ ν΄λΉ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ§λ¬Έκ³Ό λ΅λ³μ μ§νν μ μμ΅λλ€.
""")
gr.Markdown("### Gradio μΈν°νμ΄μ€λ₯Ό μ¬μ©νμ¬ LLM λͺ¨λΈκ³Ό μνΈμμ©νμΈμ!")
if __name__ == "__main__":
demo.launch()
|