CoolFace
Apppublic

Pokie26/Basic_bert_finetuning_practical_1

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1import streamlit as st2from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification3import torch4import torch.nn.functional as F5 6import zipfile7import shutil8import os9 10def unzip_and_save(zip_file_path, extraction_path):11    # Create the extraction directory if it doesn't exist12    os.makedirs(extraction_path, exist_ok=True)13 14    with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:15        folder_name = os.path.basename(zip_file_path).split('.')[0]16        zip_ref.extractall(extraction_path)17        source_path = os.path.join(extraction_path, folder_name)18        destination_path = os.path.join(extraction_path, folder_name)19        if os.path.exists(destination_path):20            print(f"Error: Destination path '{destination_path}' already exists")21        else:22            shutil.move(source_path, destination_path)23 24# Example usage:25# Path to your ZIP file which is your sentimetn analysis model zip26zip_file_path = 'bert_model_dir.zip'  27# Destination folder for extraction28extraction_path = 'bert_model_sentiment_v1'  29 30unzip_and_save(zip_file_path, extraction_path)31 32# Load the fine-tuned model and tokenizer33model_path = "bert_model_sentiment_v1/bert_model_dir"34tokenizer_path = "bert_model_sentiment_v1/bert_model_dir"35 36@st.cache_resource37def load_model():38    model = DistilBertForSequenceClassification.from_pretrained(model_path)39    tokenizer = DistilBertTokenizerFast.from_pretrained(tokenizer_path)40    return model, tokenizer41 42model, tokenizer = load_model()43 44def predict_sentiment(text):45    device = 'cuda' if torch.cuda.is_available() else 'cpu'46    model.to(device)47 48    tokenized = tokenizer(text, truncation=True, padding=True, return_tensors='pt').to(device)49    outputs = model(**tokenized)50 51    probs = F.softmax(outputs.logits, dim=-1)52    preds = torch.argmax(outputs.logits, dim=-1).item()53    probs_max = probs.max().detach().cpu().numpy()54 55    prediction = "Positive" if preds == 1 else "Negative"56    return prediction, probs_max * 10057 58st.title("Sentiment Analysis App")59text = st.text_area("Enter your text:")60 61if st.button("Predict Sentiment"):62    if text:63        sentiment, confidence = predict_sentiment(text)64        st.write(f"Sentiment: {sentiment}")65        st.write(f"Confidence: {confidence:.2f}%")66    else:67        st.write("Please enter some text.")