Spaces:
Runtime error
Runtime error
Create tr.py
Browse files
tr.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
|
3 |
+
from tensorflow.keras.models import Sequential
|
4 |
+
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification, pipeline
|
5 |
+
|
6 |
+
# Sample data for sentiment analysis
|
7 |
+
texts = ["I love deep learning!", "I hate Mondays.", "This movie is fantastic.", "The weather is terrible."]
|
8 |
+
|
9 |
+
labels = [1, 0, 1, 0] # 1 for positive sentiment, 0 for negative sentiment
|
10 |
+
|
11 |
+
# Load the tokenizer and model
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
13 |
+
model = TFAutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
|
14 |
+
|
15 |
+
# Tokenize the texts
|
16 |
+
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors='tf')
|
17 |
+
|
18 |
+
# Compile the model
|
19 |
+
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
|
20 |
+
|
21 |
+
# Train the model
|
22 |
+
model.fit(inputs, labels, epochs=3, batch_size=2)
|
23 |
+
|
24 |
+
# Save the model to Hugging Face Model Hub
|
25 |
+
model.save_pretrained("./my-text-classifier")
|
26 |
+
|
27 |
+
# Load the saved model from disk
|
28 |
+
loaded_model = TFAutoModelForSequenceClassification.from_pretrained("./my-text-classifier")
|
29 |
+
|
30 |
+
# Use the loaded model for prediction
|
31 |
+
classifier = pipeline('text-classification', model=loaded_model, tokenizer=tokenizer)
|
32 |
+
result = classifier("I'm feeling great!")
|
33 |
+
print(result)
|