RamiIbrahim commited on
Commit
49f7354
1 Parent(s): 7ae7652

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -17
app.py CHANGED
@@ -1,25 +1,34 @@
1
  import gradio as gr
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
 
3
 
4
- # Load your model and tokenizer
5
- model_name = "tunisian_arabiz_sentiment_analysis_model"
6
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
- def predict(input_text):
10
- # Tokenize and make prediction
11
- inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
12
- outputs = model(**inputs)
13
- prediction = outputs.logits.argmax(-1).item()
14
- return prediction
 
 
 
 
 
 
 
 
 
15
 
16
- # Define Gradio interface
17
  iface = gr.Interface(
18
- fn=predict,
19
- inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
20
- outputs="label",
21
- title="Your Model Name",
22
- description="Enter some text to get a prediction."
23
  )
24
 
25
  # Launch the interface
 
1
  import gradio as gr
2
+ import joblib
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
 
5
+ # Load the saved model and vectorizer
6
+ model = joblib.load('tunisian_arabiz_sentiment_analysis_model.pkl')
7
+ vectorizer = joblib.load('tfidf_vectorizer.pkl')
 
8
 
9
+ def predict_sentiment(text):
10
+ # Transform the input text using the loaded vectorizer
11
+ text_vectorized = vectorizer.transform([text])
12
+
13
+ # Make prediction
14
+ prediction = model.predict(text_vectorized)[0]
15
+
16
+ # Convert prediction to sentiment
17
+ sentiment = "Positive" if prediction == 1 else "Negative"
18
+
19
+ # Get prediction probability
20
+ probabilities = model.predict_proba(text_vectorized)[0]
21
+ confidence = max(probabilities)
22
+
23
+ return f"Sentiment: {sentiment}\nConfidence: {confidence:.2f}"
24
 
25
+ # Create Gradio interface
26
  iface = gr.Interface(
27
+ fn=predict_sentiment,
28
+ inputs=gr.Textbox(lines=3, placeholder="Enter Tunisian Arabiz text here..."),
29
+ outputs="text",
30
+ title="Tunisian Arabiz Sentiment Analysis",
31
+ description="This model predicts the sentiment of Tunisian Arabiz text as either Positive or Negative."
32
  )
33
 
34
  # Launch the interface