Update app1.py
Browse files
app1.py
CHANGED
|
@@ -1,21 +1,29 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from pandasai import SmartDataframe
|
| 4 |
-
from pandasai.llm import OpenAI
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from datasets import load_dataset
|
| 7 |
import os
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
| 11 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# App title and description
|
| 21 |
st.title("Patent Analytics: Chat With Your Dataset")
|
|
@@ -31,31 +39,36 @@ st.markdown(
|
|
| 31 |
if "df" not in st.session_state:
|
| 32 |
st.session_state.df = None
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Show the loaded dataframe preview
|
| 61 |
if st.session_state.df is not None:
|
|
@@ -77,14 +90,14 @@ if st.session_state.df is not None:
|
|
| 77 |
# Chat with the dataframe
|
| 78 |
response = chat_df.chat(question)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
if
|
| 82 |
-
st.write("###
|
|
|
|
| 83 |
else:
|
| 84 |
st.write("### Response")
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
st.write(response)
|
| 88 |
st.success("Request processed successfully!")
|
| 89 |
except Exception as e:
|
| 90 |
st.error(f"An error occurred: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from pandasai import SmartDataframe
|
| 4 |
+
from pandasai.llm import OpenAI, ChatGroq
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from datasets import load_dataset
|
| 7 |
import os
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
|
|
|
| 11 |
|
| 12 |
+
def initialize_llm(model_choice):
|
| 13 |
+
"""Initialize the chosen LLM based on the user's selection."""
|
| 14 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 15 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 16 |
|
| 17 |
+
if model_choice == "llama-3.3-70b":
|
| 18 |
+
if not groq_api_key:
|
| 19 |
+
st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
|
| 20 |
+
return None
|
| 21 |
+
return ChatGroq(groq_api_key=groq_api_key, model="groq/llama-3.3-70b-versatile")
|
| 22 |
+
elif model_choice == "GPT-4o":
|
| 23 |
+
if not openai_api_key:
|
| 24 |
+
st.error("OpenAI API key is missing. Please set the OPENAI_API_KEY environment variable.")
|
| 25 |
+
return None
|
| 26 |
+
return OpenAI(api_token=openai_api_key)
|
| 27 |
|
| 28 |
# App title and description
|
| 29 |
st.title("Patent Analytics: Chat With Your Dataset")
|
|
|
|
| 39 |
if "df" not in st.session_state:
|
| 40 |
st.session_state.df = None
|
| 41 |
|
| 42 |
+
# Select the model
|
| 43 |
+
model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b"], index=0, horizontal=True)
|
| 44 |
+
llm = initialize_llm(model_choice)
|
| 45 |
+
if not llm:
|
| 46 |
+
st.stop()
|
|
|
|
| 47 |
|
| 48 |
+
def load_dataset_into_session():
|
| 49 |
+
"""Load dataset based on user input."""
|
| 50 |
+
input_option = st.radio("Choose Dataset Input Method", ["Use Hugging Face Dataset", "Upload CSV File"], index=0)
|
| 51 |
+
|
| 52 |
+
if input_option == "Use Hugging Face Dataset":
|
| 53 |
+
dataset_name = st.text_input("Enter Hugging Face Dataset Name:", value="HUPD/hupd")
|
| 54 |
+
if st.button("Load Dataset"):
|
| 55 |
+
try:
|
| 56 |
+
dataset = load_dataset(dataset_name, split="train", trust_remote_code=True)
|
| 57 |
+
st.session_state.df = pd.DataFrame(dataset)
|
| 58 |
+
st.success(f"Dataset '{dataset_name}' loaded successfully!")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"Error loading dataset: {e}")
|
| 61 |
+
elif input_option == "Upload CSV File":
|
| 62 |
+
uploaded_file = st.file_uploader("Upload CSV File:", type=["csv"])
|
| 63 |
+
if uploaded_file:
|
| 64 |
+
try:
|
| 65 |
+
st.session_state.df = pd.read_csv(uploaded_file)
|
| 66 |
+
st.success("File uploaded successfully!")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"Error loading file: {e}")
|
| 69 |
+
|
| 70 |
+
# Load dataset
|
| 71 |
+
load_dataset_into_session()
|
| 72 |
|
| 73 |
# Show the loaded dataframe preview
|
| 74 |
if st.session_state.df is not None:
|
|
|
|
| 90 |
# Chat with the dataframe
|
| 91 |
response = chat_df.chat(question)
|
| 92 |
|
| 93 |
+
# Display response
|
| 94 |
+
if isinstance(response, pd.DataFrame):
|
| 95 |
+
st.write("### Response")
|
| 96 |
+
st.dataframe(response)
|
| 97 |
else:
|
| 98 |
st.write("### Response")
|
| 99 |
+
st.write(response)
|
| 100 |
+
|
|
|
|
| 101 |
st.success("Request processed successfully!")
|
| 102 |
except Exception as e:
|
| 103 |
st.error(f"An error occurred: {e}")
|