Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,27 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
-
import
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
|
10 |
-
|
11 |
-
st.title("
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
if text:
|
17 |
-
# Tokenize the input text
|
18 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
19 |
|
20 |
-
# Perform text classification
|
21 |
-
with torch.no_grad():
|
22 |
-
outputs = model(**inputs)
|
23 |
-
|
24 |
-
# Get the predicted label and score
|
25 |
-
label_id = torch.argmax(outputs.logits, dim=1).item()
|
26 |
-
label = model.config.id2label[label_id]
|
27 |
-
score = torch.softmax(outputs.logits, dim=1)[0, label_id].item()
|
28 |
-
|
29 |
-
# Display the classification result
|
30 |
-
st.subheader("Classification Result:")
|
31 |
-
st.write(f"Predicted Label: {label}")
|
32 |
-
st.write(f"Confidence Score: {score:.4f}")
|
33 |
-
|
34 |
-
# Sidebar with model information
|
35 |
-
st.sidebar.subheader("Model Information")
|
36 |
-
st.sidebar.write(f"Model: {model_name}")
|
37 |
-
st.sidebar.write(f"Number of Labels: {model.config.num_labels}")
|
38 |
-
|
39 |
-
# Powered by Hugging Face
|
40 |
-
st.write("Powered by Hugging Face's Transformers library.")
|
41 |
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import req
|
5 |
|
6 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
7 |
+
def get_img_from_url(url):
|
8 |
+
return Image.open(requests.get(url, stream=True).raw)
|
|
|
9 |
|
10 |
+
def main():
|
11 |
+
st.title("image-classification")
|
12 |
|
13 |
+
with st.form("image"):
|
14 |
+
|
15 |
+
url = st.text_input("URL to some image", "https://images.livemint.com/img/2022/08/01/600x338/Cat-andriyko-podilnyk-RCfi7vgJjUY-unsplash_1659328989095_1659328998370_1659328998370.jpg")
|
16 |
+
img = get_img_from_url(url)
|
17 |
+
# clicked==True only when the button is clicked
|
18 |
+
clicked = st.form_submit_button("Submit")
|
19 |
+
if clicked:
|
20 |
+
results = classifier(img)
|
21 |
+
st.json(results)
|
22 |
|
|
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
|
26 |
+
if _name_ == "_main_":
|
27 |
+
main()
|