ginipharm / app.py
openfree's picture
Update app.py
ad9db85 verified
raw
history blame
6.48 kB
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", # κΈ°λ³Έ λͺ¨λΈ
"Mistral": "mistralai/Mistral-7B-Instruct-v0.2",
"Zephyr": "HuggingFaceH4/zephyr-7b-beta",
"OpenChat": "openchat/openchat-3.5",
"Llama2": "meta-llama/Llama-2-7b-chat-hf",
"Phi": "microsoft/phi-2",
"Neural": "nvidia/neural-chat-7b-v3-1",
"Starling": "HuggingFaceH4/starling-lm-7b-alpha"
}
def get_client(model_name):
return InferenceClient(LLM_MODELS[model_name], token=os.getenv("HF_TOKEN"))
def read_uploaded_file(file):
if file is None:
return ""
try:
if file.name.endswith('.parquet'):
df = pd.read_parquet(file.name, engine='pyarrow')
return df.head(10).to_markdown(index=False)
else:
content = file.read()
if isinstance(content, bytes):
return content.decode('utf-8')
return content
except Exception as e:
return f"νŒŒμΌμ„ μ½λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
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 = """λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ 닡변할것. λ„ˆλŠ” 주어진 μ†ŒμŠ€μ½”λ“œλ‚˜ 데이터λ₯Ό 기반으둜 "μ„œλΉ„μŠ€ μ‚¬μš© μ„€λͺ… 및 μ•ˆλ‚΄, Q&Aλ₯Ό ν•˜λŠ” 역할이닀". μ•„μ£Ό μΉœμ ˆν•˜κ³  μžμ„Έν•˜κ²Œ 4000토큰 이상 Markdown ν˜•μ‹μœΌλ‘œ μž‘μ„±ν•˜λΌ. λ„ˆλŠ” μž…λ ₯된 λ‚΄μš©μ„ 기반으둜 μ‚¬μš© μ„€λͺ… 및 질의 응닡을 μ§„ν–‰ν•˜λ©°, μ΄μš©μžμ—κ²Œ 도움을 μ£Όμ–΄μ•Ό ν•œλ‹€. μ΄μš©μžκ°€ κΆκΈˆν•΄ ν•  λ§Œν•œ λ‚΄μš©μ— μΉœμ ˆν•˜κ²Œ μ•Œλ €μ£Όλ„λ‘ ν•˜λΌ. 전체 λ‚΄μš©μ— λŒ€ν•΄μ„œλŠ” λ³΄μ•ˆμ„ μœ μ§€ν•˜κ³ , ν‚€ κ°’ 및 μ—”λ“œν¬μΈνŠΈμ™€ ꡬ체적인 λͺ¨λΈμ€ κ³΅κ°œν•˜μ§€ 마라."""
if uploaded_file:
content = read_uploaded_file(uploaded_file)
file_extension = os.path.splitext(uploaded_file.name)[1].lower()
if file_extension == '.parquet':
system_message += f"\n\n파일 λ‚΄μš©:\n```markdown\n{content}\n```"
else:
system_message += f"\n\n파일 λ‚΄μš©:\n```python\n{content}\n```"
if message == "파일 뢄석을 μ‹œμž‘ν•©λ‹ˆλ‹€.":
message = """μ—…λ‘œλ“œλœ νŒŒμΌμ„ λΆ„μ„ν•˜μ—¬ λ‹€μŒ λ‚΄μš©μ„ ν¬ν•¨ν•˜μ—¬ μƒμ„Ένžˆ μ„€λͺ…ν•˜λΌ:
1. 파일의 μ£Όμš” λͺ©μ κ³Ό κΈ°λŠ₯
2. μ£Όμš” νŠΉμ§•κ³Ό κ΅¬μ„±μš”μ†Œ
3. ν™œμš© 방법 및 μ‚¬μš© μ‹œλ‚˜λ¦¬μ˜€
4. μ£Όμ˜μ‚¬ν•­ 및 μ œν•œμ‚¬ν•­
5. κΈ°λŒ€νš¨κ³Ό 및 μž₯점"""
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
messages.extend(format_history(history))
messages.append({"role": "user", "content": message})
response = ""
try:
client = get_client(model_name)
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:
response += token
history = history + [[message, response]]
return "", history
except Exception as e:
error_msg = f"μΆ”λ‘  쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
history = history + [[message, error_msg]]
return "", history
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.Dropdown(
choices=list(LLM_MODELS.keys()),
value="Default",
label="LLM λͺ¨λΈ 선택",
info="μ‚¬μš©ν•  LLM λͺ¨λΈμ„ μ„ νƒν•˜μ„Έμš”"
)
file_upload = gr.File(
label="파일 μ—…λ‘œλ“œ (.csv, .txt, .py, .parquet)",
file_types=[".csv", ".txt", ".py", ".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]
)
# 파일 μ—…λ‘œλ“œ μ‹œ μžλ™ 뢄석
file_upload.change(
chat,
inputs=[gr.Textbox(value="파일 뢄석을 μ‹œμž‘ν•©λ‹ˆλ‹€."), chatbot, file_upload, model_name, system_message, max_tokens, temperature, top_p],
outputs=[msg, chatbot]
)
# 예제 μΆ”κ°€
gr.Examples(
examples=[
["μƒμ„Έν•œ μ‚¬μš© 방법을 마치 화면을 λ³΄λ©΄μ„œ μ„€λͺ…ν•˜λ“―이 4000 토큰 이상 μžμ„Ένžˆ μ„€λͺ…ν•˜λΌ"],
["FAQ 20건을 μƒμ„Έν•˜κ²Œ μž‘μ„±ν•˜λΌ. 4000토큰 이상 μ‚¬μš©ν•˜λΌ."],
["μ‚¬μš© 방법과 차별점, νŠΉμ§•, 강점을 μ€‘μ‹¬μœΌλ‘œ 4000 토큰 이상 유튜브 μ˜μƒ 슀크립트 ν˜•νƒœλ‘œ μž‘μ„±ν•˜λΌ"],
["λ³Έ μ„œλΉ„μŠ€λ₯Ό SEO μ΅œμ ν™”ν•˜μ—¬ λΈ”λ‘œκ·Έ 포슀트둜 4000 토큰 이상 μž‘μ„±ν•˜λΌ"],
["νŠΉν—ˆ μΆœμ›μ— ν™œμš©ν•  기술 및 λΉ„μ¦ˆλ‹ˆμŠ€λͺ¨λΈ 츑면을 ν¬ν•¨ν•˜μ—¬ νŠΉν—ˆ μΆœμ›μ„œ ꡬ성에 맞게 μž‘μ„±ν•˜λΌ"],
["계속 μ΄μ–΄μ„œ λ‹΅λ³€ν•˜λΌ"],
],
inputs=msg,
)
if __name__ == "__main__":
demo.launch()