Spaces:
Build error
Build error
Savir Dillikar commited on
Commit ·
e4249bc
1
Parent(s): 3d821f5
Add app file
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from peft import PeftModel, PeftConfig
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
model_checkpoint = 'distilbert-base-uncased'
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
config = PeftConfig.from_pretrained("SavirD/distilbert-base-uncased-lora-text-classification")
|
| 10 |
+
base_model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
|
| 11 |
+
model = PeftModel.from_pretrained(base_model, "SavirD/distilbert-base-uncased-lora-text-classification")
|
| 12 |
+
model.to('mps')
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, add_prefix_space=True)
|
| 15 |
+
if tokenizer.pad_token is None:
|
| 16 |
+
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
| 17 |
+
model.resize_token_embeddings(len(tokenizer))
|
| 18 |
+
|
| 19 |
+
id2label = {0: "Negative", 1: "Positive"}
|
| 20 |
+
|
| 21 |
+
st.markdown("## Enter Some Text")
|
| 22 |
+
review = st.text_area("", height=150)
|
| 23 |
+
|
| 24 |
+
inputs = tokenizer.encode(review, return_tensors="pt").to("mps") # moving to mps for Mac (can alternatively do 'cpu')
|
| 25 |
+
logits = model(inputs).logits
|
| 26 |
+
predictions = torch.max(logits,1).indices
|
| 27 |
+
with st.chat_message("assistant"):
|
| 28 |
+
response = "The Following Text " + f'"{review}" is ' + id2label[predictions.tolist()[0]]
|
| 29 |
+
st.markdown(response)
|