Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer | |
| import torch | |
| # Tải mô hình và tokenizer từ các tệp | |
| def load_model(): | |
| model_name = "chinhr/trans_envi" # Thay đổi tên mô hình nếu cần | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name, config="path/to/config.json") | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| return model, tokenizer | |
| # Chạy mô hình dịch | |
| def translate_sentence(sentence, model, tokenizer): | |
| inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True) | |
| with torch.no_grad(): | |
| outputs = model.generate(**inputs) | |
| translation = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return translation | |
| # Khởi tạo Streamlit | |
| st.title("Dịch máy từ tiếng Anh sang tiếng Việt") | |
| st.write("Nhập câu tiếng Anh dưới đây để dịch:") | |
| # Nhận đầu vào từ người dùng | |
| user_input = st.text_input("Câu tiếng Anh:") | |
| if st.button("Dịch"): | |
| if user_input: | |
| model, tokenizer = load_model() | |
| translation = translate_sentence(user_input, model, tokenizer) | |
| st.write("Kết quả dịch:", translation) | |
| else: | |
| st.write("Vui lòng nhập một câu.") | |