File size: 1,892 Bytes
5482396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
from PIL import Image

# Load Hugging Face models
@st.cache_resource
def load_image_classifier():
    return pipeline("image-classification", model="google/vit-base-patch16-224")

@st.cache_resource
def load_text_classifier():
    return pipeline("sentiment-analysis")  # Default model for sentiment analysis

# Initialize models
image_classifier = load_image_classifier()
text_classifier = load_text_classifier()

# App title and navigation
st.title("Hugging Face Classification App")
st.sidebar.title("Choose Task")
task = st.sidebar.selectbox("Select a task", ["Image Classification", "Text Classification"])

if task == "Image Classification":
    st.header("Image Classification")
    uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
    if uploaded_file is not None:
        # Display uploaded image
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", use_column_width=True)

        # Classify the image
        if st.button("Classify Image"):
            with st.spinner("Classifying..."):
                results = image_classifier(image)
                st.subheader("Classification Results")
                for result in results:
                    st.write(f"**{result['label']}**: {result['score']:.2f}")

elif task == "Text Classification":
    st.header("Text Classification")
    text_input = st.text_area("Enter text for classification", "Streamlit is an amazing tool!")
    
    # Classify the text
    if st.button("Classify Text"):
        with st.spinner("Classifying..."):
            results = text_classifier(text_input)
            st.subheader("Classification Results")
            for result in results:
                st.write(f"**{result['label']}**: {result['score']:.2f}")

st.write("Powered by Streamlit and Hugging Face 🤗")