Tevfik istanbullu commited on
Commit
872437c
1 Parent(s): 1f1ec4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -33
app.py CHANGED
@@ -1,34 +1,10 @@
1
  import gradio as gr
2
  import joblib
3
- import os
4
- import csv
5
- from datetime import datetime
6
-
7
 
8
  model = joblib.load('arabic_text_classifier.pkl')
9
  vectorizer = joblib.load('tfidf_vectorizer.pkl')
10
  label_encoder = joblib.load('label_encoder.pkl')
11
 
12
-
13
- LOG_FILE = "predictions_log.csv"
14
-
15
-
16
- def log_prediction(input_text, predicted_label):
17
-
18
- file_exists = os.path.isfile(LOG_FILE)
19
-
20
-
21
- with open(LOG_FILE, mode='a', newline='', encoding='utf-8') as file:
22
- writer = csv.writer(file)
23
-
24
-
25
- if not file_exists:
26
- writer.writerow(["Timestamp", "User Input", "Predicted Category"])
27
-
28
-
29
- writer.writerow([datetime.now().strftime("%Y-%m-%d %H:%M:%S"), input_text, predicted_label])
30
-
31
-
32
  def predict_category(text):
33
  text_vector = vectorizer.transform([text])
34
  probabilities = model.predict_proba(text_vector)[0]
@@ -37,16 +13,15 @@ def predict_category(text):
37
 
38
 
39
  if max_prob < 0.5:
40
- predicted_label = "Other"
41
- else:
42
- predicted_label = label_encoder.inverse_transform([predicted_category])[0]
43
 
44
- # Log the prediction
45
- log_prediction(text, predicted_label)
46
-
47
- # Return the predicted label
48
  return predicted_label
49
 
 
 
 
50
  iface = gr.Interface(
51
  fn=predict_category,
52
  inputs=gr.Textbox(
@@ -56,8 +31,10 @@ iface = gr.Interface(
56
  ),
57
  outputs=gr.Label(label="Predicted Category"),
58
  title="Arabic Text Classification",
59
- description="Enter an Arabic text to get its classification based on the trained model.",
 
 
60
  )
61
 
62
 
63
- iface.launch()
 
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]
 
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
+ HF_TOKEN = os.getenv("classification")
23
+ hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-text-classification-data")
24
+
25
  iface = gr.Interface(
26
  fn=predict_category,
27
  inputs=gr.Textbox(
 
31
  ),
32
  outputs=gr.Label(label="Predicted Category"),
33
  title="Arabic Text Classification",
34
+ description="Enter an Arabic text to get its classification based on the trained model.",
35
+ allow_flagging="auto",
36
+ flagging_callback=hf_writer,
37
  )
38
 
39
 
40
+ iface.launch(share=True)