File size: 1,538 Bytes
e6ba7eb 4dbe18e e6ba7eb 4dbe18e e6ba7eb 4dbe18e e6ba7eb 4dbe18e e6ba7eb 4dbe18e e6ba7eb 4dbe18e e6ba7eb 4dbe18e | 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 | 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'])
|