File size: 2,256 Bytes
3aafe68
 
 
2e5be48
3aafe68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

model_name = "amiguel/fintune_naming_model"  # Replace with your model repo
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

def classify_review(text, model, tokenizer, device, max_length=512):
    model.eval()
    inputs = tokenizer.encode_plus(
        text,
        truncation=True,
        padding='max_length',
        max_length=max_length,
        return_tensors="pt"
    )
    input_ids = inputs['input_ids'].to(device)
    attention_mask = inputs['attention_mask'].to(device)

    with torch.no_grad():
        outputs = model(input_ids, attention_mask=attention_mask)
        logits = outputs.logits
    predicted_label = torch.argmax(logits, dim=-1).item()
    return "Proper Naming otfcn" if predicted_label == 1 else "Wrong Naming notfcn"

def main():
    st.title("Notifications Naming Classifier")

    input_option = st.radio("Select input option", ("Single Text Query", "Upload Table"))

    if input_option == "Single Text Query":
        text_query = st.text_input("Enter text query")
        if st.button("Classify"):
            if text_query:
                predicted_label = classify_review(text_query, model, tokenizer, device)
                st.write("Predicted Label:")
                st.write(predicted_label)
            else:
                st.warning("Please enter a text query.")

    elif input_option == "Upload Table":
        uploaded_file = st.file_uploader("Choose a file", type=["csv", "xlsx"])
        if uploaded_file is not None:
            import pandas as pd
            if uploaded_file.name.endswith(".csv"):
                df = pd.read_csv(uploaded_file)
            else:
                df = pd.read_excel(uploaded_file)

            text_column = st.selectbox("Select the text column", df.columns)
            predicted_labels = [classify_review(text, model, tokenizer, device) for text in df[text_column]]
            df["Predicted Label"] = predicted_labels
            st.write(df) 

if __name__ == "__main__":
    main()