File size: 1,936 Bytes
6e55b8d
9a5c0c6
 
46f4682
 
9a5c0c6
 
 
 
 
 
46f4682
 
9a5c0c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import gdown
import os

# Set the title of the Streamlit app
st.title("Text Classification with Hugging Face Transformers")

# Function to download the model from Google Drive
def download_model_from_drive(file_id, dest_path):
    url = f'https://drive.google.com/uc?id={file_id}'
    gdown.download(url, dest_path, quiet=False)

# Download the model files
with st.spinner("Downloading model..."):
    download_model_from_drive('1-V2bEtPR9Y3iBXK9zOR-qM5y9hKiQUnF', 'model/model.safetensors')
    download_model_from_drive('1-T2etSP_k_3j5LzunWq8viKGQCQ5RMr_', 'model/config.json')
    download_model_from_drive('1-cRYNPWqlNNGRxeztympRRfVuy3hWuMY', 'model/tokenizer.json')
    download_model_from_drive('1-t9AhomeH7YIIpAqCGTok8wjvl0tml0F', 'model/vocab.json')
    download_model_from_drive('1-l77_KEdK7GBFjMX_6UXGE-ZTGDraaDm', 'model/merges.txt')

# Load the model and tokenizer
@st.cache(allow_output_mutation=True)
def load_model_and_tokenizer():
    tokenizer = AutoTokenizer.from_pretrained('model')
    # For Safetensors, you might need a custom loading mechanism
    model = AutoModelForSequenceClassification.from_pretrained('model', use_safetensors=True)  # Adjust if necessary
    return tokenizer, model

tokenizer, model = load_model_and_tokenizer()

# Input text from user
input_text = st.text_area("Enter the text to classify:")

if st.button("Classify"):
    if input_text:
        # Tokenize the input text
        inputs = tokenizer(input_text, return_tensors="pt")
        
        # Perform classification
        with torch.no_grad():
            outputs = model(**inputs)
        
        # Get the predicted class
        predicted_class = torch.argmax(outputs.logits, dim=1).item()

        st.write(f"Predicted Class: {predicted_class}")
    else:
        st.write("Please enter some text to classify.")