|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import os |
|
import pandas as pd |
|
from typing import List, Tuple |
|
|
|
|
|
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", 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') |
|
|
|
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') |
|
|
|
|
|
test_parquet_content = load_parquet('test.parquet') |
|
|
|
def respond( |
|
message, |
|
history: List[Tuple[str, str]], |
|
system_message="", |
|
max_tokens=1024, |
|
temperature=0.7, |
|
top_p=0.9, |
|
parquet_content="", |
|
): |
|
global fashion_code, uhdimage_code, MixGEN_code, test_parquet_content |
|
system_message = system_message or "" |
|
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 μ€ν": |
|
system_message += f"\n\ntest.parquet νμΌ λ΄μ©:\n```markdown\n{test_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: |
|
|
|
df = pd.read_csv(file.name) |
|
|
|
|
|
df.drop_duplicates(inplace=True) |
|
df.fillna('', inplace=True) |
|
|
|
|
|
df = df.astype({'id': 'int32', 'text': 'string', 'label': 'category', 'metadata': 'string'}) |
|
|
|
|
|
parquet_filename = os.path.splitext(file.name)[0] + '.parquet' |
|
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy') |
|
|
|
|
|
parquet_content = load_parquet(parquet_filename) |
|
|
|
return f"{parquet_filename} νμΌμ΄ μ±κ³΅μ μΌλ‘ μ
λ‘λλκ³ λ³νλμμ΅λλ€.", parquet_content |
|
except Exception as e: |
|
return f"CSV νμΌ μ
λ‘λ λ° λ³ν μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# LLM μλΉμ€ μΈν°νμ΄μ€") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
gr.Markdown("### CSV νμΌ μ
λ‘λ λ° Parquet λ³ν") |
|
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 νμΌ λ―Έλ¦¬λ³΄κΈ°") |
|
|
|
|
|
upload_button.click( |
|
upload_csv, |
|
inputs=csv_file, |
|
outputs=[upload_status, parquet_preview] |
|
) |
|
|
|
gr.Markdown("### κΈ°μ‘΄ Parquet νμΌ") |
|
gr.Markdown(f"**test.parquet νμΌ λ΄μ©:**\n```markdown\n{test_parquet_content}\n```") |
|
|
|
with gr.Column(): |
|
|
|
gr.Markdown("### LLMκ³Ό λννκΈ°") |
|
chat_history = gr.State([]) |
|
parquet_content_state = gr.State(test_parquet_content) |
|
|
|
def chat_respond( |
|
message, |
|
history, |
|
system_message, |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
parquet_content |
|
): |
|
|
|
response = respond(message, history, system_message, max_tokens, temperature, top_p, parquet_content) |
|
return response |
|
|
|
chat = gr.Chatbot() |
|
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.Row(): |
|
user_message = gr.Textbox(label="λ©μμ§ μ
λ ₯") |
|
send_button = gr.Button("보λ΄κΈ°") |
|
|
|
send_button.click( |
|
chat_respond, |
|
inputs=[user_message, chat_history, system_message, max_tokens, temperature, top_p, parquet_preview], |
|
outputs=[chat, chat_history] |
|
) |
|
|
|
gr.Markdown("## μ¬μ© μμ ") |
|
gr.Examples( |
|
examples=[ |
|
["ν¨μ
μ½λ μ€ν"], |
|
["UHD μ΄λ―Έμ§ μ½λ μ€ν"], |
|
["MixGEN μ½λ μ€ν"], |
|
["test.parquet μ€ν"], |
|
["μμΈν μ¬μ© λ°©λ²μ λ§μΉ νλ©΄μ 보면μ μ€λͺ
νλ―μ΄ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"], |
|
["FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."], |
|
["μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"], |
|
["λ³Έ μλΉμ€λ₯Ό SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈ(λ°°κ²½ λ° νμμ±, κΈ°μ‘΄ μ μ¬ μλΉμ€μ λΉκ΅νμ¬ νΉμ₯μ , νμ©μ², κ°μΉ, κΈ°λν¨κ³Ό, κ²°λ‘ μ ν¬ν¨)λ‘ 4000 ν ν° μ΄μ μμ±νλΌ"], |
|
["νΉν μΆμμ νμ©ν κΈ°μ λ° λΉμ¦λμ€λͺ¨λΈ μΈ‘λ©΄μ ν¬ν¨νμ¬ νΉν μΆμμ ꡬμ±μ λ§κ² νμ μ μΈ μ°½μ λ°λͺ
λ΄μ©μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μμ±νλΌ."], |
|
["κ³μ μ΄μ΄μ λ΅λ³νλΌ"], |
|
], |
|
inputs=user_message, |
|
label="μμ μ ν", |
|
) |
|
|
|
gr.Markdown("## μ£Όμ μ¬ν") |
|
gr.Markdown(""" |
|
- **CSV μ
λ‘λ**: CSV νμΌμ μ
λ‘λνλ©΄ μλμΌλ‘ Parquet νμΌλ‘ λ³νλ©λλ€. |
|
- **Parquet 미리보기**: μ
λ‘λλ Parquet νμΌμ 첫 10κ° νμ΄ λ―Έλ¦¬λ³΄κΈ°λ‘ νμλ©λλ€. |
|
- **LLMκ³Όμ λν**: λ³νλ Parquet νμΌ λ΄μ©μ κΈ°λ°μΌλ‘ LLMμ΄ μλ΅μ μμ±ν©λλ€. |
|
""") |
|
|
|
gr.Markdown("### Gradio μΈν°νμ΄μ€λ₯Ό μ¬μ©νμ¬ LLM λͺ¨λΈκ³Ό μνΈμμ©νμΈμ!") |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|