Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +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 |
|
12 |
# Load environment variables
|
13 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
@@ -38,15 +39,23 @@ def load_repo_dataset(file_path):
|
|
38 |
return pd.read_csv(file_path)
|
39 |
|
40 |
@st.cache_data
|
41 |
-
def load_huggingface_dataset(dataset_name):
|
42 |
dataset = load_dataset(dataset_name, name="all", split="train", trust_remote_code=True, uniform_split=True)
|
43 |
if hasattr(dataset, "to_pandas"):
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
return pd.DataFrame(dataset)
|
46 |
|
47 |
@st.cache_data
|
48 |
-
def load_uploaded_csv(uploaded_file):
|
49 |
-
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Dataset selection logic
|
52 |
def load_dataset_into_session():
|
@@ -60,7 +69,8 @@ def load_dataset_into_session():
|
|
60 |
file_path = "./source/test.csv"
|
61 |
if st.button("Load Dataset"):
|
62 |
try:
|
63 |
-
st.
|
|
|
64 |
st.success(f"File loaded successfully from '{file_path}'!")
|
65 |
except Exception as e:
|
66 |
st.error(f"Error loading dataset from the repo directory: {e}")
|
@@ -71,21 +81,31 @@ def load_dataset_into_session():
|
|
71 |
"Enter Hugging Face Dataset Name:", value="HUPD/hupd"
|
72 |
)
|
73 |
if st.button("Load Dataset"):
|
|
|
|
|
|
|
|
|
74 |
try:
|
75 |
-
st.session_state.df = load_huggingface_dataset(dataset_name)
|
76 |
st.success(f"Hugging Face Dataset '{dataset_name}' loaded successfully!")
|
77 |
except Exception as e:
|
78 |
st.error(f"Error loading Hugging Face dataset: {e}")
|
|
|
79 |
|
80 |
# Option 3: Upload CSV File
|
81 |
elif input_option == "Upload CSV File":
|
82 |
uploaded_file = st.file_uploader("Upload a CSV File:", type=["csv"])
|
83 |
if uploaded_file:
|
|
|
|
|
|
|
|
|
84 |
try:
|
85 |
-
st.session_state.df = load_uploaded_csv(uploaded_file)
|
86 |
st.success("File uploaded successfully!")
|
87 |
except Exception as e:
|
88 |
st.error(f"Error reading uploaded file: {e}")
|
|
|
89 |
|
90 |
# Load dataset into session
|
91 |
load_dataset_into_session()
|
@@ -155,7 +175,6 @@ with st.sidebar:
|
|
155 |
" - Example: 'Predict if the patent will be accepted.'\n"
|
156 |
" - Example: 'What is the primary classification of this patent?'\n"
|
157 |
" - Example: 'Summarize the abstract of this patent.'\n"
|
158 |
-
#"4. Download the processed dataset as a CSV file."
|
159 |
)
|
160 |
st.markdown("---")
|
161 |
st.header("References:")
|
|
|
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")
|
|
|
39 |
return pd.read_csv(file_path)
|
40 |
|
41 |
@st.cache_data
|
42 |
+
def load_huggingface_dataset(dataset_name, progress_callback):
|
43 |
dataset = load_dataset(dataset_name, name="all", split="train", trust_remote_code=True, uniform_split=True)
|
44 |
if hasattr(dataset, "to_pandas"):
|
45 |
+
progress_callback(50) # Update progress bar
|
46 |
+
df = dataset.to_pandas()
|
47 |
+
progress_callback(100) # Final progress update
|
48 |
+
return df
|
49 |
+
progress_callback(100)
|
50 |
return pd.DataFrame(dataset)
|
51 |
|
52 |
@st.cache_data
|
53 |
+
def load_uploaded_csv(uploaded_file, progress_callback):
|
54 |
+
time.sleep(1) # Simulate processing delay
|
55 |
+
progress_callback(50) # Update progress bar
|
56 |
+
df = pd.read_csv(uploaded_file)
|
57 |
+
progress_callback(100) # Final progress update
|
58 |
+
return df
|
59 |
|
60 |
# Dataset selection logic
|
61 |
def load_dataset_into_session():
|
|
|
69 |
file_path = "./source/test.csv"
|
70 |
if st.button("Load Dataset"):
|
71 |
try:
|
72 |
+
with st.spinner("Loading dataset from the repo directory..."):
|
73 |
+
st.session_state.df = load_repo_dataset(file_path)
|
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 |
"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, progress_callback)
|
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, progress_callback)
|
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()
|
|
|
175 |
" - Example: 'Predict if the patent will be accepted.'\n"
|
176 |
" - Example: 'What is the primary classification of this patent?'\n"
|
177 |
" - Example: 'Summarize the abstract of this patent.'\n"
|
|
|
178 |
)
|
179 |
st.markdown("---")
|
180 |
st.header("References:")
|