Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,75 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import asyncio
|
| 3 |
-
import torch
|
| 4 |
-
import streamlit as st
|
| 5 |
-
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 6 |
-
from googletrans import Translator
|
| 7 |
-
import langdetect
|
| 8 |
-
|
| 9 |
-
# β
Disable file watching for PyTorch compatibility with Streamlit
|
| 10 |
-
os.environ["STREAMLIT_WATCH_FILE_SYSTEM"] = "false"
|
| 11 |
-
|
| 12 |
-
# β
Fix for asyncio event loop crash on Windows
|
| 13 |
-
try:
|
| 14 |
-
asyncio.get_running_loop()
|
| 15 |
-
except RuntimeError:
|
| 16 |
-
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 17 |
-
|
| 18 |
-
# β
Load Fine-Tuned Chhattisgarhi Translation Model
|
| 19 |
-
model_path = "
|
| 20 |
-
model = MBartForConditionalGeneration.from_pretrained(model_path)
|
| 21 |
-
tokenizer = MBart50TokenizerFast.from_pretrained(model_path, src_lang="hi_IN", tgt_lang="hne_IN")
|
| 22 |
-
translator = Translator()
|
| 23 |
-
|
| 24 |
-
# β
Detect Language
|
| 25 |
-
def detect_language(text):
|
| 26 |
-
try:
|
| 27 |
-
return langdetect.detect(text)
|
| 28 |
-
except:
|
| 29 |
-
return "unknown"
|
| 30 |
-
|
| 31 |
-
# β
Translate English β Hindi
|
| 32 |
-
def translate_english_to_hindi(text):
|
| 33 |
-
translated = translator.translate(text, src="en", dest="hi")
|
| 34 |
-
return translated.text
|
| 35 |
-
|
| 36 |
-
# β
Translate Hindi β Chhattisgarhi
|
| 37 |
-
def translate_hindi_to_chhattisgarhi(text):
|
| 38 |
-
sentences = text.split("ΰ₯€") # Sentence splitting
|
| 39 |
-
translated_sentences = []
|
| 40 |
-
|
| 41 |
-
for sentence in sentences:
|
| 42 |
-
sentence = sentence.strip()
|
| 43 |
-
if sentence:
|
| 44 |
-
inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding="longest", max_length=256)
|
| 45 |
-
with torch.no_grad():
|
| 46 |
-
translated_ids = model.generate(**inputs, max_length=256, num_beams=5, early_stopping=True)
|
| 47 |
-
translated_text = tokenizer.decode(translated_ids[0], skip_special_tokens=True)
|
| 48 |
-
translated_sentences.append(translated_text)
|
| 49 |
-
|
| 50 |
-
return " ΰ₯€ ".join(translated_sentences)
|
| 51 |
-
|
| 52 |
-
# β
Streamlit UI
|
| 53 |
-
st.title("English/Hindi to Chhattisgarhi Translator π£οΈ")
|
| 54 |
-
st.write("Enter an English or Hindi sentence and get its translation in Chhattisgarhi.")
|
| 55 |
-
|
| 56 |
-
user_input = st.text_area("Enter text:")
|
| 57 |
-
|
| 58 |
-
if st.button("Translate"):
|
| 59 |
-
if user_input.strip():
|
| 60 |
-
lang = detect_language(user_input)
|
| 61 |
-
|
| 62 |
-
if lang == "en":
|
| 63 |
-
hindi_text = translate_english_to_hindi(user_input)
|
| 64 |
-
chhattisgarhi_text = translate_hindi_to_chhattisgarhi(hindi_text)
|
| 65 |
-
st.success(f"**Hindi Translation**:\n{hindi_text}")
|
| 66 |
-
st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
|
| 67 |
-
|
| 68 |
-
elif lang == "hi":
|
| 69 |
-
chhattisgarhi_text = translate_hindi_to_chhattisgarhi(user_input)
|
| 70 |
-
st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
|
| 71 |
-
|
| 72 |
-
else:
|
| 73 |
-
st.error("β Unable to detect language. Please enter text in English or Hindi.")
|
| 74 |
-
else:
|
| 75 |
-
st.warning("β Please enter some text before translating.")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import asyncio
|
| 3 |
+
import torch
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 6 |
+
from googletrans import Translator
|
| 7 |
+
import langdetect
|
| 8 |
+
|
| 9 |
+
# β
Disable file watching for PyTorch compatibility with Streamlit
|
| 10 |
+
os.environ["STREAMLIT_WATCH_FILE_SYSTEM"] = "false"
|
| 11 |
+
|
| 12 |
+
# β
Fix for asyncio event loop crash on Windows
|
| 13 |
+
try:
|
| 14 |
+
asyncio.get_running_loop()
|
| 15 |
+
except RuntimeError:
|
| 16 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 17 |
+
|
| 18 |
+
# β
Load Fine-Tuned Chhattisgarhi Translation Model
|
| 19 |
+
model_path = "app90/ChhattishgarhiAI_Model"
|
| 20 |
+
model = MBartForConditionalGeneration.from_pretrained(model_path)
|
| 21 |
+
tokenizer = MBart50TokenizerFast.from_pretrained(model_path, src_lang="hi_IN", tgt_lang="hne_IN")
|
| 22 |
+
translator = Translator()
|
| 23 |
+
|
| 24 |
+
# β
Detect Language
|
| 25 |
+
def detect_language(text):
|
| 26 |
+
try:
|
| 27 |
+
return langdetect.detect(text)
|
| 28 |
+
except:
|
| 29 |
+
return "unknown"
|
| 30 |
+
|
| 31 |
+
# β
Translate English β Hindi
|
| 32 |
+
def translate_english_to_hindi(text):
|
| 33 |
+
translated = translator.translate(text, src="en", dest="hi")
|
| 34 |
+
return translated.text
|
| 35 |
+
|
| 36 |
+
# β
Translate Hindi β Chhattisgarhi
|
| 37 |
+
def translate_hindi_to_chhattisgarhi(text):
|
| 38 |
+
sentences = text.split("ΰ₯€") # Sentence splitting
|
| 39 |
+
translated_sentences = []
|
| 40 |
+
|
| 41 |
+
for sentence in sentences:
|
| 42 |
+
sentence = sentence.strip()
|
| 43 |
+
if sentence:
|
| 44 |
+
inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding="longest", max_length=256)
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
translated_ids = model.generate(**inputs, max_length=256, num_beams=5, early_stopping=True)
|
| 47 |
+
translated_text = tokenizer.decode(translated_ids[0], skip_special_tokens=True)
|
| 48 |
+
translated_sentences.append(translated_text)
|
| 49 |
+
|
| 50 |
+
return " ΰ₯€ ".join(translated_sentences)
|
| 51 |
+
|
| 52 |
+
# β
Streamlit UI
|
| 53 |
+
st.title("English/Hindi to Chhattisgarhi Translator π£οΈ")
|
| 54 |
+
st.write("Enter an English or Hindi sentence and get its translation in Chhattisgarhi.")
|
| 55 |
+
|
| 56 |
+
user_input = st.text_area("Enter text:")
|
| 57 |
+
|
| 58 |
+
if st.button("Translate"):
|
| 59 |
+
if user_input.strip():
|
| 60 |
+
lang = detect_language(user_input)
|
| 61 |
+
|
| 62 |
+
if lang == "en":
|
| 63 |
+
hindi_text = translate_english_to_hindi(user_input)
|
| 64 |
+
chhattisgarhi_text = translate_hindi_to_chhattisgarhi(hindi_text)
|
| 65 |
+
st.success(f"**Hindi Translation**:\n{hindi_text}")
|
| 66 |
+
st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
|
| 67 |
+
|
| 68 |
+
elif lang == "hi":
|
| 69 |
+
chhattisgarhi_text = translate_hindi_to_chhattisgarhi(user_input)
|
| 70 |
+
st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
|
| 71 |
+
|
| 72 |
+
else:
|
| 73 |
+
st.error("β Unable to detect language. Please enter text in English or Hindi.")
|
| 74 |
+
else:
|
| 75 |
+
st.warning("β Please enter some text before translating.")
|