Upload 5 files
Browse files- app.py +92 -0
- readme.md +48 -0
- requirements.txt +3 -0
- static/aivn_favicon.png +0 -0
- static/aivn_logo.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title="AIVN - Vietnamese Poem Generation",
|
| 7 |
+
page_icon="./static/aivn_favicon.png",
|
| 8 |
+
layout="wide",
|
| 9 |
+
initial_sidebar_state="expanded"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
st.image("./static/aivn_logo.png", width=300)
|
| 13 |
+
|
| 14 |
+
st.title("Vietnamese Poem Generation")
|
| 15 |
+
st.write("The used model is GPT-2 trained on Vietnamese poems: thangduong0509/gpt2_viet_poem_generation")
|
| 16 |
+
|
| 17 |
+
with st.expander("Instructions"):
|
| 18 |
+
st.write("""
|
| 19 |
+
1. Enter a few words or sentences as a suggestion for the poem.
|
| 20 |
+
2. Adjust the parameters to change the way the poem is generated:
|
| 21 |
+
- Temperature: Higher values produce more diverse results.
|
| 22 |
+
- Top-k: The number of words with the highest probability to choose from at each step.
|
| 23 |
+
- Top-p: Cumulative probability threshold for word selection.
|
| 24 |
+
- Repetition penalty: Adjust to avoid repetition.
|
| 25 |
+
3. Press the "Generate Poem" button to create the poem.
|
| 26 |
+
4. You can download the generated poem using the "Download Poem" button.
|
| 27 |
+
""")
|
| 28 |
+
|
| 29 |
+
prompt_input = st.text_area("Enter a few words or sentences to start:",
|
| 30 |
+
value="Con song que toi dep\n",
|
| 31 |
+
height=100)
|
| 32 |
+
|
| 33 |
+
col1, empty_col, col2 = st.columns([0.7, 0.3, 1.0])
|
| 34 |
+
with col1:
|
| 35 |
+
max_length = st.slider("Max Output Tokens:", 10, 200, 75)
|
| 36 |
+
temperature = st.slider("Temperature:", 0.1, 1.5, 0.8)
|
| 37 |
+
top_k = st.slider("Top-k:", 1, 100, 50)
|
| 38 |
+
top_p = st.slider("Top-p:", 0.1, 1.0, 0.95)
|
| 39 |
+
repetition_penalty = st.slider("Repetition Penalty:", 1.0, 2.0, 1.2)
|
| 40 |
+
|
| 41 |
+
with empty_col:
|
| 42 |
+
st.empty()
|
| 43 |
+
|
| 44 |
+
with col2:
|
| 45 |
+
if st.button("Generate Poem"):
|
| 46 |
+
with st.spinner("Generating poem..."):
|
| 47 |
+
try:
|
| 48 |
+
# Initialize model
|
| 49 |
+
generator = pipeline('text-generation',
|
| 50 |
+
model='thangduong0509/gpt2_viet_poem_generation')
|
| 51 |
+
|
| 52 |
+
# Generate poem
|
| 53 |
+
results = generator(
|
| 54 |
+
prompt_input,
|
| 55 |
+
max_new_tokens=max_length,
|
| 56 |
+
do_sample=True,
|
| 57 |
+
top_k=top_k,
|
| 58 |
+
top_p=top_p,
|
| 59 |
+
temperature=temperature,
|
| 60 |
+
repetition_penalty=repetition_penalty
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
generated_text = results[0]['generated_text']
|
| 64 |
+
st.subheader("Generated Poem:")
|
| 65 |
+
|
| 66 |
+
for line in generated_text.split('\n'):
|
| 67 |
+
st.write(line)
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"An error occurred: {str(e)}")
|
| 71 |
+
|
| 72 |
+
st.markdown(
|
| 73 |
+
"""
|
| 74 |
+
<style>
|
| 75 |
+
.footer {
|
| 76 |
+
position: fixed;
|
| 77 |
+
bottom: 0;
|
| 78 |
+
left: 0;
|
| 79 |
+
width: 100%;
|
| 80 |
+
background-color: #f1f1f1;
|
| 81 |
+
text-align: center;
|
| 82 |
+
padding: 10px 0;
|
| 83 |
+
font-size: 14px;
|
| 84 |
+
color: #555;
|
| 85 |
+
}
|
| 86 |
+
</style>
|
| 87 |
+
<div class="footer">
|
| 88 |
+
2024 AI VIETNAM | Made by <a href="https://github.com/Koii2k3/Poem-Generation" target="_blank">Koii2k3</a>
|
| 89 |
+
</div>
|
| 90 |
+
""",
|
| 91 |
+
unsafe_allow_html=True
|
| 92 |
+
)
|
readme.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Vietnamese Poem Generation with GPT-2
|
| 2 |
+
|
| 3 |
+
This Streamlit application allows users to generate Vietnamese poems using a pre-trained GPT-2 model. The model is fine-tuned on a dataset of Vietnamese poems and is capable of generating creative and contextually relevant verses.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
* **Interactive Interface:** A user-friendly Streamlit interface for easy interaction.
|
| 8 |
+
* **Customizable Generation:** Users can provide a starting phrase or sentence and adjust various generation parameters like temperature, top-k, top-p, and repetition penalty.
|
| 9 |
+
* **Parameter Tuning:** Fine-tune the generation process by adjusting `temperature`, `top-k`, `top-p`, and `repetition_penalty` to achieve desired results.
|
| 10 |
+
* **Clear Output:** Displays the generated poem in a readable format, line by line.
|
| 11 |
+
* **Error Handling:** Includes error handling to gracefully manage exceptions during poem generation.
|
| 12 |
+
* **Footer Attribution:** Clear attribution to AI Vietnam and the original developer with a link to the GitHub repository.
|
| 13 |
+
* **Instructional Expander:** Provides helpful instructions on how to use the application effectively.
|
| 14 |
+
|
| 15 |
+
## Usage
|
| 16 |
+
|
| 17 |
+
1. **Input Prompt:** Enter a few words or a sentence as a starting point for the poem in the text area.
|
| 18 |
+
2. **Adjust Parameters:**
|
| 19 |
+
* **Max Output Tokens:** Set the maximum length of the generated poem.
|
| 20 |
+
* **Temperature:** Control the randomness of the generated text. Higher values lead to more diverse and unexpected results.
|
| 21 |
+
* **Top-k:** Limit the number of most likely next words considered at each step.
|
| 22 |
+
* **Top-p:** Set a probability threshold, selecting the smallest set of words whose cumulative probability exceeds the threshold.
|
| 23 |
+
* **Repetition Penalty:** Penalize repeated words to encourage more diverse text.
|
| 24 |
+
3. **Generate Poem:** Click the "Sinh thơ" (Generate Poem) button to generate the poem based on the provided input and parameters.
|
| 25 |
+
4. **View and Download:** The generated poem will be displayed on the screen.
|
| 26 |
+
|
| 27 |
+
## Model
|
| 28 |
+
|
| 29 |
+
* The application uses the `thangduong0509/gpt2_viet_poem_generation` GPT-2 model, which has been trained specifically for generating Vietnamese poetry.
|
| 30 |
+
|
| 31 |
+
## Setup
|
| 32 |
+
|
| 33 |
+
1. **Install Dependencies:**
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
pip install streamlit transformers
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
2. **Run the Application:**
|
| 40 |
+
|
| 41 |
+
```bash
|
| 42 |
+
streamlit run app.py
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Credits
|
| 46 |
+
|
| 47 |
+
* Developed by [Koii2k3(https://github.com/Koii2k3/Poem-Generation) for AI Vietnam.
|
| 48 |
+
* GPT-2 Model: `thangduong0509/gpt2_viet_poem_generation`
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.41.1
|
| 2 |
+
transformers==4.47.0
|
| 3 |
+
torch==2.5.1
|
static/aivn_favicon.png
ADDED
|
|
static/aivn_logo.png
ADDED
|