Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,35 +13,31 @@ from datasets import load_dataset
|
|
| 13 |
|
| 14 |
class BERTopicChatbot:
|
| 15 |
def __init__(self, dataset_name, text_column, split="train", max_samples=10000):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
dataset =
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
if text_column not in self.df.columns:
|
| 38 |
-
raise ValueError(f"Column '{text_column}' not found in dataset. Available columns: {self.df.columns}")
|
| 39 |
-
|
| 40 |
-
self.documents = self.df[text_column].tolist()
|
| 41 |
-
|
| 42 |
-
except Exception as e:
|
| 43 |
-
st.error(f"Error loading dataset: {str(e)}")
|
| 44 |
-
raise
|
| 45 |
|
| 46 |
# Create and train BERTopic model
|
| 47 |
self.topic_model = BERTopic(embedding_model=self.sentence_model)
|
|
@@ -65,6 +61,10 @@ class BERTopicChatbot:
|
|
| 65 |
'total_documents': len(self.documents),
|
| 66 |
'topics_found': len(set(self.topics))
|
| 67 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def get_most_similar_document(self, query, top_k=3):
|
| 70 |
# Encode the query
|
|
|
|
| 13 |
|
| 14 |
class BERTopicChatbot:
|
| 15 |
def __init__(self, dataset_name, text_column, split="train", max_samples=10000):
|
| 16 |
+
"""
|
| 17 |
+
Initialize chatbot with a Hugging Face dataset
|
| 18 |
+
dataset_name: name of the dataset on Hugging Face (e.g., 'vietnam/legal')
|
| 19 |
+
text_column: name of the column containing the text data
|
| 20 |
+
split: which split of the dataset to use ('train', 'test', 'validation')
|
| 21 |
+
max_samples: maximum number of samples to use (to manage memory)
|
| 22 |
+
"""
|
| 23 |
+
# Initialize BERT sentence transformer
|
| 24 |
+
self.sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 25 |
+
|
| 26 |
+
# Load dataset from Hugging Face
|
| 27 |
+
try:
|
| 28 |
+
dataset = load_dataset(dataset_name, split=split)
|
| 29 |
|
| 30 |
+
# Convert to pandas DataFrame and sample if necessary
|
| 31 |
+
if len(dataset) > max_samples:
|
| 32 |
+
dataset = dataset.shuffle(seed=42).select(range(max_samples))
|
| 33 |
+
|
| 34 |
+
self.df = dataset.to_pandas()
|
| 35 |
+
|
| 36 |
+
# Ensure text column exists
|
| 37 |
+
if text_column not in self.df.columns:
|
| 38 |
+
raise ValueError(f"Column '{text_column}' not found in dataset. Available columns: {self.df.columns}")
|
| 39 |
+
|
| 40 |
+
self.documents = self.df[text_column].tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Create and train BERTopic model
|
| 43 |
self.topic_model = BERTopic(embedding_model=self.sentence_model)
|
|
|
|
| 61 |
'total_documents': len(self.documents),
|
| 62 |
'topics_found': len(set(self.topics))
|
| 63 |
}
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(f"Error loading dataset: {str(e)}")
|
| 67 |
+
raise
|
| 68 |
|
| 69 |
def get_most_similar_document(self, query, top_k=3):
|
| 70 |
# Encode the query
|