Update app.py
Browse files
app.py
CHANGED
|
@@ -31,3 +31,69 @@ if is_allowed_time():
|
|
| 31 |
st.audio(audio_output, format='audio/mp3')
|
| 32 |
else:
|
| 33 |
st.write("Translation service is available after 6 PM IST.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
st.audio(audio_output, format='audio/mp3')
|
| 32 |
else:
|
| 33 |
st.write("Translation service is available after 6 PM IST.")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
import sacrebleu
|
| 37 |
+
import torch
|
| 38 |
+
def evaluate_bleu_score(model, tokenizer, test_data):
|
| 39 |
+
references = []
|
| 40 |
+
hypotheses = []
|
| 41 |
+
|
| 42 |
+
# Ensure the model is on the correct device
|
| 43 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 44 |
+
model.to(device)
|
| 45 |
+
|
| 46 |
+
for item in test_data:
|
| 47 |
+
english_text = item["english"]
|
| 48 |
+
expected_hindi = item["hindi"]
|
| 49 |
+
|
| 50 |
+
# Tokenize and move inputs to the correct device
|
| 51 |
+
inputs = tokenizer(english_text, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 52 |
+
translated_tokens = model.generate(**inputs)
|
| 53 |
+
translated_hindi = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
references.append([expected_hindi])
|
| 56 |
+
hypotheses.append(translated_hindi)
|
| 57 |
+
|
| 58 |
+
# Calculate BLEU score
|
| 59 |
+
bleu = sacrebleu.corpus_bleu(hypotheses, references)
|
| 60 |
+
return bleu.score
|
| 61 |
+
|
| 62 |
+
test_data = [
|
| 63 |
+
{"english": "Hello, how are you?", "hindi": "नमस्ते, आप कैसे हैं?"},
|
| 64 |
+
{"english": "What is your name?", "hindi": "आपका नाम क्या है?"},
|
| 65 |
+
{"english": "I love to play football.", "hindi": "मुझे फुटबॉल खेलना बहुत पसंद है।"},
|
| 66 |
+
{"english": "How old are you?", "hindi": "आपकी उम्र क्या है?"},
|
| 67 |
+
{"english": "I am from India.", "hindi": "मैं भारत से हूँ।"},
|
| 68 |
+
{"english": "What do you do?", "hindi": "आप क्या करते हैं?"},
|
| 69 |
+
{"english": "I like to read books.", "hindi": "मुझे किताबें पढ़ना पसंद है।"},
|
| 70 |
+
{"english": "Do you speak English?", "hindi": "क्या आप अंग्रेज़ी बोलते हैं?"},
|
| 71 |
+
{"english": "I have a dog.", "hindi": "मेरे पास एक कुत्ता है।"},
|
| 72 |
+
{"english": "What is your favorite food?", "hindi": "आपका पसंदीदा खाना क्या है?"},
|
| 73 |
+
{"english": "I love to travel.", "hindi": "मुझे यात्रा करना बहुत पसंद है।"},
|
| 74 |
+
{"english": "How much does it cost?", "hindi": "इसकी कीमत क्या है?"},
|
| 75 |
+
{"english": "I am happy to meet you.", "hindi": "आपसे मिलने में मुझे खुशी है।"}
|
| 76 |
+
|
| 77 |
+
]
|
| 78 |
+
training_args = Seq2SeqTrainingArguments(
|
| 79 |
+
output_dir="./results",
|
| 80 |
+
evaluation_strategy="epoch",
|
| 81 |
+
learning_rate=2e-5,
|
| 82 |
+
per_device_train_batch_size=16,
|
| 83 |
+
per_device_eval_batch_size=16,
|
| 84 |
+
weight_decay=0.01,
|
| 85 |
+
save_total_limit=3,
|
| 86 |
+
num_train_epochs=10, # Increase number of epochs
|
| 87 |
+
predict_with_generate=True,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
trainer = Seq2SeqTrainer(
|
| 91 |
+
model=model,
|
| 92 |
+
args=training_args,
|
| 93 |
+
train_dataset=tokenized_datasets,
|
| 94 |
+
eval_dataset=tokenized_datasets,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
trainer.train()
|
| 98 |
+
bleu_score = evaluate_bleu_score(model, tokenizer, test_data)
|
| 99 |
+
print(f"Accuracy: {bleu_score:.2f}%")
|