|
import gradio as gr |
|
import cloudpickle |
|
import codecs |
|
import string |
|
from bnltk.tokenize import Tokenizers |
|
|
|
|
|
model = None |
|
tfidf_vectorizer = None |
|
tokenizer = None |
|
bangla_stopwords = None |
|
punctuation_marks = None |
|
|
|
def load_models_and_components(): |
|
"""Load the saved model, vectorizer, and preprocessing components""" |
|
global model, tfidf_vectorizer, tokenizer, bangla_stopwords, punctuation_marks |
|
|
|
try: |
|
|
|
with open('model.pkl', 'rb') as f: |
|
model = cloudpickle.load(f) |
|
|
|
|
|
with open('tfidf_VECt.pkl', 'rb') as f: |
|
tfidf_vectorizer = cloudpickle.load(f) |
|
|
|
|
|
tokenizer = Tokenizers() |
|
|
|
|
|
stopwords_list = "stopwords.txt" |
|
bangla_stopwords = codecs.open(stopwords_list, 'r', encoding='utf-8').read().split() |
|
|
|
|
|
punctuation_marks = set(string.punctuation) |
|
|
|
return "Models and components loaded successfully!" |
|
|
|
except Exception as e: |
|
return f"Error loading models: {str(e)}" |
|
|
|
def preprocess_text(text): |
|
"""Preprocess the input text similar to training data preprocessing""" |
|
|
|
words = tokenizer.bn_word_tokenizer(text) |
|
|
|
|
|
words_no_punct = [word for word in words if word not in punctuation_marks] |
|
|
|
|
|
words_clean = [word for word in words_no_punct if word not in bangla_stopwords] |
|
|
|
|
|
return ' '.join(words_clean) |
|
|
|
def predict_sentiment(input_text): |
|
"""Predict sentiment for the input text""" |
|
if not input_text.strip(): |
|
return "Please enter some text to analyze.", "" |
|
|
|
if model is None or tfidf_vectorizer is None: |
|
return "Models not loaded. Please load models first.", "" |
|
|
|
try: |
|
|
|
processed_text = preprocess_text(input_text) |
|
|
|
if not processed_text.strip(): |
|
return "After preprocessing, no valid words found. Please try different text.", "" |
|
|
|
|
|
transformed_input = tfidf_vectorizer.transform([processed_text]) |
|
|
|
|
|
prediction = model.predict(transformed_input)[0] |
|
|
|
|
|
prediction_proba = model.predict_proba(transformed_input)[0] |
|
confidence = max(prediction_proba) * 100 |
|
|
|
|
|
sentiment = "Positive 😊" if prediction == 1 else "Negative 😞" |
|
|
|
|
|
result = f"**Sentiment:** {sentiment}\n**Confidence:** {confidence:.2f}%" |
|
|
|
|
|
details = f"**Processed Text:** {processed_text}\n**Raw Prediction:** {prediction}\n**Probabilities:** Negative: {prediction_proba[0]:.3f}, Positive: {prediction_proba[1]:.3f}" |
|
|
|
return result, details |
|
|
|
except Exception as e: |
|
return f"Error during prediction: {str(e)}", "" |
|
|
|
def create_gradio_interface(): |
|
"""Create and configure the Gradio interface""" |
|
|
|
|
|
css = """ |
|
.gradio-container { |
|
font-family: 'Arial', sans-serif; |
|
} |
|
.main-header { |
|
text-align: center; |
|
color: #2d3748; |
|
margin-bottom: 20px; |
|
} |
|
.prediction-box { |
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|
color: white; |
|
padding: 15px; |
|
border-radius: 10px; |
|
margin: 10px 0; |
|
} |
|
""" |
|
|
|
with gr.Blocks(css=css, title="Bengali Sentiment Analysis", theme=gr.themes.Ocean()) as demo: |
|
gr.HTML(""" |
|
<div class="main-header"> |
|
<h1>🇧🇩 Bengali Sentiment Analysis</h1> |
|
<p>Analyze the sentiment of Bengali text using machine learning</p> |
|
</div> |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=2): |
|
|
|
gr.Markdown("### 📝 Enter Bengali Text") |
|
input_text = gr.Textbox( |
|
label="Bengali Text", |
|
placeholder="এখানে বাংলা টেক্সট লিখুন... (Enter Bengali text here...)", |
|
lines=4, |
|
max_lines=8 |
|
) |
|
|
|
with gr.Row(): |
|
predict_btn = gr.Button("🔍 Analyze Sentiment", variant="primary", size="lg") |
|
clear_btn = gr.Button("🗑️ Clear", variant="secondary") |
|
|
|
|
|
gr.Markdown("### ⚙️ Model Management") |
|
load_btn = gr.Button("📥 Load Models", variant="secondary") |
|
load_status = gr.Textbox(label="Load Status", interactive=False) |
|
|
|
with gr.Column(scale=2): |
|
|
|
gr.Markdown("### 📊 Results") |
|
output_sentiment = gr.Markdown(label="Sentiment Analysis Result") |
|
output_details = gr.Textbox( |
|
label="Analysis Details", |
|
lines=6, |
|
interactive=False |
|
) |
|
|
|
|
|
gr.Markdown("### 💡 Example Texts to Try") |
|
gr.Examples( |
|
examples=[ |
|
["এই পণ্যটি অসাধারণ! আমি খুবই সন্তুষ্ট।"], |
|
["এই পণ্যটি কাজ করছে না। খুবই খারাপ।"], |
|
["দারুণ সার্ভিস! দ্রুত ডেলিভারি পেয়েছি।"], |
|
["প্রোডাক্ট কোয়ালিটি ভালো না। টাকার অপচয়।"], |
|
["চমৎকার অভিজ্ঞতা! আবার কিনব।"] |
|
], |
|
inputs=[input_text], |
|
label="Click on any example to try it" |
|
) |
|
|
|
|
|
predict_btn.click( |
|
fn=predict_sentiment, |
|
inputs=[input_text], |
|
outputs=[output_sentiment, output_details] |
|
) |
|
|
|
clear_btn.click( |
|
fn=lambda: ("", "", ""), |
|
outputs=[input_text, output_sentiment, output_details] |
|
) |
|
|
|
load_btn.click( |
|
fn=load_models_and_components, |
|
outputs=[load_status] |
|
) |
|
|
|
|
|
gr.HTML(""" |
|
<div style="text-align: center; margin-top: 30px; padding: 20px; background-color: #f8f9fa; border-radius: 10px;"> |
|
<p><strong>Bengali Sentiment Analysis App</strong></p> |
|
<p>Powered by SVM with TF-IDF features | Built with Gradio</p> |
|
<p><em>Load the models first, then enter Bengali text to analyze sentiment</em></p> |
|
<p><a href="https://drive.google.com/file/d/1VnvNSO2q-qd7SJya3mX7Er2U_5hEyBVl/view?usp=sharing" target="_blank" style="color: #667eea; text-decoration: none; font-weight: bold;">📄 Paper Link</a></p> |
|
</div> |
|
""") |
|
|
|
return demo |
|
|
|
def main(): |
|
"""Main function to run the Gradio app""" |
|
print("Starting Bengali Sentiment Analysis App...") |
|
print("Make sure you have the following files in the specified paths:") |
|
print("- model.pkl") |
|
print("- tfidf_VECt.pkl") |
|
print("- stopwords.txt") |
|
|
|
|
|
demo = create_gradio_interface() |
|
|
|
|
|
demo.launch( |
|
share=True, |
|
inbrowser=True, |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
show_error=True |
|
) |
|
|
|
if __name__ == "__main__": |
|
|
|
try: |
|
import gradio |
|
except ImportError: |
|
print("Installing Gradio...") |
|
import subprocess |
|
subprocess.check_call(["pip", "install", "gradio"]) |
|
|
|
main() |