Instructions to use Ah7med/BERTopic_ArXiv with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- BERTopic
How to use Ah7med/BERTopic_ArXiv with BERTopic:
from bertopic import BERTopic model = BERTopic.load("Ah7med/BERTopic_ArXiv") - Notebooks
- Google Colab
- Kaggle
File size: 369,419 Bytes
b255031 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | # -*- coding: utf-8 -*-
"""Topic_Modeling
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/#fileId=https%3A//storage.googleapis.com/kaggle-colab-exported-notebooks/topic-modeling-bdd24b57-13d3-48b6-af92-3163e201b3d6.ipynb%3FX-Goog-Algorithm%3DGOOG4-RSA-SHA256%26X-Goog-Credential%3Dgcp-kaggle-com%2540kaggle-161607.iam.gserviceaccount.com/20250120/auto/storage/goog4_request%26X-Goog-Date%3D20250120T233605Z%26X-Goog-Expires%3D259200%26X-Goog-SignedHeaders%3Dhost%26X-Goog-Signature%3D4b3e7c096f94ba3002728872dc8f625b31d067ff1ed2658b2dceeb40a8ea2323c967e017902f432a25610f53b5fe9a84b44ae021823e422010301ec4b90e388406008e2d053ff8d2b50e6e08fbf05a79a3503bfa51be6284540b75d946cd5cb5895e5229728ace2cc35fc58e4af09d20a2f945e17e4f4fe7b8652a50f5578e688d184698986ccdbaa2531d5318f95d65cc6251b77b6d85e50352bc58b5b53a8017ec8b86b569c93b18fed9bf69630f8ef42039fa38632512af13df4f86bbbbbe859ea4d471b257d78ab4e9d0cd28ff4ed59b3eb92a1f3e44717c3aa482eaca9f7f17739bab1ee0918fe04decfa14394dfc2bcb7f69dd3e0f881cb7eca5ca5237
Referance :
1- https://chatgpt.com/share/6776a2e7-c510-8008-8d1b-9f8b1df0e113
11-https://chatgpt.com/c/676d3d51-b468-8008-820f-c2f676a8ba3c
2- https://maartengr.github.io/BERTopic/index.html
3: https://huggingface.co/datasets/inparallel/saudinewsnet `dataset`
4: sentence_transformers : https://www.sbert.net/docs/sentence_transformer/pretrained_models.html#multilingual-models
5: Arabic Tokenizers Leaderboard :https://huggingface.co/spaces/MohamedRashad/arabic-tokenizers-leaderboard
6: New embedding models are released frequently and their performance keeps getting better. To keep track of the best embedding models out there, you can visit the MTEB leaderboard : https://huggingface.co/spaces/mteb/leaderboard
7: AIR-Bench: https://huggingface.co/spaces/AIR-Bench/leaderboard
8:https://chatgpt.com/share/678eca95-a6c8-8008-97c0-9a8d520a8f3f
"""
! pip install datasets bertopic sentence_transformers Arabic-Stopwords==0.4.3
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
from warnings import filterwarnings
filterwarnings('ignore')
from datasets import load_dataset
# clean_library
import nltk
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('omw-1.4')
nltk.download('punkt_tab')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import re
import string
import re
from bertopic import BERTopic
from sentence_transformers import SentenceTransformer
# def clean_text(text):
# if text is None:
# return ''
# elif not isinstance(text, str):
# return ''
# text=str(text)
# #replace in url with رابط
# text = re.sub(r'http\S+|www\S+|https\S+', 'رابط', text, flags=re.MULTILINE)
# #replace any number with رقم
# text = re.sub(r'\d+', 'رقم', text)
# #replace any email with ايميل
# text = re.sub(r'\S*@\S*\s?', 'ايميل', text)
# #remove any extra space
# text = re.sub(r'\s+', ' ', text).strip()
# #set any space before and after punctuation
# text=re.sub(r'\s*([.,:;!?])\s*', r'\1', text)
# #tokenize
# words=word_tokenize(text)
# text=" ".join([ word for word in words if len(word)>1])
# #remove stopwords arabic
# stop_words=set(stopwords.words('arabic'))
# words=word_tokenize(text)
# text=" ".join([ word for word in words if word not in stop_words])
# return text.lower().strip()
def clean_text(text):
if text is None:
return ''
elif not isinstance(text, str):
return ''
text=str(text)
#replace in url with رابط
text = re.sub(r'http\S+|www\S+|https\S+', 'رابط', text, flags=re.MULTILINE)
#replace any number with رقم
text = re.sub(r'\d+', 'رقم', text)
#replace any email with ايميل
text = re.sub(r'\S*@\S*\s?', 'ايميل', text)
#remove any extra space
text = re.sub(r'\s+', ' ', text).strip()
#set any space before and after punctuation
text=re.sub(r'\s*([.,:;!?])\s*', r'\1', text)
#tokenize
words=word_tokenize(text)
text=" ".join([ word for word in words if len(word)>1])
#remove stopwords arabic
stop_words=set(stopwords.words('arabic'))
words=word_tokenize(text)
text=" ".join([ word for word in words if word not in stop_words])
return text.lower().strip()
"""# using Spacy"""
# #using spacy
# import spacy
# nlp=spacy.load('ar_core_news_sm')
# def clean_text_spacy(text):
# if text is None or not isinstance(text, str):
# return ''
# #replace in url with رابط
# text = re.sub(r'http\S+|www\S+|https\S+', 'رابط', text, flags=re.MULTILINE)
# #replace any number with رقم
# text = re.sub(r'\d+', 'رقم', text)
# #replace any email with ايميل
# text = re.sub(r'\S*@\S*\s?', 'ايميل', text)
# #remove any extra space
# text = re.sub(r'\s+', ' ', text).strip()
# #set any space before and after punctuation
# text=re.sub(r'\s*([.,:;!?])\s*', r'\1', text)
# doc=nlp(text)
# filterderd_text=" ".join([token.text for token in doc if not token.is_stop]) # or add also and len(token.text) > 1
# return filterderd_text.lower().strip()
stop_words=set(stopwords.words('arabic'))
print(stop_words)
try:
dataset=load_dataset("saudinewsnet")
except Exception as e:
print(e)
dataset
df=dataset['train'].to_pandas()
df.head()
Raw_dataset = df[["source", "date_extracted", "content"]]
Raw_dataset.head()
Raw_dataset=Raw_dataset.sample(frac=1,random_state=101)
dataset["train"]["content"][0]
Raw_dataset["content"] = Raw_dataset["content"].apply(clean_text)
Raw_dataset['content'].head()
Raw_dataset["text_len"]=Raw_dataset["content"].apply(lambda x:len(x.split()))
Raw_dataset.head()
#show in len in matlotlib
plt.figure(figsize=(10,5))
sns.histplot(Raw_dataset["text_len"],bins=50,kde=True)
plt.show()
print (Raw_dataset.shape)
Raw_dataset=Raw_dataset[Raw_dataset["text_len"]<=1000]
print (Raw_dataset.shape)
"""# convert date_extracted into date type"""
#date_extracted info type
Raw_dataset.info()
Raw_dataset["date_extracted"]=pd.to_datetime(Raw_dataset["date_extracted"])
"""# topic_modeling using bert_topic"""
bert_topic=BERTopic(language="arabic",verbose=True)
topic,probs=bert_topic.fit_transform(Raw_dataset["content"])
# print (topic)
# # Display topics
# for topic_id, topic_keywords in bert_topic.get_topics(216).items():
# print(f"Topic {topic_id}: {topic_keywords}")
topic_freq=bert_topic.get_topic_info()
print (topic_freq)
"""# most frequent topic that was generated, topic 0"""
bert_topic.get_topic(216)
"""# extract information on a document level, such as their corresponding topics, probabilities,"""
bert_topic.get_document_info(Raw_dataset["content"])
"""# fine_tune representation with keybertinspierd
## represented by contextual keyphrases, not just frequent terms.
"""
from bertopic.representation import KeyBERTInspired
topic_model=BERTopic(language="arabic",verbose=True,representation_model=KeyBERTInspired())
topics,probs=topic_model.fit_transform(Raw_dataset["content"])
topic_freq=topic_model.get_topic_info()
print (topic_freq)
topic_model.get_document_info(Raw_dataset["content"])
topic_model.get_document_info(Raw_dataset["content"]).loc[3]
"""# Comparison of Representation"""
bert_topic.get_topic(170)
bert_topic.get_document_info(Raw_dataset["content"]).loc[3]
#Document 3
topic_model.get_document_info(Raw_dataset["content"]).loc[3]
"""the `key_inspierd_representation` is the best :
1. Improved Clarity: Topics are represented by contextual keyphrases, not just
frequent terms.
2. Better Interpretability: KeyBERT ensures that the topic representations are more human-readable and understandable.
3. Domain-Specific Insights: For applications like Saudi newspapers, this method captures nuanced topics such as "Vision 2030" or "Al-Ula heritage," making the topics more relevant to the domain.
"""
def clean_for_inferance(text):
# Remove percentages
text = re.sub(r'\d+%', '', text)
# Remove dates in the format dd / mm / yyyy or similar
text = re.sub(r'\d+\s*/\s*\d+\s*/\s*\d+', '', text)
# Remove all numbers
text = re.sub(r'\d+', '', text)
# Remove extra spaces
text = re.sub(r'\s+', ' ', text).strip()
return text
# Predict new documents
new_documents="""
ضبطت الحملات الميدانية المشتركة لمتابعة وضبط مخالفي أنظمة الإقامة والعمل وأمن الحدود، في مناطق السعودية كافة في الفترة من 09 / 01 / 2025 إلى 15 / 01 / 2025، 21485 مخالفاً.
وطبقاً لبيان وزارة الداخلية السعودية، فإن هؤلاء المخالفين منهم 13562 مخالفًا لنظام الإقامة، و4853 مخالفًا لنظام أمن الحدود، و3070 مخالفًا لنظام العمل.
ويشير البيان أيضاً إلى أن إجمالي الذين ضُبطوا في أثناء محاولتهم عبور الحدود إلى داخل السعودية يصل عددهم نحو 1568 شخصًا 47%، منهم يمنيو الجنسية، و50% إثيوبيو الجنسية، و03% من جنسيات أخرى.
وضبطت الحملات الميدانية المشتركة 64 شخصًا لمحاولتهم عبور الحدود إلى خارج المملكة بطريقة غير نظامية، فضلاً عن ضبط 16متورطـًا في نقل وإيواء وتشغيل مخالفي أنظمة الإقامة والعمل وأمن الحدود والتستر عليهم.
وأكدت وزارة الداخلية أن كل من يسهل دخول مخالفي نظام أمن الحدود للمملكة أو نقلهم داخلها أو يوفر لهم المأوى أو يقدم لهم أي مساعدة أو خدمة بأي شكل من الأشكال، يعرض نفسه لعقوبات تصل إلى السجن مدة 15 سنة، وغرامة مالية تصل إلى مليون ريال، ومصادرة وسيلة النقل والسكن المستخدم للإيواء، إضافة إلى التشهير به.
وأوضحت في الوقت نفسه أن إجمالي الذين يخضعون للإجراءات الخاصة بتنفيذ الأنظمة يصل عددهم إلى نحو 33007 وافدين مخالفين، منهم 30335 رجلاً، و2672 امرأة
"""
# Clean the text
cleaned_text = clean_for_inferance(new_documents)
print(cleaned_text)
# new_documents=[clean_for_inferance(doc) for doc in new_documents]
new_topics, new_probs = topic_model.transform(cleaned_text)
# new_topics
# Split the text into sentences (optional)
documents = cleaned_text.split('. ')
documents
# Get topic info for all topics
print(topic_model.get_topic_info(new_topics[0]))
"""# Update topic representation"""
topic_model.update_topics(Raw_dataset["content"], n_gram_range=(1, 2))
# print(topic_model.get_topics())
topic_model.get_topic_info()
topic_model.get_document_info(Raw_dataset["content"])
topic_model.get_document_info(Raw_dataset["content"]).loc[3]
"""
#### `Improved Context`: By including bigrams or trigrams, the updated topic representations capture more contextually meaningful phrases (e.g., "Vision 2030" instead of just "Vision").
#### `More Informative Keywords`: Longer n-grams often provide more specific and relevant representations for topics, especially in domains like news, where phrases like "economic transformation" or "football league" are common.
#### `Customization`: You can adjust the n-gram range to suit the nature of your data:
1- Use unigrams for general topics.
2-Use bigrams/trigrams for more domain-specific or context-sensitive topics.
"""
topic_model.get_topic_freq()
# # Get representative documents
# representative_docs = topic_model.get_representative_docs()
# # Display representative documents for each topic
# for topic_id, docs in representative_docs.items():
# print(f"Topic {topic_id}:")
# for doc in docs:
# print(f"- {doc}")
"""### Generate topic labels without manual topic labeling"""
# # Generate topic labels
# topic_labels=topic_model.generate_topic_labels(nr_words=10)
# for topic_id, label in enumerate(topic_labels):
# print(f"Topic {topic_id}: {label}")
"""### Improved Interpretability: `Automatically generated labels` make it easier to understand and communicate the results of topic modeling."""
# # .reduce_topics(docs, nr_topics=30)
# topic_reduce=topic_model.reduce_topics(Raw_dataset["content"], nr_topics=30)
# for topic_id, topic_keywords in topic_model.get_topics(topic_reduce).items():
# print(f"Topic {topic_id}: {topic_keywords}")
topic_model.get_topic_info()
topic_model=BERTopic(language="arabic",verbose=True,representation_model=KeyBERTInspired())
topics,probs=topic_model.fit_transform(Raw_dataset["content"])
topic_model.find_topics("vehicle")
"""# Visualizations"""
#visulization
topic_model.visualize_topics()
"""## from the previouse visulization we can see the closely similar topic"""
Raw_dataset["content"].head()
#visulization
Raw_dataset_reset = Raw_dataset.reset_index(drop=True)
Raw_dataset_reset.head()
topic_model.visualize_documents(Raw_dataset_reset["content"])
#Custom Hover¶
# topic_model.visualize_documents(Raw_dataset_reset["content"],titles)
# topic_model.visualize_hierarchical_documents(Raw_dataset_reset["content"])
# topic_model.visualize_hierarchy(Raw_dataset_reset["content"])
"""### After we use the package we try build customized topic model using Modularity :

"""
raw_dataset = df[["source", "date_extracted", "content"]]
raw_dataset=raw_dataset.sample(frac=1,random_state=101)
raw_dataset["content"] = raw_dataset["content"].apply(clean_text)
raw_dataset.head(1)
raw_dataset["date_extracted"]=pd.to_datetime(raw_dataset["date_extracted"]).dt.date
raw_dataset["text_len"]=raw_dataset["content"].apply(lambda x:len(x.split()))
print (raw_dataset.shape)
raw_dataset=raw_dataset[raw_dataset["text_len"]<=1000]
print (raw_dataset.shape)
"""# Embedding"""
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('sentence-transformers/LaBSE') #you can use this :model_id = "sentence-transformers/LaBSE"
embedding=model.encode(raw_dataset["content"].values,show_progress_bar=True)
# import numpy as np
# # save embedding for this path /content/drive/MyDrive/embedding
# np.save("/content/drive/MyDrive/embedding.npy", embedding)
# # upload embedding
# embedding_test=np.load("/content/drive/MyDrive/embedding.npy")
# embedding_test.shape
embedding.shape
"""# Dimensionality Reduction
### you can use UMAP but i here use cuML UMAP
"""
# !pip install cuml-cu12 --extra-index-url=https://pypi.nvidia.com
from umap import UMAP
umap_model=UMAP(n_components=15, n_neighbors=15,
min_dist=0.0,random_state=101,metric='cosine')
"""# Clustering"""
from hdbscan import HDBSCAN
"""### A higher min_cluster_size will generate fewer topics
### A lower min_cluster_size will generate more topics.
"""
hdbscan_model=HDBSCAN(min_cluster_size=50,metric='euclidean',
cluster_selection_method='eom', prediction_data=True)
"""# Vectorizer"""
from sklearn.feature_extraction.text import CountVectorizer
import arabicstopwords.arabicstopwords as stp
stop_words=stp.stopwords_list() #or use ["", " "," "]
vectorizer_model = CountVectorizer(min_df=3,
stop_words=stop_words,
analyzer='word',
max_df=0.5,
ngram_range=(1,3)
)
# topic_model.update_topics(docs, vectorizer_model=vectorizer_model)
"""# Topic Representer"""
from bertopic.representation import KeyBERTInspired
keybert_model = KeyBERTInspired()
representation_model = {
"KeyBERT": keybert_model
}
"""# Let's Go with Customized topic model using Modularity :"""
topic_model = BERTopic(embedding_model=model,
umap_model=umap_model,
hdbscan_model=hdbscan_model,
vectorizer_model=vectorizer_model,
representation_model=representation_model,
top_n_words=10,
calculate_probabilities=True,
verbose=True,
)
topics, probs =topic_model.fit_transform(
raw_dataset["content"].values,
embedding
)
topic_model.get_topic_info()
raw_dataset.head(10)
raw_dataset["content"].values[19]
topics[19], probs[19]
topic_model.get_topic(11)
topic_model.get_topic_info(11)
raw_dataset["topic"]= topics
raw_dataset["prob"]=probs
raw_dataset.head(7)
raw_dataset[ raw_dataset['topic'] == 11 ].head(10)
"""# can use in recommendations and improve customer satisfaction
# Visualizing BERTopic
"""
# # Convert to datetime and remove the time
# df['date'] = pd.to_datetime(df['datetime']).dt.date
topic_model.visualize_topics()
topic_model.visualize_heatmap()
"""# Visualize Topics over Time"""
topics_over_time = topic_model.topics_over_time(raw_dataset['content'],raw_dataset['date_extracted'])
topic_model.visualize_topics_over_time(topics_over_time, topics=[10, 11, 12, 14, 15, 16, 17, 18, 19]) # or dont use topics
"""# Visualize Topics per Class"""
topics_per_class = topic_model.topics_per_class(raw_dataset['content'].values
, classes=raw_dataset['source'].values)
topic_model.visualize_topics_per_class(topics_per_class)
"""# Visualize documents with Plotly"""
# Visualize the documents
fig = topic_model.visualize_documents(raw_dataset['content'].values, embeddings=embedding)
fig.show()
"""# Visualize documents with DataMapPlot
"""
topic_model.visualize_document_datamap(raw_dataset['content'].values,embeddings=embedding)
# # if you want to save the resulting figure
# fig = topic_model.visualize_document_datamap(raw_dataset['content'].values)
# fig.savefig("path/to/file.png", bbox_inches="tight")
topic_model.get_topic(6)
raw_dataset['content'].values[6]
topics[6]
# To visualize the probabilities of topic assignment
topic_model.visualize_distribution(probs[6], min_probability=0.01)
print(embedding.shape)
print(len(raw_dataset['content'].values))
# Calculate the topic distributions on a token-level
topic_distr, topic_token_distr = topic_model.approximate_distribution(raw_dataset['content'].values, calculate_tokens=True)
# Visualize the token-level distributions
df = topic_model.visualize_approximate_distribution(raw_dataset['content'].values[7], topic_token_distr[7])
df
"""# How It Helps:
#### Identify key tokens that strongly influence the topic or label assignment.
#### Understand why a document is assigned to a specific topic or class.
#### Optimize preprocessing by filtering out low-contributing tokens.
#### Focus on tokens that carry the most semantic weight.
#### Merge or split topics based on token overlaps.
#### Remove outlier tokens that skew topic representation.
#### Refine keywords to target specific audiences or improve SEO.
#### Debug models by finding tokens that lead to misclassifications.
#### Adjust data preprocessing or model training to fix errors.
# Terms
"""
topic_model.visualize_barchart()
topic_model.visualize_term_rank()
"""### Optimize content creation by focusing on terms that define high-priority topics.
### Refine SEO strategies by identifying keywords associated with relevant topics
"""
topic_model.get_topic_info(2)
topic_model.visualize_term_rank(log_scale=True)
"""# Hierarchy"""
topic_model.visualize_hierarchy()
# # if you want to merge two topic or more
# topic_to_merge=[
# [15,60,4],
# [30,23,7]
# ]
# topic_model.merge_topics(
# raw_dataset_df['content'].values,
# topics_to_merge
# )
# hierarchical_topics = topic_model.hierarchical_topics(
# raw_dataset_df['content'].values
# )
# topic_model.visualize_hierarchy(
# hierarchical_topics=hierarchical_topics
# )
hierarchical_topics = topic_model.hierarchical_topics(raw_dataset['content'].values)
topic_model.visualize_hierarchy(hierarchical_topics=hierarchical_topics)
topic_model.get_topic(5)
tree = topic_model.get_topic_tree(hierarchical_topics)
print(tree)
from umap import UMAP
# Generate hierarchical topics
hierarchical_topics = topic_model.hierarchical_topics(raw_dataset['content'].values)
# # Run the visualization with original embeddings
topic_model.visualize_hierarchical_documents(
raw_dataset['content'].values,
hierarchical_topics,
embeddings=embedding
)
# Optional: Reduce dimensionality of embeddings
umap = UMAP(n_neighbors=15, n_components=15, min_dist=0.0, metric='cosine', random_state=101)
reduced_embeddings = umap.fit_transform(embedding)
# Visualize with reduced embeddings
topic_model.visualize_hierarchical_documents(
raw_dataset['content'].values,
hierarchical_topics,
reduced_embeddings=reduced_embeddings,
hide_document_hover=True
)
raw_dataset.head()
"""# Inference"""
story = """
طرحت مؤسسة البترول الكويتية عطاءً؛ لبيع زيت وقود عالي الكبريت للتحميل في الفترة من فبراير/ شباط إلى إبريل/ نيسان.
وأوضحت مصادر تجارية، اليوم الخميس، أن المؤسسة تعرض شحنات من زيت الوقود عالي الكبريت تبلغ الواحدة 60 ألف طن لتحميلها من الكويت شهرياً بالفترة المذكورة.
"""
_topic, _prob = topic_model.transform([story])
_topic
topic_model.get_topic(17)
raw_dataset[raw_dataset["topic"]==17].head()
"""# that is perfect
# Save model
"""
model_id="sentence-transformers/LaBSE"
topic_model.save("/kaggle/working/bertopic_dir", serialization="safetensors", save_ctfidf=True, save_embedding_model=model_id)
"""# Loading"""
# Load from directory
loaded_model = BERTopic.load("/kaggle/working/bertopic_dir")
from huggingface_hub import login
login()
from bertopic import BERTopic
# Push to HuggingFace Hub
topic_model.push_to_hf_hub(
repo_id="Ah7med/BERTopic_ArXiv",
save_ctfidf=True
)
|