File size: 1,454 Bytes
87e0cfe
 
 
fa0bd4d
87e0cfe
 
fa0bd4d
69dc3fc
fa0bd4d
 
 
 
 
 
 
87e0cfe
 
 
fa0bd4d
87e0cfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import safetensors_rust

# Load the trained model and tokenizer
model_path = 'viv/AIKIA'  # Ensure this path is correct, either local or Hugging Face path
tokenizer = AutoTokenizer.from_pretrained("nlpaueb/bert-base-greek-uncased-v1")

# Try loading the model, fallback to `.bin` if `.safetensors` fails
try:
    model = AutoModelForSequenceClassification.from_pretrained(model_path)
except safetensors_rust.SafetensorError:
    print("Safetensors failed, trying to load bin file.")
    model = AutoModelForSequenceClassification.from_pretrained("viv/AIKIA/pytorch_model.bin")

# Preprocessing function for Greek text
def preprocessing_greek(text):
    text = text.lower()  # Example step: Convert to lowercase
    return text

# Prediction function
def predict(sentence):
    model.eval()
    preprocessed_sentence = preprocessing_greek(sentence)
    inputs = tokenizer(preprocessed_sentence, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)
    logits = outputs.logits
    probabilities = torch.nn.functional.softmax(logits, dim=1)
    predicted_label = torch.argmax(probabilities, dim=1).item()
    labels_map = {0: 'NOT', 1: 'OFFENSIVE'}
    return labels_map[predicted_label], probabilities.tolist()

# Gradio Interface
iface = gr.Interface(fn=predict, inputs="text", outputs=["text", "json"])
iface.launch()