Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,27 @@
|
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Page
|
| 6 |
st.set_page_config(
|
| 7 |
page_title="AI Text Generator",
|
| 8 |
page_icon="🤖",
|
| 9 |
layout="centered"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
st.title("Dawar Shariq | Student ID: B04-0923-000022")
|
| 14 |
-
|
| 15 |
st.header("AI Text Generator")
|
|
|
|
| 16 |
st.write(
|
| 17 |
-
"Enter
|
| 18 |
-
"using a pretrained Hugging Face model."
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
MODEL_NAME = "distilgpt2"
|
| 23 |
|
|
|
|
|
|
|
| 24 |
@st.cache_resource
|
| 25 |
def load_model():
|
| 26 |
return pipeline(
|
|
@@ -28,20 +30,21 @@ def load_model():
|
|
| 28 |
model=MODEL_NAME
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
with st.spinner("Loading AI model..."):
|
| 33 |
generator = load_model()
|
| 34 |
|
| 35 |
st.success(f"Model loaded: {MODEL_NAME}")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
prompt = st.text_area(
|
| 39 |
"Enter your starting text:",
|
| 40 |
-
placeholder="Artificial intelligence can help people...",
|
| 41 |
height=120
|
| 42 |
)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
max_new_tokens = st.slider(
|
| 46 |
"Maximum new tokens",
|
| 47 |
min_value=10,
|
|
@@ -50,35 +53,43 @@ max_new_tokens = st.slider(
|
|
| 50 |
step=10
|
| 51 |
)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
if st.button("Generate Text", type="primary"):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
max_new_tokens=max_new_tokens,
|
| 60 |
-
num_return_sequences=1,
|
| 61 |
-
do_sample=True,
|
| 62 |
-
temperature=0.8,
|
| 63 |
-
pad_token_id=generator.tokenizer.eos_token_id
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Display only the generated continuation
|
| 67 |
-
full_text = result[0]["generated_text"]
|
| 68 |
-
continuation = full_text[len(prompt):]
|
| 69 |
-
|
| 70 |
-
st.subheader("Generated Continuation")
|
| 71 |
-
if continuation.strip():
|
| 72 |
-
st.write(continuation)
|
| 73 |
-
else:
|
| 74 |
-
st.write(full_text)
|
| 75 |
|
| 76 |
else:
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
with st.expander("About this application"):
|
| 81 |
-
st.write("Model: distilgpt2")
|
| 82 |
-
st.write("
|
| 83 |
-
st.write("Framework: Hugging Face Transformers")
|
| 84 |
-
st.write("Interface: Streamlit")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Page settings
|
| 6 |
st.set_page_config(
|
| 7 |
page_title="AI Text Generator",
|
| 8 |
page_icon="🤖",
|
| 9 |
layout="centered"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# Required student information
|
| 13 |
st.title("Dawar Shariq | Student ID: B04-0923-000022")
|
|
|
|
| 14 |
st.header("AI Text Generator")
|
| 15 |
+
|
| 16 |
st.write(
|
| 17 |
+
"Enter any starting text and let the AI generate "
|
| 18 |
+
"a continuation using a pretrained Hugging Face model."
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Pretrained model
|
| 22 |
MODEL_NAME = "distilgpt2"
|
| 23 |
|
| 24 |
+
|
| 25 |
+
# Load the model once and reuse it
|
| 26 |
@st.cache_resource
|
| 27 |
def load_model():
|
| 28 |
return pipeline(
|
|
|
|
| 30 |
model=MODEL_NAME
|
| 31 |
)
|
| 32 |
|
| 33 |
+
|
| 34 |
+
# Load model with a loading message
|
| 35 |
with st.spinner("Loading AI model..."):
|
| 36 |
generator = load_model()
|
| 37 |
|
| 38 |
st.success(f"Model loaded: {MODEL_NAME}")
|
| 39 |
|
| 40 |
+
# User can enter their own text
|
| 41 |
prompt = st.text_area(
|
| 42 |
"Enter your starting text:",
|
| 43 |
+
placeholder="Example: Artificial intelligence can help people...",
|
| 44 |
height=120
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# User controls the length of generated text
|
| 48 |
max_new_tokens = st.slider(
|
| 49 |
"Maximum new tokens",
|
| 50 |
min_value=10,
|
|
|
|
| 53 |
step=10
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Generate text when the button is clicked
|
| 57 |
if st.button("Generate Text", type="primary"):
|
| 58 |
+
|
| 59 |
+
# Handle empty input
|
| 60 |
+
if not prompt.strip():
|
| 61 |
+
st.warning("Please enter some text before generating.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
else:
|
| 64 |
+
try:
|
| 65 |
+
with st.spinner("Generating text..."):
|
| 66 |
+
result = generator(
|
| 67 |
+
prompt,
|
| 68 |
+
max_new_tokens=max_new_tokens,
|
| 69 |
+
num_return_sequences=1,
|
| 70 |
+
do_sample=True,
|
| 71 |
+
temperature=0.8,
|
| 72 |
+
pad_token_id=generator.tokenizer.eos_token_id
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
generated_text = result[0]["generated_text"]
|
| 76 |
+
|
| 77 |
+
# Display only the newly generated continuation
|
| 78 |
+
continuation = generated_text[len(prompt):].strip()
|
| 79 |
+
|
| 80 |
+
st.subheader("Generated Continuation")
|
| 81 |
+
|
| 82 |
+
if continuation:
|
| 83 |
+
st.write(continuation)
|
| 84 |
+
else:
|
| 85 |
+
st.write(generated_text)
|
| 86 |
+
|
| 87 |
+
except Exception as error:
|
| 88 |
+
st.error(f"Something went wrong: {error}")
|
| 89 |
|
| 90 |
+
# Information about the app
|
| 91 |
with st.expander("About this application"):
|
| 92 |
+
st.write("**Model:** distilgpt2")
|
| 93 |
+
st.write("**Task:** Text generation")
|
| 94 |
+
st.write("**Framework:** Hugging Face Transformers")
|
| 95 |
+
st.write("**Interface:** Streamlit")
|