Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +108 -40
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,108 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"β
Model {model_name} loaded successfully on {DEVICE_STR}!")
|
| 2 |
+
return tokenizer, model
|
| 3 |
+
|
| 4 |
+
except ValueError as e:
|
| 5 |
+
if "Unrecognized configuration class" in str(e):
|
| 6 |
+
progress_placeholder.error(f"β Error: {model_name} is not a causal language model suitable for text generation. Please select a different model.")
|
| 7 |
+
st.error(f"Technical details: {str(e)}")
|
| 8 |
+
else:
|
| 9 |
+
progress_placeholder.error(f"β Error loading model: {str(e)}")
|
| 10 |
+
raise e
|
| 11 |
+
except Exception as e:
|
| 12 |
+
progress_placeholder.error(f"β Unexpected error loading model: {str(e)}")
|
| 13 |
+
raise e
|
| 14 |
+
|
| 15 |
+
tokenizer, model = load_model(MODEL_NAME)
|
| 16 |
+
|
| 17 |
+
def generate_text(prompt, max_new_tokens=150, temperature=0.7, top_p=0.9):
|
| 18 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 19 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_new_tokens=max_new_tokens,
|
| 24 |
+
do_sample=True,
|
| 25 |
+
temperature=temperature,
|
| 26 |
+
top_p=top_p,
|
| 27 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 28 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 29 |
+
)
|
| 30 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
+
return text
|
| 32 |
+
|
| 33 |
+
# ---------- Streamlit UI ----------
|
| 34 |
+
st.title(f"Language Model Text Generator ({DEVICE_STR.upper()})")
|
| 35 |
+
st.caption("Choose from various pre-trained language models for text generation")
|
| 36 |
+
|
| 37 |
+
prompt = st.text_area(
|
| 38 |
+
"Enter prompt (English or other supported languages depending on model)",
|
| 39 |
+
value="The future of artificial intelligence is",
|
| 40 |
+
height=150,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
max_new_tokens = st.slider("Max output tokens", 32, 512, 150)
|
| 44 |
+
temperature = st.slider("Temperature", 0.1, 1.2, 0.7)
|
| 45 |
+
top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.9)
|
| 46 |
+
|
| 47 |
+
if st.button("Generate"):
|
| 48 |
+
# Create progress placeholder
|
| 49 |
+
progress_container = st.container()
|
| 50 |
+
|
| 51 |
+
with progress_container:
|
| 52 |
+
progress_bar = st.progress(0)
|
| 53 |
+
status_text = st.empty()
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
status_text.text("π Preparing input...")
|
| 57 |
+
progress_bar.progress(25)
|
| 58 |
+
|
| 59 |
+
status_text.text("π€ Generating text... (this may take 20-40s on CPU)")
|
| 60 |
+
progress_bar.progress(50)
|
| 61 |
+
|
| 62 |
+
output = generate_text(prompt, max_new_tokens, temperature, top_p)
|
| 63 |
+
|
| 64 |
+
progress_bar.progress(100)
|
| 65 |
+
status_text.text("β
Generation complete!")
|
| 66 |
+
|
| 67 |
+
# Clear progress indicators after a short delay
|
| 68 |
+
import time
|
| 69 |
+
time.sleep(1)
|
| 70 |
+
progress_bar.empty()
|
| 71 |
+
status_text.empty()
|
| 72 |
+
|
| 73 |
+
st.subheader("Model output:")
|
| 74 |
+
st.write(output)
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
progress_bar.empty()
|
| 78 |
+
status_text.empty()
|
| 79 |
+
st.error(f"β Generation failed: {e}")
|
| 80 |
+
|
| 81 |
+
st.markdown("---")
|
| 82 |
+
|
| 83 |
+
# Model Status Section
|
| 84 |
+
st.subheader("π Model Status")
|
| 85 |
+
col1, col2, col3 = st.columns(3)
|
| 86 |
+
|
| 87 |
+
with col1:
|
| 88 |
+
st.metric("Current Model", MODEL_NAME)
|
| 89 |
+
with col2:
|
| 90 |
+
st.metric("Device", DEVICE_STR.upper())
|
| 91 |
+
with col3:
|
| 92 |
+
# Check if model is loaded by trying to access it
|
| 93 |
+
try:
|
| 94 |
+
model_params = sum(p.numel() for p in model.parameters())
|
| 95 |
+
st.metric("Model Parameters", f"{model_params:,}")
|
| 96 |
+
except:
|
| 97 |
+
st.metric("Model Parameters", "Loading...")
|
| 98 |
+
|
| 99 |
+
st.markdown("---")
|
| 100 |
+
st.markdown(
|
| 101 |
+
"""
|
| 102 |
+
**Tips**
|
| 103 |
+
- First run will download model to `~/.cache/huggingface`.
|
| 104 |
+
- DialoGPT models work well for conversational text.
|
| 105 |
+
- GPT-2/DistilGPT-2 work best with English prompts.
|
| 106 |
+
- Use smaller models (DialoGPT-small, DistilGPT-2) for faster CPU response.
|
| 107 |
+
"""
|
| 108 |
+
)
|