Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import pipeline | |
| # Step 1: Load the Hugging Face model | |
| 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 | |
| def load_image_classifier(): | |
| return pipeline("image-classification", model="google/vit-base-patch16-224") | |
| 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 π€") | |