Add app, model, and requirements
Browse files- app.py +38 -0
- random_forest_model.pkl +3 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import XLNetTokenizer, XLNetModel
|
| 5 |
+
import numpy as np
|
| 6 |
+
import joblib
|
| 7 |
+
|
| 8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 9 |
+
tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
|
| 10 |
+
xlnet_model = XLNetModel.from_pretrained('xlnet-base-cased').to(device)
|
| 11 |
+
|
| 12 |
+
random_forest_classifier = joblib.load("random_forest_model.pkl")
|
| 13 |
+
|
| 14 |
+
def get_embedding(text):
|
| 15 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512).to(device)
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = xlnet_model(**inputs)
|
| 18 |
+
return outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
|
| 19 |
+
|
| 20 |
+
def predict_complexity(sentence, target_word):
|
| 21 |
+
try:
|
| 22 |
+
sentence_embedding = get_embedding(sentence)
|
| 23 |
+
word_embedding = get_embedding(target_word)
|
| 24 |
+
combined_embedding = np.concatenate([sentence_embedding, word_embedding]).reshape(1, -1)
|
| 25 |
+
prediction = random_forest_classifier.predict(combined_embedding)[0]
|
| 26 |
+
return f"🔍 Predicted Complexity Level: **{prediction}**"
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"❌ Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown("## ✨ Word Complexity Predictor")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
sentence_input = gr.Textbox(label="Full Sentence", placeholder="Type a full sentence...")
|
| 34 |
+
word_input = gr.Textbox(label="Target Word", placeholder="Type the target word...")
|
| 35 |
+
output = gr.Markdown()
|
| 36 |
+
gr.Button("Predict Complexity").click(predict_complexity, [sentence_input, word_input], output)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|
random_forest_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b52464cc9f5eb892d0fd9de1cabf7a101df5131a3adba20fdcd00798f4740f74
|
| 3 |
+
size 38174881
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
scikit-learn
|
| 6 |
+
pandas
|
| 7 |
+
numpy
|