Spaces:
Running
Running
import gradio as gr | |
from huggingface_hub import InferenceClient | |
import os | |
import pandas as pd | |
from typing import List, Tuple | |
# LLM ๋ชจ๋ธ ์ ์ | |
LLM_MODELS = { | |
"Default": "CohereForAI/c4ai-command-r-plus-08-2024", # ๊ธฐ๋ณธ ๋ชจ๋ธ | |
"Meta": "meta-llama/Llama-3.3-70B-Instruct", | |
"Mistral": "mistralai/Mistral-Nemo-Instruct-2407", | |
"Alibaba": "Qwen/QwQ-32B-Preview" | |
} | |
def get_client(model_name): | |
return InferenceClient(LLM_MODELS[model_name], token=os.getenv("HF_TOKEN")) | |
def analyze_file_content(content, file_type): | |
"""ํ์ผ ๋ด์ฉ์ ๋ถ์ํ์ฌ ๊ตฌ์กฐ์ ์์ฝ์ ๋ฐํ""" | |
if file_type in ['parquet', 'csv']: | |
try: | |
# ๋ฐ์ดํฐ์ ๊ตฌ์กฐ ๋ถ์ | |
lines = content.split('\n') | |
header = lines[0] | |
columns = header.count('|') - 1 | |
rows = len(lines) - 3 # ํค๋์ ๊ตฌ๋ถ์ ์ ์ธ | |
return f"๋ฐ์ดํฐ์ ๊ตฌ์กฐ: {columns}๊ฐ ์ปฌ๋ผ, {rows}๊ฐ ๋ฐ์ดํฐ ์ํ" | |
except: | |
return "๋ฐ์ดํฐ์ ๊ตฌ์กฐ ๋ถ์ ์คํจ" | |
# ํ ์คํธ/์ฝ๋ ํ์ผ์ ๊ฒฝ์ฐ | |
lines = content.split('\n') | |
total_lines = len(lines) | |
non_empty_lines = len([line for line in lines if line.strip()]) | |
if any(keyword in content.lower() for keyword in ['def ', 'class ', 'import ', 'function']): | |
functions = len([line for line in lines if 'def ' in line]) | |
classes = len([line for line in lines if 'class ' in line]) | |
imports = len([line for line in lines if 'import ' in line or 'from ' in line]) | |
return f"์ฝ๋ ๊ตฌ์กฐ ๋ถ์: ์ด {total_lines}์ค (ํจ์ {functions}๊ฐ, ํด๋์ค {classes}๊ฐ, ์ํฌํธ {imports}๊ฐ)" | |
paragraphs = content.count('\n\n') + 1 | |
words = len(content.split()) | |
return f"๋ฌธ์ ๊ตฌ์กฐ ๋ถ์: ์ด {total_lines}์ค, {paragraphs}๊ฐ ๋ฌธ๋จ, ์ฝ {words}๊ฐ ๋จ์ด" | |
def read_uploaded_file(file): | |
if file is None: | |
return "", "" | |
try: | |
file_ext = os.path.splitext(file.name)[1].lower() | |
if file_ext == '.parquet': | |
df = pd.read_parquet(file.name, engine='pyarrow') | |
content = df.head(10).to_markdown(index=False) | |
return content, "parquet" | |
elif file_ext == '.csv': | |
df = pd.read_csv(file.name) | |
content = f"๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n{df.head(10).to_markdown(index=False)}\n\n" | |
content += f"\n๋ฐ์ดํฐ ์ ๋ณด:\n" | |
content += f"- ์ด ํ ์: {len(df)}\n" | |
content += f"- ์ด ์ด ์: {len(df.columns)}\n" | |
content += f"- ์ปฌ๋ผ ๋ชฉ๋ก: {', '.join(df.columns)}\n" | |
return content, "csv" | |
else: | |
with open(file.name, 'r', encoding='utf-8') as f: | |
content = f.read() | |
return content, "text" | |
except Exception as e: | |
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "error" | |
def format_history(history): | |
formatted_history = [] | |
for user_msg, assistant_msg in history: | |
formatted_history.append({"role": "user", "content": user_msg}) | |
if assistant_msg: | |
formatted_history.append({"role": "assistant", "content": assistant_msg}) | |
return formatted_history | |
def chat(message, history, uploaded_file, model_name, system_message="", max_tokens=4000, temperature=0.7, top_p=0.9): | |
system_prefix = """๋๋ ํ์ผ ๋ถ์ ์ ๋ฌธ๊ฐ์ ๋๋ค. ์ ๋ก๋๋ ํ์ผ์ ๋ด์ฉ์ ๊น์ด ์๊ฒ ๋ถ์ํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๊ด์ ์์ ์ค๋ช ํด์ผ ํฉ๋๋ค: | |
1. ํ์ผ์ ์ ๋ฐ์ ์ธ ๊ตฌ์กฐ์ ๊ตฌ์ฑ | |
2. ์ฃผ์ ๋ด์ฉ๊ณผ ํจํด ๋ถ์ | |
3. ๋ฐ์ดํฐ์ ํน์ง๊ณผ ์๋ฏธ | |
- ๋ฐ์ดํฐ์ ์ ๊ฒฝ์ฐ: ์ปฌ๋ผ์ ์๋ฏธ, ๋ฐ์ดํฐ ํ์ , ๊ฐ์ ๋ถํฌ | |
- ํ ์คํธ/์ฝ๋์ ๊ฒฝ์ฐ: ๊ตฌ์กฐ์ ํน์ง, ์ฃผ์ ํจํด | |
4. ์ ์ฌ์ ํ์ฉ ๋ฐฉ์ | |
5. ๋ฐ์ดํฐ ํ์ง ๋ฐ ๊ฐ์ ๊ฐ๋ฅํ ๋ถ๋ถ | |
์ ๋ฌธ๊ฐ์ ๊ด์ ์์ ์์ธํ๊ณ ๊ตฌ์กฐ์ ์ธ ๋ถ์์ ์ ๊ณตํ๋, ์ดํดํ๊ธฐ ์ฝ๊ฒ ์ค๋ช ํ์ธ์. ๋ถ์ ๊ฒฐ๊ณผ๋ Markdown ํ์์ผ๋ก ์์ฑํ๊ณ , ๊ฐ๋ฅํ ํ ๊ตฌ์ฒด์ ์ธ ์์๋ฅผ ํฌํจํ์ธ์.""" | |
if uploaded_file: | |
content, file_type = read_uploaded_file(uploaded_file) | |
if file_type == "error": | |
yield "", history + [[message, content]] | |
return | |
# ํ์ผ ๋ด์ฉ ๋ถ์ ๋ฐ ๊ตฌ์กฐ์ ์์ฝ | |
file_summary = analyze_file_content(content, file_type) | |
if file_type in ['parquet', 'csv']: | |
system_message += f"\n\nํ์ผ ๋ด์ฉ:\n```markdown\n{content}\n```" | |
else: | |
system_message += f"\n\nํ์ผ ๋ด์ฉ:\n```\n{content}\n```" | |
if message == "ํ์ผ ๋ถ์์ ์์ํฉ๋๋ค.": | |
message = f"""[๊ตฌ์กฐ ๋ถ์] {file_summary} | |
๋ค์ ๊ด์ ์์ ์์ธ ๋ถ์์ ์ ๊ณตํด์ฃผ์ธ์: | |
1. ํ์ผ์ ์ ๋ฐ์ ์ธ ๊ตฌ์กฐ์ ํ์ | |
2. ์ฃผ์ ๋ด์ฉ ๋ฐ ๊ตฌ์ฑ์์ ๋ถ์ | |
3. ๋ฐ์ดํฐ/๋ด์ฉ์ ํน์ง๊ณผ ํจํด | |
4. ํ์ง ๋ฐ ์์ฑ๋ ํ๊ฐ | |
5. ๊ฐ์ ๊ฐ๋ฅํ ๋ถ๋ถ ์ ์ | |
6. ์ค์ ํ์ฉ ๋ฐฉ์ ๋ฐ ์ถ์ฒ์ฌํญ""" | |
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] | |
messages.extend(format_history(history)) | |
messages.append({"role": "user", "content": message}) | |
try: | |
client = get_client(model_name) | |
partial_message = "" | |
for msg in 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: | |
partial_message += token | |
yield "", history + [[message, partial_message]] | |
except Exception as e: | |
error_msg = f"์ถ๋ก ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" | |
yield "", history + [[message, error_msg]] | |
css = """ | |
footer {visibility: hidden} | |
""" | |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo: | |
with gr.Row(): | |
with gr.Column(scale=2): | |
chatbot = gr.Chatbot(height=600) | |
msg = gr.Textbox( | |
label="๋ฉ์์ง๋ฅผ ์ ๋ ฅํ์ธ์", | |
show_label=False, | |
placeholder="๋ฉ์์ง๋ฅผ ์ ๋ ฅํ์ธ์...", | |
container=False | |
) | |
clear = gr.ClearButton([msg, chatbot]) | |
with gr.Column(scale=1): | |
model_name = gr.Radio( | |
choices=list(LLM_MODELS.keys()), | |
value="Default", | |
label="LLM ๋ชจ๋ธ ์ ํ", | |
info="์ฌ์ฉํ LLM ๋ชจ๋ธ์ ์ ํํ์ธ์" | |
) | |
file_upload = gr.File( | |
label="ํ์ผ ์ ๋ก๋ (ํ ์คํธ, ์ฝ๋, CSV, Parquet ํ์ผ)", | |
file_types=["text", ".csv", ".parquet"], | |
type="filepath" | |
) | |
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") | |
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ | |
msg.submit( | |
chat, | |
inputs=[msg, chatbot, file_upload, model_name, system_message, max_tokens, temperature, top_p], | |
outputs=[msg, chatbot], | |
queue=True | |
).then( | |
lambda: gr.update(interactive=True), | |
None, | |
[msg] | |
) | |
# ํ์ผ ์ ๋ก๋ ์ ์๋ ๋ถ์ | |
file_upload.change( | |
chat, | |
inputs=[gr.Textbox(value="ํ์ผ ๋ถ์์ ์์ํฉ๋๋ค."), chatbot, file_upload, model_name, system_message, max_tokens, temperature, top_p], | |
outputs=[msg, chatbot], | |
queue=True | |
) | |
# ์์ ์ถ๊ฐ | |
gr.Examples( | |
examples=[ | |
["ํ์ผ์ ์ ๋ฐ์ ์ธ ๊ตฌ์กฐ์ ํน์ง์ ์์ธํ ์ค๋ช ํด์ฃผ์ธ์."], | |
["์ด ํ์ผ์ ์ฃผ์ ํจํด๊ณผ ํน์ง์ ๋ถ์ํด์ฃผ์ธ์."], | |
["ํ์ผ์ ํ์ง๊ณผ ๊ฐ์ ๊ฐ๋ฅํ ๋ถ๋ถ์ ํ๊ฐํด์ฃผ์ธ์."], | |
["์ด ํ์ผ์ ์ค์ ๋ก ์ด๋ป๊ฒ ํ์ฉํ ์ ์์๊น์?"], | |
["ํ์ผ์ ์ฃผ์ ๋ด์ฉ์ ์์ฝํ๊ณ ํต์ฌ ์ธ์ฌ์ดํธ๋ฅผ ๋์ถํด์ฃผ์ธ์."], | |
["์ด์ ๋ถ์์ ์ด์ด์ ๋ ์์ธํ ์ค๋ช ํด์ฃผ์ธ์."], | |
], | |
inputs=msg, | |
) | |
if __name__ == "__main__": | |
demo.launch() |