File size: 2,143 Bytes
038a896
 
 
 
63183b6
 
 
 
038a896
 
 
cdf1d82
 
 
70b07d3
038a896
 
 
 
 
 
70b07d3
 
 
 
 
 
038a896
 
 
 
 
 
 
 
 
 
70b07d3
 
038a896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63183b6
70b07d3
 
 
63183b6
70b07d3
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import warnings

# Suppress NVML warning
warnings.filterwarnings("ignore", message="Can't initialize NVML")

os.environ["TRANSFORMERS_CACHE"] = "/tmp"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"

MODEL_NAME = "s-nlp/roberta-base-formality-ranker"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)

model = model.to(device)

def calculate_formality_percentages(score):
    grayscale = int(score * 100)
    formal_percent = grayscale
    informal_percent = 100 - grayscale
    return formal_percent, informal_percent

def predict_formality(text):
    # Tokenize input
    encoding = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    encoding = {k: v.to(device) for k, v in encoding.items()}

    # Predict formality score
    with torch.no_grad():
        logits = model(**encoding).logits
    score = logits.softmax(dim=1)[:, 1].item()

    # Calculate percentages using grayscale
    formal_percent, informal_percent = calculate_formality_percentages(score)
    
    response = {
        "formality_score": round(score, 3),
        "formal_percent": formal_percent,
        "informal_percent": informal_percent,
        "classification": f"Your speech is {formal_percent}% formal and {informal_percent}% informal."
    }
    
    return response

demo = gr.Interface(
    fn=predict_formality,
    inputs=gr.Textbox(label="Enter your text", lines=3),
    outputs=gr.JSON(label="Formality Analysis"),
    title="Formality Classifier",
    description="Enter text to analyze its formality level.",
    examples=[
        ["Hello, how are you doing today?"],
        ["Hey, what's up?"],
        ["I would like to request your assistance with this matter."]
    ]
)

# Launch the app
if __name__ == "__main__":
    demo.queue() 
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True  
    )