Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| classifier = pipeline( | |
| "sentiment-analysis", | |
| model="Umranz/distilbert-yelp-sentiment", | |
| tokenizer="Umranz/distilbert-yelp-sentiment" | |
| ) | |
| def predict_single(text): | |
| if not text.strip(): | |
| return "β οΈ Please enter some text." | |
| result = classifier(text)[0] | |
| label = result["label"] | |
| score = result["score"] * 100 | |
| emoji = "π" if label == "POSITIVE" else "π" | |
| return f"{emoji} {label} ({score:.1f}% confidence)" | |
| def predict_batch(text): | |
| if not text.strip(): | |
| return "β οΈ Please enter at least one review." | |
| lines = [l.strip() for l in text.strip().split("\n") if l.strip()] | |
| results = classifier(lines) | |
| output = "" | |
| for line, res in zip(lines, results): | |
| label = res["label"] | |
| score = res["score"] * 100 | |
| emoji = "π" if label == "POSITIVE" else "π" | |
| output += f"{emoji} **{label}** ({score:.1f}%) β {line}\n\n" | |
| return output.strip() | |
| single_tab = gr.Interface( | |
| fn=predict_single, | |
| inputs=gr.Textbox( | |
| lines=4, | |
| placeholder="e.g. The movie was absolutely fantastic!", | |
| label="Your Review" | |
| ), | |
| outputs=gr.Textbox(label="Sentiment Result"), | |
| title="π¬ Sentiment Analyzer", | |
| description="Fine-tuned **DistilBERT** on Yelp reviews. Enter a review to analyze its sentiment.", | |
| examples=[ | |
| ["This movie was absolutely fantastic! The acting was brilliant."], | |
| ["Terrible movie. It was boring and a complete waste of time."], | |
| ["Not bad, actually enjoyed it more than expected."], | |
| ["Best restaurant I've visited in years, amazing food!"], | |
| ["Waited 2 hours and the food was cold. Never going back."], | |
| ], | |
| cache_examples=False | |
| ) | |
| batch_tab = gr.Interface( | |
| fn=predict_batch, | |
| inputs=gr.Textbox( | |
| lines=8, | |
| placeholder="Enter one review per line:\nGreat food!\nWorst experience ever.", | |
| label="Reviews (one per line)" | |
| ), | |
| outputs=gr.Markdown(label="Results"), | |
| title="π Batch Sentiment Analyzer", | |
| description="Analyze multiple reviews at once β one review per line.", | |
| examples=[ | |
| ["Amazing experience!\nTerrible service.\nNot bad overall.\nAbsolutely loved it!"], | |
| ], | |
| cache_examples=False | |
| ) | |
| app = gr.TabbedInterface( | |
| interface_list=[single_tab, batch_tab], | |
| tab_names=["Single Review", "Batch Reviews"] | |
| ) | |
| app.launch() | |