File size: 1,396 Bytes
b8b150c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
import gradio as gr
import torch
from model import SimpleMultilingualClassifier  # Import your model

# --- Configuration ---
embedding_files = {
    'en': 'fasttext_embeddings/cc.en.100.bin',
    'fr': 'fasttext_embeddings/cc.fr.100.bin'
    # Add more languages as needed
}
num_classes = 3  # Replace with the actual number of classes
class_labels = ["positive", "negative", "neutral"] # Replace with your actual class labels

# Load the model
try:
    model = SimpleMultilingualClassifier(embedding_files, num_classes)
    # In a real scenario, you would load trained weights here:
    # model.load_state_dict(torch.load('path/to/your/trained_weights.pth'))
    model.eval()
except Exception as e:
    print(f"Error loading model: {e}")
    model = None

def classify_text(text, language):
    if model:
        try:
            prediction = model.predict(text, language, class_labels)
            return prediction
        except ValueError as e:
            return str(e)
    else:
        return "Model not loaded."

iface = gr.Interface(
    fn=classify_text,
    inputs=[
        gr.Textbox(label="Enter text"),
        gr.Dropdown(choices=list(embedding_files.keys()), label="Language")
    ],
    outputs=gr.Textbox(label="Prediction"),
    title="Simple Multilingual Text Classifier",
    description="A basic multilingual text classifier using FastText embeddings.",
)

iface.launch()