Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,42 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 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 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
# Import the high-level pipeline API from Hugging Face Transformers
|
| 3 |
+
# It simplifies loading models/tokenizers and running common tasks
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# 1. Cache the pipeline so it loads once
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def get_generator():
|
| 9 |
+
# Initialize a text-to-text generation pipeline:
|
| 10 |
+
# - "text2text-generation" tells the pipeline we want a seq2seq model (T5 family)
|
| 11 |
+
# - model="google/flan-t5-small" specifies which pretrained model to load
|
| 12 |
+
# The pipeline object wraps both tokenizer and model for you.
|
| 13 |
+
return pipeline("text2text-generation", model="google/flan-t5-small", use_auth_token=True)
|
| 14 |
+
|
| 15 |
+
generator = get_generator()
|
| 16 |
+
|
| 17 |
+
st.title("📝 FLAN-T5 Text-to-Text Generator")
|
| 18 |
+
st.write("Enter a prompt below and hit Generate to see the model’s output.")
|
| 19 |
+
|
| 20 |
+
# 2. Prompt the user for input
|
| 21 |
+
user_input = st.text_area("Your prompt:", height=120)
|
| 22 |
+
|
| 23 |
+
# 3. Generation settings in the sidebar
|
| 24 |
+
with st.sidebar:
|
| 25 |
+
st.header("Generation Settings")
|
| 26 |
+
max_length = st.slider("Max output length", min_value=16, max_value=200, value=50)
|
| 27 |
+
num_beams = st.slider("Beam search width", min_value=1, max_value=8, value=4)
|
| 28 |
+
do_sample = st.checkbox("Enable sampling", value=False)
|
| 29 |
+
top_k = st.slider("Top-k sampling", min_value=0, max_value=100, value=50)
|
| 30 |
+
temperature= st.slider("Temperature", min_value=0.1, max_value=2.0, value=1.0, step=0.1)
|
| 31 |
+
|
| 32 |
+
# 4. Generate button
|
| 33 |
+
if st.button("🔄 Generate"):
|
| 34 |
+
if not user_input.strip():
|
| 35 |
+
st.error("Please enter a prompt first.")
|
| 36 |
+
else:
|
| 37 |
+
with st.spinner("Generating…"):
|
| 38 |
+
outputs = generator(user_input)
|
| 39 |
+
# pipeline returns list of dicts with key "generated_text"
|
| 40 |
+
result = outputs[0]["generated_text"]
|
| 41 |
+
st.subheader("Output")
|
| 42 |
+
st.write(result)
|