app90 commited on
Commit
25a6de1
·
verified ·
1 Parent(s): 074931c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -39
app.py CHANGED
@@ -3,40 +3,22 @@ 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
- tokenizer = MBart50Tokenizer.from_pretrained(model_path, src_lang="hi_IN", tgt_lang="hne_IN")
23
- translator = Translator()
24
 
25
- # Detect Language
26
- # def detect_language(text):
27
- # try:
28
- # return langdetect.detect(text)
29
- # except:
30
- # return "unknown"
31
-
32
- # ✅ Translate English → Hindi
33
- # def translate_english_to_hindi(text):
34
- # translated = translator.translate(text, src="en", dest="hi")
35
- # return translated.text
36
-
37
- # ✅ Translate Hindi → Chhattisgarhi
38
  def translate_hindi_to_chhattisgarhi(text):
39
- sentences = text.split("।") # Sentence splitting
40
  translated_sentences = []
41
 
42
  for sentence in sentences:
@@ -50,27 +32,15 @@ def translate_hindi_to_chhattisgarhi(text):
50
 
51
  return " । ".join(translated_sentences)
52
 
53
- # Streamlit UI
54
- st.title("English/Hindi to Chhattisgarhi Translator 🗣️")
55
- st.write("Enter an English or Hindi sentence and get its translation in Chhattisgarhi.")
56
 
57
  user_input = st.text_area("Enter text:")
58
 
59
  if st.button("Translate"):
60
  if user_input.strip():
61
- lang = detect_language(user_input)
62
-
63
- if lang == "en":
64
- hindi_text = translate_english_to_hindi(user_input)
65
- chhattisgarhi_text = translate_hindi_to_chhattisgarhi(hindi_text)
66
- st.success(f"**Hindi Translation**:\n{hindi_text}")
67
- st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
68
-
69
- elif lang == "hi":
70
- chhattisgarhi_text = translate_hindi_to_chhattisgarhi(user_input)
71
- st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
72
-
73
- else:
74
- st.error("❌ Unable to detect language. Please enter text in English or Hindi.")
75
  else:
76
  st.warning("⚠ Please enter some text before translating.")
 
3
  import torch
4
  import streamlit as st
5
  from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
 
 
6
 
 
7
  os.environ["STREAMLIT_WATCH_FILE_SYSTEM"] = "false"
8
 
 
9
  try:
10
  asyncio.get_running_loop()
11
  except RuntimeError:
12
  asyncio.set_event_loop(asyncio.new_event_loop())
13
 
14
+ # Load model and tokenizer
15
  model_path = "app90/ChhattishgarhiAI_Model"
16
  model = MBartForConditionalGeneration.from_pretrained(model_path)
17
+ tokenizer = MBart50TokenizerFast.from_pretrained(model_path, src_lang="hi_IN", tgt_lang="hne_IN")
 
 
18
 
19
+ # Translate Hindi → Chhattisgarhi
 
 
 
 
 
 
 
 
 
 
 
 
20
  def translate_hindi_to_chhattisgarhi(text):
21
+ sentences = text.split("।")
22
  translated_sentences = []
23
 
24
  for sentence in sentences:
 
32
 
33
  return " । ".join(translated_sentences)
34
 
35
+ # Streamlit UI
36
+ st.title("Hindi to Chhattisgarhi Translator 🗣️")
37
+ st.write("Enter a Hindi sentence and get its translation in Chhattisgarhi.")
38
 
39
  user_input = st.text_area("Enter text:")
40
 
41
  if st.button("Translate"):
42
  if user_input.strip():
43
+ chhattisgarhi_text = translate_hindi_to_chhattisgarhi(user_input)
44
+ st.success(f"**Chhattisgarhi Translation**:\n{chhattisgarhi_text}")
 
 
 
 
 
 
 
 
 
 
 
 
45
  else:
46
  st.warning("⚠ Please enter some text before translating.")