| | import streamlit as st |
| | from transformers import pipeline |
| |
|
| | |
| | st.set_page_config(page_title="Text Prompting Demo", layout="centered") |
| | st.title("🤖 Text Prompting using Transformers") |
| |
|
| | |
| | task_option = st.selectbox( |
| | "Select Task", |
| | ("Text Generation", "Text Classification", "Question Answering") |
| | ) |
| |
|
| | |
| | user_input = st.text_area("Enter your input text", height=150) |
| |
|
| | |
| | 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']) |
| |
|