import streamlit as st from transformers import pipeline # Set up Streamlit UI st.set_page_config(page_title="Text Prompting Demo", layout="centered") st.title("🤖 Text Prompting using Transformers") # Add model selector task_option = st.selectbox( "Select Task", ("Text Generation", "Text Classification", "Question Answering") ) # User input user_input = st.text_area("Enter your input text", height=150) # Run pipeline based on task if st.button("Generate Output"): if not user_input.strip(): st.warning("Please enter some text input.") else: if task_option == "Text Generation": generator = pipeline("text-generation", model="gpt2") output = generator(user_input, max_length=50, num_return_sequences=1) st.subheader("Generated Text") st.write(output[0]['generated_text']) elif task_option == "Text Classification": classifier = pipeline("sentiment-analysis") output = classifier(user_input) st.subheader("Classification Result") st.json(output) elif task_option == "Question Answering": context = st.text_area("Enter context for question answering", height=150) if not context.strip(): st.warning("Please provide context.") else: qa = pipeline("question-answering") result = qa(question=user_input, context=context) st.subheader("Answer") st.write(result['answer'])