Deployment Link: https://huggingface.co/spaces/Yaaba/fasttext Youtube LINK: https://youtu.be/yWJLrzF9L9I
Certainly! Here's a README generated from the provided code:
This project implements a Twi language story generator using Word2Vec and FastText models. It includes data preprocessing, model training, optimization, and evaluation. The project also features a Streamlit web application for generating stories.
To run this project, you need to install the following dependencies:
pip install gensim pandas numpy tensorflow sklearn transformers torch torchvision torchaudio streamlit
-
Mount your Google Drive:
from google.colab import drive drive.mount('/content/drive')
-
Load and preprocess the datasets:
test_text = pd.read_csv('/content/drive/MyDrive/Project/test.txt', delimiter='\t', encoding='utf-8', quoting=csv.QUOTE_NONE, on_bad_lines='skip') validation_text = pd.read_csv('/content/drive/MyDrive/Project/dev.txt', delimiter='\t', encoding='utf-8', quoting=csv.QUOTE_NONE, on_bad_lines='skip') train_text = pd.read_csv('/content/drive/MyDrive/Project/train.txt', delimiter='\t', encoding='utf-8', quoting=csv.QUOTE_NONE, on_bad_lines='skip')
-
Train and optimize the models:
# Word2Vec optimized_model = optimize_word2vec(train_tokenized) optimized_model.save("optimized_word2vec_model.model") # FastText optimized_model = optimize_fasttext(train_tokenized)
-
Evaluate the models:
accuracy = evaluate_word2vec_model(model, validation_tokenized, top_n=5) print(f"Validation Accuracy: {accuracy}")
-
Generate stories:
generated_story = generate_story(optimized_model, start_prompt, max_words=5) print("Generated Story:", generated_story)
- Text preprocessing
- Word2Vec and FastText model training and optimization
- Next word prediction
- Model evaluation
- Story generation
- Streamlit web application for interactive story generation
project.Word2VEC_Predictionipynb: Main project fileword2vec_model.model: Saved Word2Vec modelfasttext_model.model: Saved FastText modelword_embeddings.kv: Saved word embeddingsoptimized_word_embeddings.txt: Optimized word embeddings in text formatfasttext_model.pkl: Saved optimized FastText model
The project trains both Word2Vec and FastText models on the Twi language dataset. The models are then optimized for better performance.
The models are evaluated using custom functions that measure the accuracy of next word predictions on validation and test datasets.
For The Deployment
This Streamlit application demonstrates text generation and next word prediction using a FastText model trained on Twi language data.
- Story Generation: Generate a story based on a given prompt.
- Next Word Prediction: Predict the most likely next words given a context.
-
Clone this repository:
git clone <repository-url> -
Install the required packages:
pip install streamlit gensim numpy -
Ensure you have the
fasttext_model.pklfile in the same directory as the script.
- Input a start prompt
- Choose the maximum number of words to generate
- Click "Generate Story" to create a story based on the input
- Input a context (a word or phrase)
- Select the number of top predictions to display
- Click "Predict Next Words" to see the most likely next words
preprocess_text(text): Cleans and preprocesses input text.predict_next_word(model, context, top_n=7): Predicts the next words given a context.generate_story(model, start_prompt, max_words=100): Generates a story based on a starting prompt.
The application uses a pre-trained FastText model (fasttext_model.pkl) for text generation and prediction. Ensure this file is present in the same directory as the script.
If the model fails to load, an error message will be displayed, and the application will stop.
This application is designed for the Twi language. Ensure that your input text and prompts are in Twi for the best results.