Spaces:
Build error
Build error
Tevfik istanbullu
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from datasets import Dataset, DatasetDict, load_dataset
|
| 4 |
+
|
| 5 |
+
model = joblib.load('arabic_text_classifier.pkl')
|
| 6 |
+
vectorizer = joblib.load('tfidf_vectorizer.pkl')
|
| 7 |
+
label_encoder = joblib.load('label_encoder.pkl')
|
| 8 |
+
|
| 9 |
+
def predict_category(text):
|
| 10 |
+
text_vector = vectorizer.transform([text])
|
| 11 |
+
probabilities = model.predict_proba(text_vector)[0]
|
| 12 |
+
max_prob = max(probabilities)
|
| 13 |
+
predicted_category = model.predict(text_vector)[0]
|
| 14 |
+
|
| 15 |
+
if max_prob < 0.5:
|
| 16 |
+
return "Other"
|
| 17 |
+
|
| 18 |
+
predicted_label = label_encoder.inverse_transform([predicted_category])[0]
|
| 19 |
+
return predicted_label
|
| 20 |
+
|
| 21 |
+
def flag_data(text, prediction):
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
dataset = load_dataset("Tevfik34/crowdsourced-text-classification-data", split="train")
|
| 25 |
+
except:
|
| 26 |
+
|
| 27 |
+
dataset = Dataset.from_dict({"text": [], "prediction": []})
|
| 28 |
+
|
| 29 |
+
new_data = {"text": [text], "prediction": [prediction]}
|
| 30 |
+
dataset = dataset.add_item(new_data)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
dataset.push_to_hub("Tevfik34/crowdsourced-text-classification-data")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def classify_and_flag(text):
|
| 37 |
+
prediction = predict_category(text)
|
| 38 |
+
flag_data(text, prediction)
|
| 39 |
+
return prediction
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
interface = gr.Interface(fn=classify_and_flag, inputs=gr.Textbox(lines=5, placeholder= "Enter text in Arabic here...", label="Text" ), outputs=gr.Label(label="text"),
|
| 43 |
+
title="Arabic Text Classifier", description="Classify Arabic text into categories bu using Logistic Regression")
|
| 44 |
+
|
| 45 |
+
interface.launch()
|