Maitreyee22 commited on
Commit
7926074
·
verified ·
1 Parent(s): 1295034

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)