tasal9's picture
Upload ZamAI Multilingual Embeddings model
bf7fc70 verified
raw
history blame
20.9 kB
#!/usr/bin/env python3
"""
ZamAI Multilingual Embeddings - Hugging Face Hub Deployment Script
This script pushes the ZamAI multilingual embeddings model to Hugging Face Hub
"""
import os
import subprocess
import sys
from pathlib import Path
from huggingface_hub import HfApi, login, create_repo
def check_huggingface_auth():
"""Check if user is authenticated with Hugging Face"""
try:
api = HfApi()
user_info = api.whoami()
print(f"βœ… Authenticated as: {user_info['name']}")
return True
except Exception as e:
print("❌ Not authenticated with Hugging Face")
print("Please run: huggingface-cli login")
return False
def create_model_card():
"""Create a comprehensive model card for the ZamAI model"""
model_card_content = """---
license: apache-2.0
datasets:
- tasal9/Pashto_Dataset
language:
- ps
- en
- ar
- ur
- fa
library_name: sentence-transformers
tags:
- multilingual
- embeddings
- semantic-search
- pashto
- chromadb
- llamaindex
- cross-lingual
- afghanistan
- zamai
pipeline_tag: feature-extraction
model-index:
- name: Multilingual-ZamAI-Embeddings
results: []
widget:
- source_sentence: "This is a sample sentence in English."
sentences:
- "This sentence is similar to the first one."
- "Ψ―Ψ§ Ψ¬Ω…Ω„Ω‡ Ψ― Ω„ΩˆΩ…Ϊ“Ϋ جملې Ψ³Ψ±Ω‡ ورΨͺΩ‡ Ψ―Ω‡."
- "This sentence has nothing to do with the others."
example_title: "English to multilingual similarity"
- source_sentence: "Ψ―Ψ§ ΩΎΩ‡ پښΨͺو کې ΫŒΩˆΩ‡ Ω†Ω…ΩˆΩ†Ω‡ Ψ¬Ω…Ω„Ω‡ Ψ―Ω‡."
sentences:
- "This is a sample sentence in English."
- "Ψ―Ψ§ Ψ¬Ω…Ω„Ω‡ Ψ― Ω„ΩˆΩ…Ϊ“Ϋ جملې Ψ³Ψ±Ω‡ ورΨͺΩ‡ Ψ―Ω‡."
- "Ψ²Ω‡ Ψ― پښΨͺو ژبې Ψ²Ψ―Ω‡ Ϊ©Ϊ“Ω‡ Ϊ©ΩˆΩ…."
example_title: "Pashto to multilingual similarity"
---
# ZamAI Multilingual Embeddings
This model provides state-of-the-art multilingual sentence embeddings with a special focus on Pashto language support. Built on the foundation of `sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2`, this model enables semantic search, document retrieval, and cross-lingual understanding across 50+ languages.
## Model Details
- **Model Type**: Sentence Transformer (BERT-based)
- **Base Model**: [sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2](https://huggingface.co/sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2)
- **Languages Supported**: 50+ including Pashto (ps), English (en), Arabic (ar), Urdu (ur), Farsi (fa), and more
- **Max Sequence Length**: 512 tokens
- **Output Dimensionality**: 384
- **License**: Apache 2.0
## Key Features
- **Cross-lingual Understanding**: Retrieve semantically similar content across different languages
- **Pashto Language Support**: Optimized for Pashto language processing and understanding
- **Vector Database Integration**: Ready-to-use with ChromaDB and LlamaIndex
- **High Performance**: Efficient processing suitable for real-time applications
## Usage
### Basic Usage with Sentence Transformers
```python
from sentence_transformers import SentenceTransformer
import numpy as np
# Load the model
model = SentenceTransformer('tasal9/Multilingual-ZamAI-Embeddings')
# English sentences
sentences_en = [
"This is a sample sentence in English.",
"This sentence is similar to the first one."
]
# Pashto sentences
sentences_ps = [
"Ψ―Ψ§ ΩΎΩ‡ پښΨͺو کې ΫŒΩˆΩ‡ Ω†Ω…ΩˆΩ†Ω‡ Ψ¬Ω…Ω„Ω‡ Ψ―Ω‡.",
"Ψ―Ψ§ Ψ¬Ω…Ω„Ω‡ Ψ― Ω„ΩˆΩ…Ϊ“Ϋ جملې Ψ³Ψ±Ω‡ ورΨͺΩ‡ Ψ―Ω‡."
]
# Get embeddings
embeddings_en = model.encode(sentences_en)
embeddings_ps = model.encode(sentences_ps)
# Calculate cross-lingual similarity
from numpy import dot
from numpy.linalg import norm
def cosine_similarity(a, b):
return dot(a, b) / (norm(a) * norm(b))
# Compare English and Pashto sentences
similarity = cosine_similarity(embeddings_en[0], embeddings_ps[0])
print(f"Cross-lingual similarity: {similarity:.4f}")
```
### Advanced Usage with ChromaDB and LlamaIndex
```python
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext, VectorStoreIndex
import chromadb
# Initialize the embedding model
embed_model = HuggingFaceEmbedding(model_name="tasal9/Multilingual-ZamAI-Embeddings")
# Set up ChromaDB
chroma_client = chromadb.PersistentClient(path="./chroma_db")
collection = chroma_client.get_or_create_collection("multilingual_collection")
vector_store = ChromaVectorStore(chroma_collection=collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# Create index with your documents
# index = VectorStoreIndex.from_documents(documents, storage_context=storage_context, embed_model=embed_model)
# query_engine = index.as_query_engine()
# Query in any language
# result = query_engine.query("What is the capital of Afghanistan?")
# result_ps = query_engine.query("Ψ― افغانسΨͺΨ§Ω† پلازمېنه Ϊ…Ω‡ Ψ―Ω‡ΨŸ")
```
## Performance
The model demonstrates excellent cross-lingual performance:
- **English-English**: High semantic similarity detection
- **Pashto-Pashto**: Native language understanding and similarity
- **Cross-lingual (English-Pashto)**: Strong cross-lingual semantic alignment
- **Multilingual**: Supports 50+ languages with consistent performance
## Applications
- **Semantic Search**: Find relevant documents across multiple languages
- **Cross-lingual Information Retrieval**: Retrieve Pashto content using English queries and vice versa
- **Document Similarity**: Compare documents in different languages
- **Question Answering**: Build multilingual QA systems
- **Content Recommendation**: Recommend similar content across languages
## Technical Details
- **Architecture**: BERT-based transformer model
- **Training Data**: Multilingual parallel and monolingual corpora
- **Optimization**: Optimized for semantic similarity tasks
- **Integration**: Compatible with Hugging Face Transformers, Sentence Transformers, LlamaIndex, and ChromaDB
## Citation
If you use this model in your research, please cite:
```bibtex
@misc{zamai-multilingual-embeddings-2024,
title={ZamAI Multilingual Embeddings: Cross-lingual Sentence Transformers with Pashto Support},
author={ZamAI Team},
year={2024},
url={https://huggingface.co/tasal9/Multilingual-ZamAI-Embeddings}
}
```
## License
This model is released under the Apache 2.0 License. See the [LICENSE](LICENSE) file for details.
## Contact
For questions or support, please open an issue on the [model repository](https://huggingface.co/tasal9/Multilingual-ZamAI-Embeddings) or contact the ZamAI team.
"""
with open("README.md", "w", encoding="utf-8") as f:
f.write(model_card_content)
print("βœ… Model card created: README.md")
def prepare_repository():
"""Prepare the repository for upload"""
print("πŸ”§ Preparing repository...")
# Create model card
create_model_card()
# Create additional files
files_to_create = {
".gitattributes": """*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text
""",
"LICENSE": """Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(which shall not include communications that are clearly marked or
otherwise designated in writing by the copyright owner as "Not a Work").
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based upon (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and derivative works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control
systems, and issue tracking systems that are managed by, or on behalf
of, the Licensor for the purpose of discussing and improving the Work,
but excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to use, reproduce, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Work, and to
permit persons to whom the Work is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Work.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, trademark, patent, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright notice to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Support. When redistributing the Work or
Derivative Works thereof, You may choose to offer, and charge a fee
for, acceptance of support, warranty, indemnity, or other liability
obligations and/or rights consistent with this License. However, in
accepting such obligations, You may act only on Your own behalf and on
Your sole responsibility, not on behalf of any other Contributor, and
only if You agree to indemnify, defend, and hold each Contributor
harmless for any liability incurred by, or claims asserted against,
such Contributor by reason of your accepting any such warranty or support.
END OF TERMS AND CONDITIONS"""
}
for filename, content in files_to_create.items():
with open(filename, "w", encoding="utf-8") as f:
f.write(content)
print(f"βœ… Created: {filename}")
def push_to_huggingface(repo_name="tasal9/Multilingual-ZamAI-Embeddings"):
"""Push the model to Hugging Face Hub"""
# Check authentication
if not check_huggingface_auth():
print("\nπŸ”‘ Please authenticate with Hugging Face first:")
print("huggingface-cli login")
return False
try:
# Prepare repository
prepare_repository()
# Create repository on Hugging Face Hub
api = HfApi()
try:
print(f"πŸš€ Creating repository: {repo_name}")
create_repo(
repo_id=repo_name,
repo_type="model",
exist_ok=True,
private=False
)
print(f"βœ… Repository created/verified: {repo_name}")
except Exception as e:
print(f"ℹ️ Repository may already exist: {e}")
# Upload files
print("πŸ“€ Uploading files to Hugging Face Hub...")
# Upload all files in the current directory
current_dir = Path(".")
files_to_upload = [
"README.md",
"requirements.txt",
"setup.py",
"demo.py",
"simple_demo.py",
"indexer.py",
"modeling.py",
"config.json",
"LICENSE",
".gitattributes"
]
for file_path in files_to_upload:
if os.path.exists(file_path):
try:
api.upload_file(
path_or_fileobj=file_path,
path_in_repo=file_path,
repo_id=repo_name,
commit_message=f"Add {file_path}"
)
print(f"βœ… Uploaded: {file_path}")
except Exception as e:
print(f"⚠️ Warning uploading {file_path}: {e}")
print(f"\nπŸŽ‰ Successfully pushed ZamAI Multilingual Embeddings to Hugging Face Hub!")
print(f"πŸ”— Model URL: https://huggingface.co/{repo_name}")
print(f"πŸ“– Usage: model = SentenceTransformer('{repo_name}')")
return True
except Exception as e:
print(f"❌ Error pushing to Hugging Face: {e}")
return False
if __name__ == "__main__":
print("πŸš€ ZamAI Multilingual Embeddings - Hugging Face Deployment")
print("=" * 60)
# Check if we're in the right directory
if not os.path.exists("setup.py"):
print("❌ Please run this script from the Multilingual-ZamAI-Embeddings directory")
sys.exit(1)
# Push to Hugging Face
success = push_to_huggingface()
if success:
print("\n✨ Deployment completed successfully!")
print("Your model is now available on Hugging Face Hub")
else:
print("\n❌ Deployment failed. Please check the errors above.")
sys.exit(1)