Upload 2 files
Browse files- app.py +48 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load models
|
| 6 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 7 |
+
classifier = pipeline("sentiment-analysis", device=device)
|
| 8 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
|
| 9 |
+
|
| 10 |
+
def analyze_sentiment(text):
|
| 11 |
+
if not text.strip():
|
| 12 |
+
return {"Error": 1.0}
|
| 13 |
+
result = classifier(text)[0]
|
| 14 |
+
return {
|
| 15 |
+
f"{result['label']} {'😊' if result['label'] == 'POSITIVE' else '😠'}": result['score'],
|
| 16 |
+
"Other": 1 - result['score']
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def summarize(text, max_len, min_len):
|
| 20 |
+
if len(text) < 50:
|
| 21 |
+
return "Please enter at least 50 characters."
|
| 22 |
+
result = summarizer(text, max_length=max_len, min_length=min_len)
|
| 23 |
+
return result[0]['summary_text']
|
| 24 |
+
|
| 25 |
+
with gr.Blocks(title="NLP Toolkit") as app:
|
| 26 |
+
gr.Markdown("# 🛠️ NLP Toolkit")
|
| 27 |
+
|
| 28 |
+
with gr.Tabs():
|
| 29 |
+
with gr.TabItem("Sentiment"):
|
| 30 |
+
gr.Interface(
|
| 31 |
+
fn=analyze_sentiment,
|
| 32 |
+
inputs=gr.Textbox(lines=3),
|
| 33 |
+
outputs=gr.Label(),
|
| 34 |
+
examples=[["I love this!"], ["This is terrible."]]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
with gr.TabItem("Summarize"):
|
| 38 |
+
gr.Interface(
|
| 39 |
+
fn=summarize,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(lines=6),
|
| 42 |
+
gr.Slider(50, 200, value=100),
|
| 43 |
+
gr.Slider(20, 80, value=30)
|
| 44 |
+
],
|
| 45 |
+
outputs=gr.Textbox()
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|