RamiIbrahim commited on
Commit
60e6eca
1 Parent(s): 64b1a12
Files changed (1) hide show
  1. app.py +18 -31
app.py CHANGED
@@ -1,39 +1,26 @@
1
- import os
2
- import joblib
3
  import gradio as gr
4
- from sklearn.feature_extraction.text import TfidfVectorizer
5
- from sklearn.linear_model import LogisticRegression
6
 
7
- MODEL_PATH = 'tunisian_arabiz_sentiment_analysis_model.pkl'
8
- VECTORIZER_PATH = 'tfidf_vectorizer.pkl'
 
 
9
 
10
- def load_model():
11
- if os.path.exists(MODEL_PATH) and os.path.exists(VECTORIZER_PATH):
12
- model = joblib.load(MODEL_PATH)
13
- vectorizer = joblib.load(VECTORIZER_PATH)
14
- return model, vectorizer
15
- return None, None
16
 
17
- def predict_sentiment(input_text):
18
- model, vectorizer = load_model()
19
- if model and vectorizer:
20
- prediction = model.predict(input_vector)[0]
21
- probabilities = model.predict_proba(input_vector)[0]
22
-
23
- sentiment = "Positive" if prediction == 1 else "Negative"
24
- confidence = probabilities[1] if prediction == 1 else probabilities[0]
25
-
26
- return f"Sentiment: {sentiment}\nConfidence: {confidence:.4f}"
27
- else:
28
- return "Model not found or could not be loaded."
29
-
30
- # Gradio Interface
31
  iface = gr.Interface(
32
- fn=predict_sentiment,
33
- inputs="text",
34
- outputs="text",
35
- title="Sentiment Analysis Predictor",
36
- description="Enter a text to predict its sentiment."
37
  )
38
 
 
39
  iface.launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
 
3
 
4
+ # Load your model and tokenizer
5
+ model_name = "your-model-name"
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
26
  iface.launch()