Tevfik istanbullu
Update app.py
7fdacf0 verified
raw
history blame
2.56 kB
import gradio as gr
import joblib
model = joblib.load('arabic_text_classifier.pkl')
vectorizer = joblib.load('tfidf_vectorizer.pkl')
label_encoder = joblib.load('label_encoder.pkl')
def predict_category(text):
text_vector = vectorizer.transform([text])
probabilities = model.predict_proba(text_vector)[0]
max_prob = max(probabilities)
predicted_category = model.predict(text_vector)[0]
if max_prob < 0.5:
return "Other"
predicted_label = label_encoder.inverse_transform([predicted_category])[0]
return predicted_label
iface = gr.Interface(
fn=predict_category,
inputs=gr.Textbox(
lines=5,
placeholder="Enter text in Arabic here...",
label="Text"
),
outputs=gr.Label(label="Predicted Category"),
title="Arabic Text Classification",
description="Enter an Arabic text to get its classification based on the trained model.",
)
iface.launch(share=True)
import gradio as gr
import joblib
import os
import csv
from datetime import datetime
model = joblib.load('arabic_text_classifier.pkl')
vectorizer = joblib.load('tfidf_vectorizer.pkl')
label_encoder = joblib.load('label_encoder.pkl')
LOG_FILE = "predictions_log.csv"
def log_prediction(input_text, predicted_label):
file_exists = os.path.isfile(LOG_FILE)
with open(LOG_FILE, mode='a', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
if not file_exists:
writer.writerow(["Timestamp", "User Input", "Predicted Category"])
writer.writerow([datetime.now().strftime("%Y-%m-%d %H:%M:%S"), input_text, predicted_label])
def predict_category(text):
text_vector = vectorizer.transform([text])
probabilities = model.predict_proba(text_vector)[0]
max_prob = max(probabilities)
predicted_category = model.predict(text_vector)[0]
if max_prob < 0.5:
predicted_label = "Other"
else:
predicted_label = label_encoder.inverse_transform([predicted_category])[0]
# Log the prediction
log_prediction(text, predicted_label)
# Return the predicted label
return predicted_label
iface = gr.Interface(
fn=predict_category,
inputs=gr.Textbox(
lines=5,
placeholder="Enter text in Arabic here...",
label="Text"
),
outputs=gr.Label(label="Predicted Category"),
title="Arabic Text Classification",
description="Enter an Arabic text to get its classification based on the trained model.",
)
iface.launch()