Noveramaaz commited on
Commit
41120f1
1 Parent(s): 13d5777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -3,7 +3,6 @@ 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
- from langchain_community.chat_models import ChatOpenAI
7
  import pandas as pd
8
  import matplotlib.pyplot as plt
9
  from io import BytesIO
@@ -22,14 +21,15 @@ def create_csv_agent_instance(llm, file_path):
22
  return agent
23
 
24
  # Define the function to perform QA and optionally plot graphs
25
- def qa_app(csv_file, question, plot=False):
26
  try:
27
  df = pd.read_csv(csv_file.name)
28
- llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k", openai_api_key='sk-proj-bFQSTYKhv8H4NqKfMFuaT3BlbkFJecIT52917IZGzTu8bhia')
29
  agent = create_csv_agent_instance(llm, csv_file.name)
30
  response = agent.run(question)
31
 
32
- if plot:
 
33
  graph_output = plot_data(df)
34
  else:
35
  graph_output = None
@@ -59,16 +59,16 @@ demo = gr.Interface(
59
  fn=qa_app,
60
  inputs=[
61
  gr.File(label="Upload CSV file"),
62
- gr.Textbox(label="Question"),
63
- gr.Checkbox(label="Plot Graphs", value=False)
64
  ],
65
  outputs=[
66
  gr.Textbox(label="Answer"),
67
- gr.Image(label="Generated Plot", type="pil"),
68
  ],
69
  title="Data Analysis Chatbot",
70
- description="Upload a CSV file, ask a question about the data, and optionally plot graphs."
71
  )
72
 
73
  # Launch the Gradio app with debugging enabled to trace any runtime issues
74
  demo.launch(debug=True)
 
 
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
 
21
  return agent
22
 
23
  # Define the function to perform QA and optionally plot graphs
24
+ def qa_app(csv_file, question):
25
  try:
26
  df = pd.read_csv(csv_file.name)
27
+ llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k", openai_api_key=key) # ensure key is properly managed
28
  agent = create_csv_agent_instance(llm, csv_file.name)
29
  response = agent.run(question)
30
 
31
+ # Check if the user's question implies a request for a plot
32
+ if "plot" in question.lower() or "graph" in question.lower():
33
  graph_output = plot_data(df)
34
  else:
35
  graph_output = None
 
59
  fn=qa_app,
60
  inputs=[
61
  gr.File(label="Upload CSV file"),
62
+ gr.Textbox(label="Question")
 
63
  ],
64
  outputs=[
65
  gr.Textbox(label="Answer"),
66
+ gr.Image(label="Generated Plot", type="pil", optional=True), # Mark the plot as optional
67
  ],
68
  title="Data Analysis Chatbot",
69
+ description="Upload a CSV file, ask a question about the data. Include 'plot' or 'graph' in your question to generate graphs."
70
  )
71
 
72
  # Launch the Gradio app with debugging enabled to trace any runtime issues
73
  demo.launch(debug=True)
74
+