Update app.py
Browse files
app.py
CHANGED
@@ -6,22 +6,15 @@ 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")
|
|
|
12 |
|
13 |
-
#
|
14 |
-
if not openai_api_key:
|
15 |
-
st.error("OpenAI API key is not set. Please add it to a .env file.")
|
16 |
-
st.stop()
|
17 |
-
|
18 |
-
# Initialize the LLM
|
19 |
-
#llm = OpenAI(api_token=openai_api_key)
|
20 |
-
|
21 |
def initialize_llm(model_choice):
|
22 |
-
groq_api_key = os.getenv("GROQ_API_KEY")
|
23 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
24 |
-
|
25 |
if model_choice == "llama-3.3-70b":
|
26 |
if not groq_api_key:
|
27 |
st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
|
@@ -33,9 +26,9 @@ def initialize_llm(model_choice):
|
|
33 |
return None
|
34 |
return ChatOpenAI(api_key=openai_api_key, model="gpt-4o")
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
def load_dataset_into_session():
|
41 |
input_option = st.radio(
|
@@ -49,6 +42,7 @@ def load_dataset_into_session():
|
|
49 |
if st.button("Load Dataset"):
|
50 |
try:
|
51 |
st.session_state.df = pd.read_csv(file_path)
|
|
|
52 |
st.success(f"File loaded successfully from '{file_path}'!")
|
53 |
except Exception as e:
|
54 |
st.error(f"Error loading dataset from the repo directory: {e}")
|
@@ -83,23 +77,27 @@ def load_dataset_into_session():
|
|
83 |
|
84 |
st.title("Chat with Patent Dataset Using PandasAI")
|
85 |
|
86 |
-
#
|
87 |
with st.sidebar:
|
88 |
-
st.header("Instructions
|
89 |
st.markdown(
|
90 |
"1. Select how you want to input the dataset.\n"
|
91 |
"2. Upload, select, or fetch the dataset using the provided options.\n"
|
92 |
-
"3.
|
93 |
" - Example: 'Predict if the patent will be accepted.'\n"
|
94 |
" - Example: 'What is the primary classification of this patent?'\n"
|
95 |
" - Example: 'Summarize the abstract of this patent.'\n"
|
96 |
"4. Enter a query to generate and view graphs based on patent attributes.\n"
|
97 |
)
|
98 |
|
|
|
|
|
|
|
|
|
99 |
# Load dataset into session
|
100 |
load_dataset_into_session()
|
101 |
|
102 |
-
if "df" in st.session_state:
|
103 |
df = st.session_state.df
|
104 |
st.write("### Data Preview")
|
105 |
st.dataframe(df.head(10))
|
|
|
6 |
import tempfile
|
7 |
import matplotlib.pyplot as plt
|
8 |
from datasets import load_dataset
|
9 |
+
from langchain_groq import ChatGroq
|
10 |
+
from langchain_openai import ChatOpenAI
|
11 |
|
12 |
# Load environment variables
|
13 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
14 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
15 |
|
16 |
+
# Initialize the LLM based on user selection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def initialize_llm(model_choice):
|
|
|
|
|
|
|
18 |
if model_choice == "llama-3.3-70b":
|
19 |
if not groq_api_key:
|
20 |
st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
|
|
|
26 |
return None
|
27 |
return ChatOpenAI(api_key=openai_api_key, model="gpt-4o")
|
28 |
|
29 |
+
def validate_and_clean_dataset(dataframe):
|
30 |
+
# Placeholder for dataset validation and cleaning logic
|
31 |
+
return dataframe
|
32 |
|
33 |
def load_dataset_into_session():
|
34 |
input_option = st.radio(
|
|
|
42 |
if st.button("Load Dataset"):
|
43 |
try:
|
44 |
st.session_state.df = pd.read_csv(file_path)
|
45 |
+
st.session_state.df = validate_and_clean_dataset(st.session_state.df)
|
46 |
st.success(f"File loaded successfully from '{file_path}'!")
|
47 |
except Exception as e:
|
48 |
st.error(f"Error loading dataset from the repo directory: {e}")
|
|
|
77 |
|
78 |
st.title("Chat with Patent Dataset Using PandasAI")
|
79 |
|
80 |
+
# Push instructions to the sidebar
|
81 |
with st.sidebar:
|
82 |
+
st.header("Instructions")
|
83 |
st.markdown(
|
84 |
"1. Select how you want to input the dataset.\n"
|
85 |
"2. Upload, select, or fetch the dataset using the provided options.\n"
|
86 |
+
"3. Choose an LLM (Groq-based or OpenAI-based) to interact with the data.\n"
|
87 |
" - Example: 'Predict if the patent will be accepted.'\n"
|
88 |
" - Example: 'What is the primary classification of this patent?'\n"
|
89 |
" - Example: 'Summarize the abstract of this patent.'\n"
|
90 |
"4. Enter a query to generate and view graphs based on patent attributes.\n"
|
91 |
)
|
92 |
|
93 |
+
# Select LLM model
|
94 |
+
model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b"], index=0, horizontal=True)
|
95 |
+
llm = initialize_llm(model_choice)
|
96 |
+
|
97 |
# Load dataset into session
|
98 |
load_dataset_into_session()
|
99 |
|
100 |
+
if "df" in st.session_state and llm:
|
101 |
df = st.session_state.df
|
102 |
st.write("### Data Preview")
|
103 |
st.dataframe(df.head(10))
|