File size: 2,843 Bytes
615d2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import asyncio
import torch
import streamlit as st
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
from googletrans import Translator
import langdetect

# βœ… Disable file watching for PyTorch compatibility with Streamlit
os.environ["STREAMLIT_WATCH_FILE_SYSTEM"] = "false"

# βœ… Fix for asyncio event loop crash on Windows
try:
    asyncio.get_running_loop()
except RuntimeError:
    asyncio.set_event_loop(asyncio.new_event_loop())

# βœ… Load Fine-Tuned Chhattisgarhi Translation Model
model_path = "chhattisgarhi_translator"
model = MBartForConditionalGeneration.from_pretrained(model_path)
tokenizer = MBart50TokenizerFast.from_pretrained(model_path, src_lang="hi_IN", tgt_lang="hne_IN")
translator = Translator()

# βœ… Detect Language
def detect_language(text):
    try:
        return langdetect.detect(text)
    except:
        return "unknown"

# βœ… Translate English β†’ Hindi
def translate_english_to_hindi(text):
    translated = translator.translate(text, src="en", dest="hi")
    return translated.text

# βœ… Translate Hindi β†’ Chhattisgarhi
def translate_hindi_to_chhattisgarhi(text):
    sentences = text.split("ΰ₯€")  # Sentence splitting
    translated_sentences = []

    for sentence in sentences:
        sentence = sentence.strip()
        if sentence:
            inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding="longest", max_length=256)
            with torch.no_grad():
                translated_ids = model.generate(**inputs, max_length=256, num_beams=5, early_stopping=True)
            translated_text = tokenizer.decode(translated_ids[0], skip_special_tokens=True)
            translated_sentences.append(translated_text)

    return " ΰ₯€ ".join(translated_sentences)

# βœ… Streamlit UI
st.title("English/Hindi to Chhattisgarhi Translator πŸ—£οΈ")
st.write("Enter an English or Hindi sentence and get its translation in Chhattisgarhi.")

user_input = st.text_area("Enter text:")

if st.button("Translate"):
    if user_input.strip():
        lang = detect_language(user_input)

        if lang == "en":
            hindi_text = translate_english_to_hindi(user_input)
            chhattisgarhi_text = translate_hindi_to_chhattisgarhi(hindi_text)
            st.success(f"**Hindi Translation**:\n{hindi_text}")
            st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")

        elif lang == "hi":
            chhattisgarhi_text = translate_hindi_to_chhattisgarhi(user_input)
            st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")

        else:
            st.error("❌ Unable to detect language. Please enter text in English or Hindi.")
    else:
        st.warning("⚠ Please enter some text before translating.")