This project is a Streamlit app that uses the mGPT-XL (1.3B) model to generate Azerbaijani text. Users can input partial text, and the model will complete it with contextually relevant text in Azerbaijani.
import streamlit as st
import time
from modeling.model_inference import generate_response
def main():
    st.title("mGPT - Azərbaycanca 🇦🇿")

    # Input text
    user_input = st.text_input("", placeholder="Davamının yazılmasını istədiyiniz mətni daxil edin...", key="input")

    if st.button("Tamamla"):
        with st.spinner("Mətniniz tamamlanır..."):
            if len(user_input) > 0:
                text = generate_response(user_input)
            st.markdown(f'<div id="animated-text">{text}</div>', unsafe_allow_html=True)
            st.markdown(
                """
                <style>
                    #animated-text {
                        white-space: pre-wrap;
                        overflow: hidden;
                        border-right: 1px solid;
                        font-size: 24px;
                        width: fit-content;
                        animation: typing 2s steps(40, end);
                    }

                    @keyframes typing {
                        from {
                            width: 0;
                        }
                        to {
                            width: 100%;
                        }
                    }
                </style>
                """,
                unsafe_allow_html=True,
            )


if __name__ == "__main__":
    main()











main.py
NijatZeynalov/mgpt-az-streamlit
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

def get_device():
    if torch.backends.mps.is_available():
        device = torch.device("mps")
    elif torch.cuda.is_available():
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")
    return device


tokenizer = GPT2Tokenizer.from_pretrained("ai-forever/mGPT-1.3B-azerbaijan")
model = GPT2LMHeadModel.from_pretrained("ai-forever/mGPT-1.3B-azerbaijan")

device = get_device()
print('device', device)
model.to(device)

def generate_response(user_input):
        input_ids = tokenizer.encode(user_input, return_tensors="pt").to(device)

        out = model.generate(
                input_ids,
                min_length=50,
                max_length=180,
                eos_token_id=5,
                top_k=5,
                top_p=0.0,
                no_repeat_ngram_size=3
        )
        generated_text = list(map(tokenizer.decode, out))[0]
        last_dot_index = generated_text.rfind(".")

        # Extract the substring up to the last dot
        result = generated_text[:last_dot_index + 1]

        return result


# delete venv folder and dockerize

modeling/model_inference.py
NijatZeynalov/mgpt-az-streamlit