Upload 2 files
Browse files- app.py +39 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from gradio.components import Textbox
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load the sentiment analysis pipeline with DistilBERT
|
| 10 |
+
distilbert_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 11 |
+
label_map = {"POSITIVE":"OTHER", "NEGATIVE":"SENSITIVE"}
|
| 12 |
+
|
| 13 |
+
input1 = Textbox(lines=2, placeholder="Type your text here...")
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
async def root():
|
| 17 |
+
def predict_sentiment(text):
|
| 18 |
+
"""
|
| 19 |
+
Predicts the sentiment of the input text using DistilBERT.
|
| 20 |
+
:param text: str, input text to analyze.
|
| 21 |
+
:return: str, predicted sentiment and confidence score.
|
| 22 |
+
"""
|
| 23 |
+
result = distilbert_pipeline(text)[0]
|
| 24 |
+
label = label_map[result['label']]
|
| 25 |
+
score = result['score']
|
| 26 |
+
return f"TAG: {label}, Confidence: {score:.2f}"
|
| 27 |
+
|
| 28 |
+
# Create a Gradio interface
|
| 29 |
+
text_input = gr.Interface(fn=predict_sentiment,
|
| 30 |
+
inputs=input1,
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="Talk2Loop Sensitive statement tags",
|
| 33 |
+
description="This model predicts the sensitivity of the input text. Enter a sentence to see if it's sensitive or not.")
|
| 34 |
+
|
| 35 |
+
return text_input.launch(share=True, host="0.0.0.0", port=8000)
|
| 36 |
+
|
| 37 |
+
# Launch the interface
|
| 38 |
+
app = gr.mount_gradio_app(app, text_input, path="/")
|
| 39 |
+
# iface.launch(port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
gunicorn
|
| 4 |
+
gradio
|
| 5 |
+
torch
|
| 6 |
+
transformers
|