File size: 2,560 Bytes
7fdacf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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()