Any-to-Any
Transformers
English
Polish
transformer
hardbanai, hardbanrecords, audio-generation, text-generation, image-generation, ai-publishing, creative-ai, emotional-style, multimodal, polish-language, musicgen
Instructions to use HardbanRecordsLab/HardbanAI with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use HardbanRecordsLab/HardbanAI with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("HardbanRecordsLab/HardbanAI", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Create handler.py
Browse files- handler.py +38 -0
handler.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import torch
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Inicjalizacja modelu i tokenizera (ładuj tylko raz, przy starcie handlera)
|
| 6 |
+
model_path = "./" # ścieżka do folderu z modelem
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
def handle(event, context):
|
| 13 |
+
"""
|
| 14 |
+
event: dict, z kluczem 'text' zawierającym tekst do przetworzenia
|
| 15 |
+
context: obiekt kontekstowy (zależny od platformy, nieużywany tu)
|
| 16 |
+
|
| 17 |
+
Zwraca dict z wygenerowanym tekstem.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
# Wczytaj input
|
| 21 |
+
try:
|
| 22 |
+
if isinstance(event, str):
|
| 23 |
+
event = json.loads(event)
|
| 24 |
+
text = event.get("text", "")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return {"error": f"Błąd parsowania danych wejściowych: {str(e)}"}
|
| 27 |
+
|
| 28 |
+
if not text:
|
| 29 |
+
return {"error": "Brak tekstu do przetworzenia."}
|
| 30 |
+
|
| 31 |
+
# Tokenizacja i generacja
|
| 32 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
outputs = model.generate(**inputs)
|
| 35 |
+
|
| 36 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
return {"generated_text": result}
|