Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain.llms import OpenAI
|
| 3 |
+
from langchain.chat_models import ChatOpenAI
|
| 4 |
+
from langchain.agents.agent_types import AgentType
|
| 5 |
+
from langchain_experimental.agents.agent_toolkits import create_csv_agent
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
# Define a function to create the CSV agent
|
| 13 |
+
def create_csv_agent_instance(llm, file_path):
|
| 14 |
+
# Set up the agent
|
| 15 |
+
agent = create_csv_agent(
|
| 16 |
+
llm,
|
| 17 |
+
file_path,
|
| 18 |
+
verbose=True,
|
| 19 |
+
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 20 |
+
)
|
| 21 |
+
return agent
|
| 22 |
+
|
| 23 |
+
# Define the function to perform QA and optionally plot graphs
|
| 24 |
+
def qa_app(csv_file, question, plot=False):
|
| 25 |
+
try:
|
| 26 |
+
df = pd.read_csv(csv_file.name)
|
| 27 |
+
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k", openai_api_key='sk-proj-bFQSTYKhv8H4NqKfMFuaT3BlbkFJecIT52917IZGzTu8bhia')
|
| 28 |
+
agent = create_csv_agent_instance(llm, csv_file.name)
|
| 29 |
+
response = agent.run(question)
|
| 30 |
+
|
| 31 |
+
if plot:
|
| 32 |
+
graph_output = plot_data(df)
|
| 33 |
+
else:
|
| 34 |
+
graph_output = None
|
| 35 |
+
|
| 36 |
+
return response, graph_output
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error in processing: {str(e)}", None
|
| 39 |
+
|
| 40 |
+
def plot_data(df):
|
| 41 |
+
plt.figure(figsize=(10, 5))
|
| 42 |
+
if np.issubdtype(df[df.columns[0]].dtype, np.number) and np.issubdtype(df[df.columns[1]].dtype, np.number):
|
| 43 |
+
plt.scatter(df[df.columns[0]], df[df.columns[1]]) # Use scatter for numeric vs numeric
|
| 44 |
+
else:
|
| 45 |
+
df[df.columns[0]].value_counts().plot(kind='bar') # Example for categorical data
|
| 46 |
+
plt.title('Data Distribution')
|
| 47 |
+
plt.xlabel(df.columns[0])
|
| 48 |
+
plt.ylabel(df.columns[1])
|
| 49 |
+
plt.grid(True)
|
| 50 |
+
buf = BytesIO()
|
| 51 |
+
plt.savefig(buf, format='png')
|
| 52 |
+
plt.close()
|
| 53 |
+
buf.seek(0)
|
| 54 |
+
return Image.open(buf)
|
| 55 |
+
|
| 56 |
+
# Set up the Gradio interface
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=qa_app,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.File(label="Upload CSV file"),
|
| 61 |
+
gr.Textbox(label="Question"),
|
| 62 |
+
gr.Checkbox(label="Plot Graphs", value=False)
|
| 63 |
+
],
|
| 64 |
+
outputs=[
|
| 65 |
+
gr.Textbox(label="Answer"),
|
| 66 |
+
gr.Image(label="Generated Plot", type="pil"),
|
| 67 |
+
],
|
| 68 |
+
title="Data Analysis Chatbot",
|
| 69 |
+
description="Upload a CSV file, ask a question about the data, and optionally plot graphs."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch the Gradio app with debugging enabled to trace any runtime issues
|
| 73 |
+
demo.launch(debug=True)
|