File size: 1,530 Bytes
5dcdd88 681f270 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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() |