Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import plotly.express as px
|
| 4 |
import os
|
| 5 |
import io
|
| 6 |
|
|
@@ -10,11 +9,11 @@ from langchain.prompts import PromptTemplate
|
|
| 10 |
from langchain.agents.agent_types import AgentType
|
| 11 |
from langchain_experimental.agents import create_pandas_dataframe_agent
|
| 12 |
|
| 13 |
-
# Load API key
|
| 14 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 15 |
llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)
|
| 16 |
|
| 17 |
-
# Global
|
| 18 |
dataset_dict = {}
|
| 19 |
agent_dict = {}
|
| 20 |
|
|
@@ -76,8 +75,9 @@ Value counts: {value_counts}
|
|
| 76 |
"value_counts": value_counts_str
|
| 77 |
})
|
| 78 |
|
| 79 |
-
def
|
| 80 |
df = dataset_dict[dataset_name]
|
|
|
|
| 81 |
if dataset_name not in agent_dict:
|
| 82 |
agent_dict[dataset_name] = create_pandas_dataframe_agent(
|
| 83 |
llm=llm,
|
|
@@ -87,8 +87,9 @@ def chat_with_csv(dataset_name, query):
|
|
| 87 |
handle_parsing_errors=True,
|
| 88 |
allow_dangerous_code=True
|
| 89 |
)
|
|
|
|
| 90 |
agent = agent_dict[dataset_name]
|
| 91 |
-
raw_output = agent.run(
|
| 92 |
|
| 93 |
refine_prompt = PromptTemplate(
|
| 94 |
input_variables=["question", "raw_output"],
|
|
@@ -105,22 +106,18 @@ Final response format:
|
|
| 105 |
- Markdown-friendly format
|
| 106 |
"""
|
| 107 |
)
|
|
|
|
| 108 |
refine_chain = LLMChain(llm=llm, prompt=refine_prompt)
|
| 109 |
-
|
| 110 |
-
"question":
|
| 111 |
"raw_output": raw_output
|
| 112 |
})
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
fig = px.scatter(
|
| 117 |
-
df, x=x_col, y=y_col,
|
| 118 |
-
color=color_col if color_col and color_col != "None" else None
|
| 119 |
-
)
|
| 120 |
-
return fig
|
| 121 |
|
| 122 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 123 |
-
# π Gradio
|
| 124 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 125 |
|
| 126 |
with gr.Blocks(title="CSV Chat Assistant") as app:
|
|
@@ -131,19 +128,18 @@ with gr.Blocks(title="CSV Chat Assistant") as app:
|
|
| 131 |
upload_output = gr.Textbox(label="Upload Status")
|
| 132 |
|
| 133 |
dataset_radio = gr.Radio(choices=[], label="Select Dataset", interactive=True)
|
| 134 |
-
|
| 135 |
-
# β
Ensures radio options are updated properly
|
| 136 |
file_upload.change(fn=upload_csv, inputs=[file_upload], outputs=[upload_output, dataset_radio])
|
| 137 |
|
| 138 |
-
with gr.
|
| 139 |
-
summary_button = gr.Button("Generate Summary")
|
| 140 |
summary_output = gr.Markdown()
|
| 141 |
summary_button.click(fn=generate_summary, inputs=dataset_radio, outputs=summary_output)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import os
|
| 4 |
import io
|
| 5 |
|
|
|
|
| 9 |
from langchain.agents.agent_types import AgentType
|
| 10 |
from langchain_experimental.agents import create_pandas_dataframe_agent
|
| 11 |
|
| 12 |
+
# Load API key
|
| 13 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 14 |
llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)
|
| 15 |
|
| 16 |
+
# Global data stores
|
| 17 |
dataset_dict = {}
|
| 18 |
agent_dict = {}
|
| 19 |
|
|
|
|
| 75 |
"value_counts": value_counts_str
|
| 76 |
})
|
| 77 |
|
| 78 |
+
def ask_csv(dataset_name, user_input, history):
|
| 79 |
df = dataset_dict[dataset_name]
|
| 80 |
+
|
| 81 |
if dataset_name not in agent_dict:
|
| 82 |
agent_dict[dataset_name] = create_pandas_dataframe_agent(
|
| 83 |
llm=llm,
|
|
|
|
| 87 |
handle_parsing_errors=True,
|
| 88 |
allow_dangerous_code=True
|
| 89 |
)
|
| 90 |
+
|
| 91 |
agent = agent_dict[dataset_name]
|
| 92 |
+
raw_output = agent.run(user_input)
|
| 93 |
|
| 94 |
refine_prompt = PromptTemplate(
|
| 95 |
input_variables=["question", "raw_output"],
|
|
|
|
| 106 |
- Markdown-friendly format
|
| 107 |
"""
|
| 108 |
)
|
| 109 |
+
|
| 110 |
refine_chain = LLMChain(llm=llm, prompt=refine_prompt)
|
| 111 |
+
final_response = refine_chain.run({
|
| 112 |
+
"question": user_input,
|
| 113 |
"raw_output": raw_output
|
| 114 |
})
|
| 115 |
|
| 116 |
+
history.append((user_input, final_response))
|
| 117 |
+
return history, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 120 |
+
# π Gradio App
|
| 121 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 122 |
|
| 123 |
with gr.Blocks(title="CSV Chat Assistant") as app:
|
|
|
|
| 128 |
upload_output = gr.Textbox(label="Upload Status")
|
| 129 |
|
| 130 |
dataset_radio = gr.Radio(choices=[], label="Select Dataset", interactive=True)
|
|
|
|
|
|
|
| 131 |
file_upload.change(fn=upload_csv, inputs=[file_upload], outputs=[upload_output, dataset_radio])
|
| 132 |
|
| 133 |
+
with gr.Row():
|
| 134 |
+
summary_button = gr.Button("π§Ύ Generate Summary")
|
| 135 |
summary_output = gr.Markdown()
|
| 136 |
summary_button.click(fn=generate_summary, inputs=dataset_radio, outputs=summary_output)
|
| 137 |
|
| 138 |
+
chatbot = gr.Chatbot(label="CSV Chat", height=400)
|
| 139 |
+
msg = gr.Textbox(label="Type your question")
|
| 140 |
+
clear = gr.Button("Clear Chat")
|
| 141 |
+
|
| 142 |
+
msg.submit(fn=ask_csv, inputs=[dataset_radio, msg, chatbot], outputs=[chatbot, chatbot])
|
| 143 |
+
clear.click(lambda: [], None, chatbot)
|
| 144 |
|
| 145 |
app.launch()
|