saad1BM's picture
Update app.py
5dcdd88 verified
import pandas as pd
import gradio as gr
from textblob import TextBlob
import re
def preprocess_text(text):
if not isinstance(text, str):
return ""
text = text.lower()
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
text = re.sub(r'[^\w\s]', '', text)
return text.strip()
def predict_sentiment(user_input):
cleaned_text = preprocess_text(user_input)
if not cleaned_text:
return "Please enter actual text"
# TextBlob analysis
blob = TextBlob(cleaned_text)
polarity = blob.sentiment.polarity
# Classification based on polarity
if polarity > 0.1:
return "Positive"
elif polarity < -0.1:
return "Negative"
else:
return "Neutral"
def get_dataset_info():
try:
df = pd.read_csv('sentiment_analysis.csv')
summary = f"Dataset loaded! Total rows: {len(df)}. Columns: {', '.join(df.columns)}"
return summary
except:
return "error."
with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Text",
placeholder="Type anything",
lines=4
)
submit_btn = gr.Button("Analyze Setiment")
with gr.Column():
output_label = gr.Label(label="Predicted result")
submit_btn.click(fn=predict_sentiment, inputs=input_text, outputs=output_label)
if __name__ == "__main__":
demo.launch()