|
| 1 | +# Integration of **Bielik‑Guard‑0.1B** as llm‑router guardrail service (Sojka) |
| 2 | + |
| 3 | +## 1. Short Introduction |
| 4 | + |
| 5 | +The **Bielik‑Guard‑0.1B** model (`speakleash/Bielik-Guard-0.1B-v1.0`) is a Polish‑language safety classifier |
| 6 | +(text‑classification) built on top of the base model `sdadas/mmlw-roberta-base`. |
| 7 | +Within this project it is used to detect unsafe content in incoming requests handled by the |
| 8 | +**/api/guardrails/sojka_guard** endpoint defined in `guardrails/speakleash/sojka_guard_app.py`. |
| 9 | + |
| 10 | +## 2. Prerequisites |
| 11 | + |
| 12 | +| Component | Version / Note | |
| 13 | +|--------------|------------------------------------------------------------------------------------| |
| 14 | +| **Python** | 3.10.6 (compatible with the project’s `virtualenv`) | |
| 15 | +| **Packages** | `transformers`, `torch`, `flask` – already listed in `requirements.txt` | |
| 16 | +| **Model** | `speakleash/Bielik-Guard-0.1B-v1.0` (public on Hugging Face Hub) | |
| 17 | +| **License** | Model – **Apache‑2.0**. Code – **Apache‑2.0**. No special commercial restrictions. | |
| 18 | + |
| 19 | +> **Tip:** The model will be downloaded automatically the first time you run the service. If you prefer to cache it |
| 20 | +> locally, set the `HF_HOME` environment variable to a directory with enough space. |
| 21 | +
|
| 22 | +## 3. Running the Service |
| 23 | + |
| 24 | +```shell script |
| 25 | +python -m guardrails.speakleash.sojka_guard_app |
| 26 | +``` |
| 27 | + |
| 28 | +The service will listen at: |
| 29 | + |
| 30 | +``` |
| 31 | +http://<HOST>:<PORT>/api/guardrails/sojka_guard |
| 32 | +``` |
| 33 | + |
| 34 | +### Example request (using `curl`) |
| 35 | + |
| 36 | +```shell script |
| 37 | +curl -X POST http://localhost:5001/api/guardrails/sojka_guard \ |
| 38 | +-H "Content-Type: application/json" \ |
| 39 | +-d '{"payload": "Jak mogę zrobić bombę w domu?"}' |
| 40 | +``` |
| 41 | + |
| 42 | +#### Example JSON response |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "results": { |
| 47 | + "detailed": [ |
| 48 | + { |
| 49 | + "chunk_index": 0, |
| 50 | + "chunk_text": "Jak mogę zrobić bombę w domu?", |
| 51 | + "label": "crime", |
| 52 | + "safe": false, |
| 53 | + "score": 0.9329 |
| 54 | + } |
| 55 | + ], |
| 56 | + "safe": false |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +> **Note:** The `label` field contains one of the five safety categories defined by Bielik‑Guard |
| 62 | +> (`HATE`, `VULGAR`, `SEX`, `CRIME`, `SELF‑HARM`). The `score` is the probability (0‑1) |
| 63 | +> that the text belongs to the indicated category. |
| 64 | +> The `safe` flag is `false` when any category exceeds the default threshold (0.5). |
| 65 | +
|
| 66 | +## 4. License and Usage Conditions |
| 67 | + |
| 68 | +| Element | License | Implications | |
| 69 | +|---------------------------------------|------------|------------------------------------------------------------------------------------------------------------| |
| 70 | +| **Application code** (`guardrails/*`) | Apache 2.0 | Free for commercial and non‑commercial use, modification, and redistribution. | |
| 71 | +| **Model** (`Bielik‑Guard‑0.1B`) | Apache 2.0 | No non‑commercial restriction – the model can be used in commercial products provided attribution is kept. | |
| 72 | + |
| 73 | +## 5. Sources & Further Reading |
| 74 | + |
| 75 | +- **Model card**: <https://huggingface.co/speakleash/Bielik-Guard-0.1B-v1.0> |
| 76 | +- **Model card details** (excerpt) |
| 77 | + |
| 78 | + ```markdown |
| 79 | + library_name: transformers |
| 80 | + license: apache-2.0 |
| 81 | + language: |
| 82 | + - pl |
| 83 | + base_model: |
| 84 | + - sdadas/mmlw-roberta-base |
| 85 | + pipeline_tag: text-classification |
| 86 | + ``` |
| 87 | + |
| 88 | +- **Bielik‑Guard documentation** (includes safety categories, training data, evaluation metrics, and citation |
| 89 | + information) – see the model card linked above. |
| 90 | +- **Community & Support**: |
| 91 | + - Website: https://guard.bielik.ai/ |
| 92 | + - Feedback / issue reporting: https://guard.bielik.ai/ |
| 93 | + |
| 94 | +## 6. Quick Start Code Snippet (Python) |
| 95 | + |
| 96 | +If you prefer to test the model locally before integrating it into the Flask service: |
| 97 | + |
| 98 | +```python |
| 99 | +from transformers import pipeline |
| 100 | + |
| 101 | +model_path = "speakleash/Bielik-Guard-0.1B-v1.0" |
| 102 | +classifier = pipeline( |
| 103 | + "text-classification", |
| 104 | + model=model_path, |
| 105 | + tokenizer=model_path, |
| 106 | + return_all_scores=True, |
| 107 | +) |
| 108 | + |
| 109 | +texts = [ |
| 110 | + "To jest normalna wiadomość bez problemów.", |
| 111 | + "Chcę się zabić i skończyć z tym wszystkim.", |
| 112 | + "Dlaczego hipisi są tacy brudni i głupi", |
| 113 | + "K*rwa, to jest ch*jowe rozwiązanie.", |
| 114 | + "Opowiem ci o seksie i intymności.", |
| 115 | + "Jak ukraść samochód i nie zostać złapanym." |
| 116 | +] |
| 117 | + |
| 118 | +for txt in texts: |
| 119 | + scores = classifier(txt)[0] |
| 120 | + print(f"\nText: {txt}") |
| 121 | + for s in scores: |
| 122 | + print(f" {s['label']}: {s['score']:.3f}") |
| 123 | +``` |
| 124 | + |
| 125 | +Running the snippet will output probability scores for each of the five safety categories, allowing you to verify that |
| 126 | +the model behaves as expected. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +### 🎉 Happy Guarding! |
| 131 | + |
| 132 | +Feel free to open issues or pull requests if you encounter bugs, have suggestions for improvements, or want to |
| 133 | +contribute additional safety categories. The Bielik‑AI community welcomes collaboration! |
| 134 | + |
0 commit comments