Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,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 |
-
import time
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -33,29 +33,39 @@ def initialize_llm(model_choice):
|
|
| 33 |
model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b"], index=0, horizontal=True)
|
| 34 |
llm = initialize_llm(model_choice)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
return df
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def load_uploaded_csv(uploaded_file
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Dataset selection logic
|
| 61 |
def load_dataset_into_session():
|
|
@@ -70,7 +80,7 @@ def load_dataset_into_session():
|
|
| 70 |
if st.button("Load Dataset"):
|
| 71 |
try:
|
| 72 |
with st.spinner("Loading dataset from the repo directory..."):
|
| 73 |
-
st.session_state.df =
|
| 74 |
st.success(f"File loaded successfully from '{file_path}'!")
|
| 75 |
except Exception as e:
|
| 76 |
st.error(f"Error loading dataset from the repo directory: {e}")
|
|
@@ -81,31 +91,21 @@ def load_dataset_into_session():
|
|
| 81 |
"Enter Hugging Face Dataset Name:", value="HUPD/hupd"
|
| 82 |
)
|
| 83 |
if st.button("Load Dataset"):
|
| 84 |
-
progress_bar = st.progress(0) # Initialize progress bar
|
| 85 |
-
def progress_callback(progress):
|
| 86 |
-
progress_bar.progress(progress) # Update progress bar dynamically
|
| 87 |
-
|
| 88 |
try:
|
| 89 |
-
st.session_state.df = load_huggingface_dataset(dataset_name
|
| 90 |
st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
|
| 91 |
except Exception as e:
|
| 92 |
st.error(f"Error loading Hugging Face dataset: {e}")
|
| 93 |
-
progress_bar.progress(0) # Reset progress bar on error
|
| 94 |
|
| 95 |
# Option 3: Upload CSV File
|
| 96 |
elif input_option == "Upload CSV File":
|
| 97 |
uploaded_file = st.file_uploader("Upload a CSV File:", type=["csv"])
|
| 98 |
if uploaded_file:
|
| 99 |
-
progress_bar = st.progress(0) # Initialize progress bar
|
| 100 |
-
def progress_callback(progress):
|
| 101 |
-
progress_bar.progress(progress) # Update progress bar dynamically
|
| 102 |
-
|
| 103 |
try:
|
| 104 |
-
st.session_state.df = load_uploaded_csv(uploaded_file
|
| 105 |
st.success("File uploaded successfully!")
|
| 106 |
except Exception as e:
|
| 107 |
st.error(f"Error reading uploaded file: {e}")
|
| 108 |
-
progress_bar.progress(0) # Reset progress bar on error
|
| 109 |
|
| 110 |
# Load dataset into session
|
| 111 |
load_dataset_into_session()
|
|
|
|
| 8 |
from datasets import load_dataset
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
from langchain_openai import ChatOpenAI
|
| 11 |
+
import time
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 33 |
model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b"], index=0, horizontal=True)
|
| 34 |
llm = initialize_llm(model_choice)
|
| 35 |
|
| 36 |
+
# Dataset loading without caching to support progress bar
|
| 37 |
+
def load_huggingface_dataset(dataset_name):
|
| 38 |
+
# Initialize progress bar
|
| 39 |
+
progress_bar = st.progress(0)
|
| 40 |
+
try:
|
| 41 |
+
# Incrementally update progress
|
| 42 |
+
progress_bar.progress(10)
|
| 43 |
+
dataset = load_dataset(dataset_name, name="all", split="train", trust_remote_code=True, uniform_split=True)
|
| 44 |
+
progress_bar.progress(50)
|
| 45 |
+
if hasattr(dataset, "to_pandas"):
|
| 46 |
+
df = dataset.to_pandas()
|
| 47 |
+
else:
|
| 48 |
+
df = pd.DataFrame(dataset)
|
| 49 |
+
progress_bar.progress(100) # Final update to 100%
|
| 50 |
return df
|
| 51 |
+
except Exception as e:
|
| 52 |
+
progress_bar.progress(0) # Reset progress bar on failure
|
| 53 |
+
raise e
|
| 54 |
+
|
| 55 |
+
def load_uploaded_csv(uploaded_file):
|
| 56 |
+
# Initialize progress bar
|
| 57 |
+
progress_bar = st.progress(0)
|
| 58 |
+
try:
|
| 59 |
+
# Simulate progress
|
| 60 |
+
progress_bar.progress(10)
|
| 61 |
+
time.sleep(1) # Simulate file processing delay
|
| 62 |
+
progress_bar.progress(50)
|
| 63 |
+
df = pd.read_csv(uploaded_file)
|
| 64 |
+
progress_bar.progress(100) # Final update
|
| 65 |
+
return df
|
| 66 |
+
except Exception as e:
|
| 67 |
+
progress_bar.progress(0) # Reset progress bar on failure
|
| 68 |
+
raise e
|
| 69 |
|
| 70 |
# Dataset selection logic
|
| 71 |
def load_dataset_into_session():
|
|
|
|
| 80 |
if st.button("Load Dataset"):
|
| 81 |
try:
|
| 82 |
with st.spinner("Loading dataset from the repo directory..."):
|
| 83 |
+
st.session_state.df = pd.read_csv(file_path)
|
| 84 |
st.success(f"File loaded successfully from '{file_path}'!")
|
| 85 |
except Exception as e:
|
| 86 |
st.error(f"Error loading dataset from the repo directory: {e}")
|
|
|
|
| 91 |
"Enter Hugging Face Dataset Name:", value="HUPD/hupd"
|
| 92 |
)
|
| 93 |
if st.button("Load Dataset"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
try:
|
| 95 |
+
st.session_state.df = load_huggingface_dataset(dataset_name)
|
| 96 |
st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
|
| 97 |
except Exception as e:
|
| 98 |
st.error(f"Error loading Hugging Face dataset: {e}")
|
|
|
|
| 99 |
|
| 100 |
# Option 3: Upload CSV File
|
| 101 |
elif input_option == "Upload CSV File":
|
| 102 |
uploaded_file = st.file_uploader("Upload a CSV File:", type=["csv"])
|
| 103 |
if uploaded_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
try:
|
| 105 |
+
st.session_state.df = load_uploaded_csv(uploaded_file)
|
| 106 |
st.success("File uploaded successfully!")
|
| 107 |
except Exception as e:
|
| 108 |
st.error(f"Error reading uploaded file: {e}")
|
|
|
|
| 109 |
|
| 110 |
# Load dataset into session
|
| 111 |
load_dataset_into_session()
|