File size: 2,769 Bytes
6a487fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
from transformers import pipeline

# Step 1: Load the Hugging Face model
@st.cache_resource
def load_model():
    return pipeline("text-generation", model="gpt2")  # Replace 'gpt2' with another model if needed

generator = load_model()

# Step 2: Design the Streamlit layout
st.title("Hugging Face Text Generator")
st.write("Generate creative text using GPT-2!")

# Get user input
user_input = st.text_area("Enter a prompt for text generation:", "Once upon a time")

# Generate text when the button is clicked
if st.button("Generate Text"):
    with st.spinner("Generating..."):
        results = generator(user_input, max_length=50, num_return_sequences=1)
        generated_text = results[0]["generated_text"]
        st.subheader("Generated Text:")
        st.write(generated_text)

st.write("Powered by Streamlit and Hugging Face πŸ€—")



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 πŸ€—")