DrishtiSharma commited on
Commit
48a23ed
·
verified ·
1 Parent(s): 329ce68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -13
app.py CHANGED
@@ -3,9 +3,9 @@ import pandas as pd
3
  import os
4
  from pandasai import SmartDataframe
5
  from pandasai.llm import OpenAI
6
- from dotenv import load_dotenv
7
  import tempfile
8
  import matplotlib.pyplot as plt
 
9
 
10
  # Load environment variables
11
  openai_api_key = os.getenv("OPENAI_API_KEY")
@@ -18,30 +18,83 @@ if not openai_api_key:
18
  # Initialize the LLM
19
  llm = OpenAI(api_token=openai_api_key)
20
 
21
- st.title("Chat with CSV File Using PandasAI")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # Instructions
24
  with st.sidebar:
25
- st.header("Instructions")
26
  st.markdown(
27
- "1. Upload a CSV file to get started.\n"
28
- "2. Enter a question to interact with the data.\n"
29
- "3. Enter a query to generate and view graphs.\n"
 
 
 
 
30
  )
31
 
32
- uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
 
33
 
34
- if uploaded_file:
35
- # Read the CSV file
36
- df = pd.read_csv(uploaded_file)
37
  st.write("### Data Preview")
38
  st.dataframe(df.head(10))
39
 
40
  # Create SmartDataFrame
41
  chat_df = SmartDataframe(df, config={"llm": llm})
42
 
43
- st.write("### Chat with Your Data")
44
- user_query = st.text_input("Enter your question about the data:")
45
 
46
  if user_query:
47
  try:
@@ -51,7 +104,7 @@ if uploaded_file:
51
  st.error(f"Error: {e}")
52
 
53
  st.write("### Generate and View Graphs")
54
- plot_query = st.text_input("Enter a query to generate a graph (e.g., 'Can you plot me a bar graph of total weekly sales for each store?'):")
55
 
56
  if plot_query:
57
  try:
 
3
  import os
4
  from pandasai import SmartDataframe
5
  from pandasai.llm import OpenAI
 
6
  import tempfile
7
  import matplotlib.pyplot as plt
8
+ from datasets import load_dataset
9
 
10
  # Load environment variables
11
  openai_api_key = os.getenv("OPENAI_API_KEY")
 
18
  # Initialize the LLM
19
  llm = OpenAI(api_token=openai_api_key)
20
 
21
+ def validate_and_clean_dataset(dataframe):
22
+ # Placeholder for dataset validation and cleaning logic
23
+ return dataframe
24
+
25
+ def load_dataset_into_session():
26
+ input_option = st.radio(
27
+ "Select Dataset Input:",
28
+ ["Use Repo Directory Dataset", "Use Hugging Face Dataset", "Upload CSV File"],
29
+ )
30
+
31
+ # Option 1: Load dataset from the repo directory
32
+ if input_option == "Use Repo Directory Dataset":
33
+ file_path = "./source/test.csv"
34
+ if st.button("Load Dataset"):
35
+ try:
36
+ st.session_state.df = pd.read_csv(file_path)
37
+ st.session_state.df = validate_and_clean_dataset(st.session_state.df)
38
+ st.success(f"File loaded successfully from '{file_path}'!")
39
+ except Exception as e:
40
+ st.error(f"Error loading dataset from the repo directory: {e}")
41
+
42
+ # Option 2: Load dataset from Hugging Face
43
+ elif input_option == "Use Hugging Face Dataset":
44
+ dataset_name = st.text_input(
45
+ "Enter Hugging Face Dataset Name:", value="HUPD/hupd"
46
+ )
47
+ if st.button("Load Hugging Face Dataset"):
48
+ try:
49
+ dataset = load_dataset(dataset_name, split="train", trust_remote_code=True)
50
+ if hasattr(dataset, "to_pandas"):
51
+ st.session_state.df = dataset.to_pandas()
52
+ else:
53
+ st.session_state.df = pd.DataFrame(dataset)
54
+ st.session_state.df = validate_and_clean_dataset(st.session_state.df)
55
+ st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
56
+ except Exception as e:
57
+ st.error(f"Error loading Hugging Face dataset: {e}")
58
+
59
+ # Option 3: Upload CSV File
60
+ elif input_option == "Upload CSV File":
61
+ uploaded_file = st.file_uploader("Upload a CSV File:", type=["csv"])
62
+ if uploaded_file:
63
+ try:
64
+ st.session_state.df = pd.read_csv(uploaded_file)
65
+ st.session_state.df = validate_and_clean_dataset(st.session_state.df)
66
+ st.success("File uploaded successfully!")
67
+ except Exception as e:
68
+ st.error(f"Error reading uploaded file: {e}")
69
+
70
+ st.title("Chat with Patent Dataset Using PandasAI")
71
 
72
  # Instructions
73
  with st.sidebar:
74
+ st.header("Instructions:")
75
  st.markdown(
76
+ "1. Select how you want to input the dataset.\n"
77
+ "2. Upload, select, or fetch the dataset using the provided options.\n"
78
+ "3. Enter a question to interact with the patent data.\n"
79
+ " - Example: 'Predict if the patent will be accepted.'\n"
80
+ " - Example: 'What is the primary classification of this patent?'\n"
81
+ " - Example: 'Summarize the abstract of this patent.'\n"
82
+ "4. Enter a query to generate and view graphs based on patent attributes.\n"
83
  )
84
 
85
+ # Load dataset into session
86
+ load_dataset_into_session()
87
 
88
+ if "df" in st.session_state:
89
+ df = st.session_state.df
 
90
  st.write("### Data Preview")
91
  st.dataframe(df.head(10))
92
 
93
  # Create SmartDataFrame
94
  chat_df = SmartDataframe(df, config={"llm": llm})
95
 
96
+ st.write("### Chat with Your Patent Data")
97
+ user_query = st.text_input("Enter your question about the patent data (e.g., 'Predict if the patent will be accepted.'):")
98
 
99
  if user_query:
100
  try:
 
104
  st.error(f"Error: {e}")
105
 
106
  st.write("### Generate and View Graphs")
107
+ plot_query = st.text_input("Enter a query to generate a graph (e.g., 'Plot the number of patents by filing year.'):")
108
 
109
  if plot_query:
110
  try: