Tevfik istanbullu
commited on
Commit
•
7fdacf0
1
Parent(s):
2114539
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,101 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import joblib
|
3 |
-
|
4 |
-
model = joblib.load('arabic_text_classifier.pkl')
|
5 |
-
vectorizer = joblib.load('tfidf_vectorizer.pkl')
|
6 |
-
label_encoder = joblib.load('label_encoder.pkl')
|
7 |
-
|
8 |
-
def predict_category(text):
|
9 |
-
text_vector = vectorizer.transform([text])
|
10 |
-
probabilities = model.predict_proba(text_vector)[0]
|
11 |
-
max_prob = max(probabilities)
|
12 |
-
predicted_category = model.predict(text_vector)[0]
|
13 |
-
|
14 |
-
|
15 |
-
if max_prob < 0.5:
|
16 |
-
return "Other"
|
17 |
-
|
18 |
-
|
19 |
-
predicted_label = label_encoder.inverse_transform([predicted_category])[0]
|
20 |
-
return predicted_label
|
21 |
-
|
22 |
-
|
23 |
-
iface = gr.Interface(
|
24 |
-
fn=predict_category,
|
25 |
-
inputs=gr.Textbox(
|
26 |
-
lines=5,
|
27 |
-
placeholder="Enter text in Arabic here...",
|
28 |
-
label="Text"
|
29 |
-
),
|
30 |
-
outputs=gr.Label(label="Predicted Category"),
|
31 |
-
title="Arabic Text Classification",
|
32 |
-
description="Enter an Arabic text to get its classification based on the trained model.",
|
33 |
-
)
|
34 |
-
|
35 |
-
|
36 |
-
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import joblib
|
3 |
+
|
4 |
+
model = joblib.load('arabic_text_classifier.pkl')
|
5 |
+
vectorizer = joblib.load('tfidf_vectorizer.pkl')
|
6 |
+
label_encoder = joblib.load('label_encoder.pkl')
|
7 |
+
|
8 |
+
def predict_category(text):
|
9 |
+
text_vector = vectorizer.transform([text])
|
10 |
+
probabilities = model.predict_proba(text_vector)[0]
|
11 |
+
max_prob = max(probabilities)
|
12 |
+
predicted_category = model.predict(text_vector)[0]
|
13 |
+
|
14 |
+
|
15 |
+
if max_prob < 0.5:
|
16 |
+
return "Other"
|
17 |
+
|
18 |
+
|
19 |
+
predicted_label = label_encoder.inverse_transform([predicted_category])[0]
|
20 |
+
return predicted_label
|
21 |
+
|
22 |
+
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=predict_category,
|
25 |
+
inputs=gr.Textbox(
|
26 |
+
lines=5,
|
27 |
+
placeholder="Enter text in Arabic here...",
|
28 |
+
label="Text"
|
29 |
+
),
|
30 |
+
outputs=gr.Label(label="Predicted Category"),
|
31 |
+
title="Arabic Text Classification",
|
32 |
+
description="Enter an Arabic text to get its classification based on the trained model.",
|
33 |
+
)
|
34 |
+
|
35 |
+
|
36 |
+
iface.launch(share=True)
|
37 |
+
|
38 |
+
|
39 |
+
import gradio as gr
|
40 |
+
import joblib
|
41 |
+
import os
|
42 |
+
import csv
|
43 |
+
from datetime import datetime
|
44 |
+
|
45 |
+
|
46 |
+
model = joblib.load('arabic_text_classifier.pkl')
|
47 |
+
vectorizer = joblib.load('tfidf_vectorizer.pkl')
|
48 |
+
label_encoder = joblib.load('label_encoder.pkl')
|
49 |
+
|
50 |
+
|
51 |
+
LOG_FILE = "predictions_log.csv"
|
52 |
+
|
53 |
+
|
54 |
+
def log_prediction(input_text, predicted_label):
|
55 |
+
|
56 |
+
file_exists = os.path.isfile(LOG_FILE)
|
57 |
+
|
58 |
+
|
59 |
+
with open(LOG_FILE, mode='a', newline='', encoding='utf-8') as file:
|
60 |
+
writer = csv.writer(file)
|
61 |
+
|
62 |
+
|
63 |
+
if not file_exists:
|
64 |
+
writer.writerow(["Timestamp", "User Input", "Predicted Category"])
|
65 |
+
|
66 |
+
|
67 |
+
writer.writerow([datetime.now().strftime("%Y-%m-%d %H:%M:%S"), input_text, predicted_label])
|
68 |
+
|
69 |
+
|
70 |
+
def predict_category(text):
|
71 |
+
text_vector = vectorizer.transform([text])
|
72 |
+
probabilities = model.predict_proba(text_vector)[0]
|
73 |
+
max_prob = max(probabilities)
|
74 |
+
predicted_category = model.predict(text_vector)[0]
|
75 |
+
|
76 |
+
|
77 |
+
if max_prob < 0.5:
|
78 |
+
predicted_label = "Other"
|
79 |
+
else:
|
80 |
+
predicted_label = label_encoder.inverse_transform([predicted_category])[0]
|
81 |
+
|
82 |
+
# Log the prediction
|
83 |
+
log_prediction(text, predicted_label)
|
84 |
+
|
85 |
+
# Return the predicted label
|
86 |
+
return predicted_label
|
87 |
+
|
88 |
+
iface = gr.Interface(
|
89 |
+
fn=predict_category,
|
90 |
+
inputs=gr.Textbox(
|
91 |
+
lines=5,
|
92 |
+
placeholder="Enter text in Arabic here...",
|
93 |
+
label="Text"
|
94 |
+
),
|
95 |
+
outputs=gr.Label(label="Predicted Category"),
|
96 |
+
title="Arabic Text Classification",
|
97 |
+
description="Enter an Arabic text to get its classification based on the trained model.",
|
98 |
+
)
|
99 |
+
|
100 |
+
|
101 |
+
iface.launch()
|