Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain_experimental.agents import create_pandas_dataframe_agent
|
| 5 |
+
from langchain.agents.agent_types import AgentType
|
| 6 |
+
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# OpenAI API ํค ์ค์ (ํ๊ฒฝ ๋ณ์ ์ฌ์ฉ ๊ถ์ฅ)
|
| 10 |
+
# Hugging Face Spaces์ ๋ฐฐํฌ ์ Secrets์ OPENAI_API_KEY๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.
|
| 11 |
+
# ๋ก์ปฌ์์ ์คํ ์ .env ํ์ผ์ ์ฌ์ฉํ๊ฑฐ๋ ์ง์ ์ค์ ํด์ผ ํฉ๋๋ค.
|
| 12 |
+
# ์์: os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
|
| 13 |
+
|
| 14 |
+
# CSV ํ์ผ ๋ก๋
|
| 15 |
+
CSV_FILE_PATH = "2.csv"
|
| 16 |
+
try:
|
| 17 |
+
df = pd.read_csv("https://github.com/kairess/toy-datasets/raw/master/titanic.csv")
|
| 18 |
+
except FileNotFoundError:
|
| 19 |
+
print(f"Error: '{CSV_FILE_PATH}' ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค. ํ์ผ ๊ฒฝ๋ก๋ฅผ ํ์ธํด์ฃผ์ธ์.")
|
| 20 |
+
# Gradio ์ธํฐํ์ด์ค๋ฅผ ์คํํ๊ธฐ ์ ์ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ์ฌ์ฉ์์๊ฒ ํ์ํ๋ ๋ฐฉ๋ฒ ๋ฑ์ ๊ณ ๋ คํด์ผ ํฉ๋๋ค.
|
| 21 |
+
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ๊ฒ ์ข
๋ฃํ๊ฑฐ๋ ๋น DataFrame์ ์ฌ์ฉํ๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.
|
| 22 |
+
df = pd.DataFrame() # ์ค๋ฅ ๋ฐ์ ์ ๋น DataFrame์ผ๋ก ์งํ
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"CSV ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 25 |
+
df = pd.DataFrame()
|
| 26 |
+
|
| 27 |
+
# LLM ๋ชจ๋ธ ์ค์
|
| 28 |
+
# gpt-4o-mini ๋ชจ๋ธ ์ฌ์ฉ
|
| 29 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 30 |
+
|
| 31 |
+
# ๋ํ ๊ธฐ๋ก์ ์ํ Memory ์ค์
|
| 32 |
+
# ์ฑ๋ด์ด ์ด์ ๋ํ๋ฅผ ๊ธฐ์ตํ ์ ์๋๋ก ํฉ๋๋ค.
|
| 33 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 34 |
+
|
| 35 |
+
# Pandas DataFrame Agent ์์ฑ
|
| 36 |
+
# allow_dangerous_code=True ์ค์ ์ ๋ณด์์ ์ ์ํด์ผ ํฉ๋๋ค.
|
| 37 |
+
# ๋ฉ๋ชจ๋ฆฌ๋ฅผ Agent์ ์ฐ๊ฒฐํ์ฌ ๋ํ ๊ธฐ๋ก์ ํ์ฉํฉ๋๋ค.
|
| 38 |
+
if not df.empty:
|
| 39 |
+
agent = create_pandas_dataframe_agent(
|
| 40 |
+
llm,
|
| 41 |
+
df,
|
| 42 |
+
verbose=True,
|
| 43 |
+
agent_type=AgentType.OPENAI_FUNCTIONS, # ๋๋ AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
| 44 |
+
allow_dangerous_code=True,
|
| 45 |
+
memory=memory # ๋ฉ๋ชจ๋ฆฌ ์ฐ๊ฒฐ
|
| 46 |
+
)
|
| 47 |
+
else:
|
| 48 |
+
agent = None # DataFrame ๋ก๋ ์คํจ ์ Agent ์์ฑ ์ ํจ
|
| 49 |
+
|
| 50 |
+
# ์ฑ๋ด ์์ธก ํจ์
|
| 51 |
+
def predict(message, history):
|
| 52 |
+
# Gradio history ํ์์ LangChain history ํ์์ผ๋ก ๋ณํ
|
| 53 |
+
# ConversationBufferMemory๋ฅผ ์ฌ์ฉํ๋ฉด ์ด ๋ณํ ๊ณผ์ ์ด Agent ๋ด๋ถ์์ ์ฒ๋ฆฌ๋ฉ๋๋ค.
|
| 54 |
+
# ๋ฐ๋ผ์ ์ฌ๊ธฐ์๋ ์ฌ์ฉ์ ๋ฉ์์ง๋ง Agent์ ์ ๋ฌํฉ๋๋ค.
|
| 55 |
+
|
| 56 |
+
if agent is None:
|
| 57 |
+
return "CSV ํ์ผ์ ๋ก๋ํ ์ ์์ด ์ฑ๋ด์ด ์๋ํ์ง ์์ต๋๋ค."
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# Agent ์คํ. run ๋์ invoke๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ ์ฐํ ์ ์์ต๋๋ค.
|
| 61 |
+
# invoke๋ ๋์
๋๋ฆฌ ํํ์ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋ค๋ฃน๋๋ค.
|
| 62 |
+
response = agent.invoke({"input": message})
|
| 63 |
+
# invoke ๊ฒฐ๊ณผ์์ ์๋ต ํ
์คํธ ์ถ์ถ
|
| 64 |
+
gpt_response = response.get('output', '์๋ต ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.')
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"์์ด์ ํธ ์คํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 67 |
+
gpt_response = f"์ฃ์กํฉ๋๋ค. ์์ฒญ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 68 |
+
|
| 69 |
+
# Gradio history ํ์์ ๋ง์ถฐ ์๋ต ๋ฐํ
|
| 70 |
+
# history๋ [[user_msg, bot_msg], [user_msg, bot_msg], ...] ํํ์
๋๋ค.
|
| 71 |
+
# Gradio ChatInterface๋ ์๋์ผ๋ก ์ด์ ๋ํ๋ฅผ ๊ด๋ฆฌํ๋ฏ๋ก,
|
| 72 |
+
# ์ฌ๊ธฐ์๋ ํ์ฌ ์ฌ์ฉ์ ๋ฉ์์ง์ Agent์ ์๋ต๋ง ๋ฐํํ๋ฉด ๋ฉ๋๋ค.
|
| 73 |
+
# ChatInterface์ predict ํจ์๋ (message, history)๋ฅผ ์ธ์๋ก ๋ฐ๊ณ
|
| 74 |
+
# ์
๋ฐ์ดํธ๋ history๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
|
| 75 |
+
# Gradio 4.0๋ถํฐ๋ history ์ธ์๋ฅผ dictionary ํํ๋ก ๋ฐ์ต๋๋ค.
|
| 76 |
+
# history๋ {"user": [...], "assistant": [...]} ํํ์ผ ์ ์์ต๋๋ค.
|
| 77 |
+
|
| 78 |
+
# ChatInterface์ 'messages' type์ ๋ง์ถฐ history๋ฅผ ์
๋ฐ์ดํธํ์ฌ ๋ฐํ
|
| 79 |
+
# history๋ [['user_message', 'bot_message'], ...] ํํ์
๋๋ค.
|
| 80 |
+
# Gradio ChatInterface๋ ์ด ํ์์ ๊ธฐ๋ํฉ๋๋ค.
|
| 81 |
+
return gpt_response
|
| 82 |
+
|
| 83 |
+
# Gradio ChatInterface ์ค์
|
| 84 |
+
# ChatInterface๋ history ๊ด๋ฆฌ๋ฅผ ์๋์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค.
|
| 85 |
+
# predict ํจ์๋ ํ์ฌ ๋ฉ์์ง์ ๋ํ ์๋ต๋ง ๋ฐํํ๋๋ก ๋จ์ํํฉ๋๋ค.
|
| 86 |
+
demo = gr.ChatInterface(
|
| 87 |
+
predict,
|
| 88 |
+
# type="messages", # ChatInterface๋ ๊ธฐ๋ณธ์ ์ผ๋ก "messages" ํ์
|
| 89 |
+
title="CSV ํ์ผ ๊ธฐ๋ฐ ์ฑ๋ด",
|
| 90 |
+
description=f"{CSV_FILE_PATH} ํ์ผ์ ๋ํ ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค.",
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Gradio ์ฑ ์คํ
|
| 94 |
+
# share=True ์ค์ ์ ์ธ๋ถ์์ ์ ๊ทผ ๊ฐ๋ฅํ ์์ ๋งํฌ๊ฐ ์์ฑ๋ฉ๋๋ค (๋๋ฒ๊น
๋ชฉ์ ).
|
| 95 |
+
# Hugging Face Spaces์ ๋ฐฐํฌ ์ share=False๋ก ์ค์ ํด์ผ ํฉ๋๋ค.
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|