Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
# Ensure required packages are installed at startup
|
| 4 |
+
for pkg in ["transformers", "torch", "gradio"]:
|
| 5 |
+
subprocess.check_call([os.sys.executable, "-m", "pip", "install", pkg])
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Load the Hugging Face sentiment analysis pipeline
|
| 13 |
+
classifier = pipeline("sentiment-analysis")
|
| 14 |
+
|
| 15 |
+
# Define a function to process inputs and return a readable result
|
| 16 |
+
def analyze(text):
|
| 17 |
+
if not text:
|
| 18 |
+
return "Please enter some text."
|
| 19 |
+
res = classifier(text)[0]
|
| 20 |
+
# Format the label and confidence nicely
|
| 21 |
+
label = res["label"]
|
| 22 |
+
score = res["score"] * 100
|
| 23 |
+
return f"{label} ({score:.1f}%)"
|
| 24 |
+
|
| 25 |
+
# Create and launch the Gradio interface
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=analyze,
|
| 28 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence…"),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="Sentiment Analysis App",
|
| 31 |
+
description="Enter text and click 'Submit' to see sentiment results."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if _name_ == "_main_":
|
| 35 |
+
iface.launch()
|