File size: 1,755 Bytes
03a9964
 
 
 
 
 
 
 
3176c9b
 
 
 
 
 
 
7658c25
3176c9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
import subprocess
import sys

try:
    import sentencepiece
except ImportError:
    subprocess.check_call([sys.executable, "-m", "pip", "install", "sentencepiece"])
    import sentencepiece

import gradio as gr
import torch
from transformers import XLNetTokenizer, XLNetModel
import numpy as np
import joblib


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
xlnet_model = XLNetModel.from_pretrained('xlnet-base-cased').to(device)

random_forest_classifier = joblib.load("random_forest_model.pkl")

def get_embedding(text):
    inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512).to(device)
    with torch.no_grad():
        outputs = xlnet_model(**inputs)
    return outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()

def predict_complexity(sentence, target_word):
    try:
        sentence_embedding = get_embedding(sentence)
        word_embedding = get_embedding(target_word)
        combined_embedding = np.concatenate([sentence_embedding, word_embedding]).reshape(1, -1)
        prediction = random_forest_classifier.predict(combined_embedding)[0]
        return f"🔍 Predicted Complexity Level: **{prediction}**"
    except Exception as e:
        return f"❌ Error: {str(e)}"

with gr.Blocks() as demo:
    gr.Markdown("## ✨ Word Complexity Predictor")
    with gr.Row():
        sentence_input = gr.Textbox(label="Full Sentence", placeholder="Type a full sentence...")
        word_input = gr.Textbox(label="Target Word", placeholder="Type the target word...")
    output = gr.Markdown()
    gr.Button("Predict Complexity").click(predict_complexity, [sentence_input, word_input], output)

demo.launch()