Spaces:
Sleeping
Sleeping
Download app.py from ms1449/week3day1Task: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/ms1449/week3day1Task/resolve/main/app.py
- Command line
-
hf download hf://spaces/ms1449/week3day1Task/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ms1449/week3day1Task/resolve/main/app.py
1.06 kB
| import streamlit as st | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| # Load the model and tokenizer | |
| model_name = "gpt2-large" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name) | |
| # Streamlit app | |
| st.title("blog generator") | |
| # Input area for the topic | |
| topic = st.text_area("Enter the topic for your blog post:") | |
| # Generate button | |
| if st.button("Generate Blog Post"): | |
| if topic: | |
| # Prepare the prompt | |
| prompt = f"Write a blog post about {topic}:\n\n" | |
| # Tokenize the input | |
| inputs_encoded = tokenizer.encode(prompt, return_tensors="pt") | |
| # Generate text | |
| model_output = model.generate(inputs_encoded, max_new_tokens=50, do_sample=True, temperature=0.7) | |
| # Decode the output | |
| output = tokenizer.decode(model_output[0], skip_special_tokens=True) | |
| # Display the generated blog post | |
| st.subheader("Generated Blog Post:") | |
| st.write(output) | |
| else: | |
| st.warning("no topic.") |