DrishtiSharma commited on
Commit
7af1158
·
verified ·
1 Parent(s): 79a526f

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +100 -0
app2.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from pandasai import SmartDataframe
4
+ from pandasai.llm import OpenAI
5
+ from pandasai.llm.groq import ChatGroq
6
+ from dotenv import load_dotenv
7
+ from datasets import load_dataset
8
+ import os
9
+
10
+ def initialize_llm(model_choice):
11
+ """Initialize the chosen LLM based on the user's selection."""
12
+ groq_api_key = os.getenv("GROQ_API_KEY")
13
+ openai_api_key = os.getenv("OPENAI_API_KEY")
14
+
15
+ if model_choice == "llama-3.3-70b":
16
+ if not groq_api_key:
17
+ st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
18
+ return None
19
+ return ChatGroq(groq_api_key=groq_api_key, model="groq/llama-3.3-70b-versatile")
20
+ elif model_choice == "GPT-4o":
21
+ if not openai_api_key:
22
+ st.error("OpenAI API key is missing. Please set the OPENAI_API_KEY environment variable.")
23
+ return None
24
+ return OpenAI(api_token=openai_api_key)
25
+
26
+ def load_dataset_into_session():
27
+ """Load dataset from Hugging Face or via CSV upload."""
28
+ input_option = st.radio("Select Dataset Input:", ["Use Hugging Face Dataset", "Upload CSV File"])
29
+
30
+ if input_option == "Use Hugging Face Dataset":
31
+ dataset_name = st.text_input("Enter Hugging Face Dataset Name:", value="HUPD/hupd")
32
+ if st.button("Load Dataset"):
33
+ try:
34
+ # Using the original parameters
35
+ dataset = load_dataset(
36
+ dataset_name,
37
+ name="sample",
38
+ split="train",
39
+ trust_remote_code=True,
40
+ uniform_split=True
41
+ )
42
+ st.session_state.df = pd.DataFrame(dataset)
43
+ st.success(f"Dataset '{dataset_name}' loaded successfully!")
44
+ st.dataframe(st.session_state.df.head(10))
45
+ except Exception as e:
46
+ st.error(f"Error: {e}")
47
+
48
+ elif input_option == "Upload CSV File":
49
+ uploaded_file = st.file_uploader("Upload CSV File:", type=["csv"])
50
+ if uploaded_file:
51
+ st.session_state.df = pd.read_csv(uploaded_file)
52
+ st.success("File uploaded successfully!")
53
+ st.dataframe(st.session_state.df.head(10))
54
+
55
+ if "df" not in st.session_state:
56
+ st.session_state.df = None
57
+
58
+ # Streamlit app
59
+ st.title("Chat With Your Dataset Using PandasAI")
60
+
61
+ # LLM Selection
62
+ st.sidebar.title("Choose Your LLM")
63
+ model_choice = st.sidebar.radio(
64
+ "Select a model:",
65
+ ("GPT-4o", "llama-3.3-70b"),
66
+ help="Choose between OpenAI GPT-4o or Groq Llama-3.3-70b."
67
+ )
68
+
69
+ # Initialize LLM
70
+ llm = initialize_llm(model_choice)
71
+ if not llm:
72
+ st.stop()
73
+
74
+ # Dataset Loading
75
+ #st.header("Dataset Selection")
76
+ load_dataset_into_session()
77
+
78
+ # Interact with DataFrame if it's loaded
79
+ if st.session_state.df is not None:
80
+ st.subheader("Ask Questions About Your Dataset")
81
+ chat_df = SmartDataframe(st.session_state.df, config={"llm": llm})
82
+
83
+ user_query = st.text_input("Ask a question about your data:", "")
84
+ if user_query:
85
+ try:
86
+ response = chat_df.chat(user_query)
87
+ st.write("### Response:")
88
+ st.write(response)
89
+
90
+ # Check for keywords that indicate the user wants a plot
91
+ if any(keyword in user_query.lower() for keyword in ["plot", "graph", "draw", "visualize", "chart", "visualizations"]):
92
+ st.write("### Generating Plot...")
93
+ try:
94
+ chat_df.chat(user_query)
95
+ except Exception as e:
96
+ st.error(f"An error occurred while generating the plot: {e}")
97
+ except Exception as e:
98
+ st.error(f"An error occurred: {e}")
99
+ else:
100
+ st.info("Please load a dataset to start interacting.")