Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +44 -27
src/streamlit_app.py
CHANGED
|
@@ -13,10 +13,11 @@ import os
|
|
| 13 |
# ==============================
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 22 |
|
|
@@ -25,7 +26,6 @@ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
| 25 |
# ==============================
|
| 26 |
dimension = 384
|
| 27 |
index = faiss.IndexFlatL2(dimension)
|
| 28 |
-
|
| 29 |
memory_texts = []
|
| 30 |
|
| 31 |
def embed(text):
|
|
@@ -57,6 +57,17 @@ lang_map = {
|
|
| 57 |
"German": "deu_Latn"
|
| 58 |
}
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# ==============================
|
| 61 |
# SPEECH TO TEXT
|
| 62 |
# ==============================
|
|
@@ -78,30 +89,35 @@ def speech_to_text(audio_file):
|
|
| 78 |
return text
|
| 79 |
|
| 80 |
# ==============================
|
| 81 |
-
# TRANSLATION FUNCTION
|
| 82 |
# ==============================
|
| 83 |
def translate(text, target_lang):
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
-
|
|
|
|
| 86 |
except:
|
| 87 |
-
src_lang = "
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
Only return translated text.
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
prompt,
|
| 100 |
-
max_new_tokens=300,
|
| 101 |
-
temperature=0.2
|
| 102 |
-
)
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
store_memory(text, translated)
|
| 107 |
|
|
@@ -110,24 +126,25 @@ def translate(text, target_lang):
|
|
| 110 |
# ==============================
|
| 111 |
# UI
|
| 112 |
# ==============================
|
|
|
|
|
|
|
| 113 |
st.title("π AI Translator with Voice")
|
| 114 |
|
| 115 |
tab1, tab2 = st.tabs(["π Text Input", "π€ Voice Input"])
|
| 116 |
|
|
|
|
|
|
|
| 117 |
# ------------------------------
|
| 118 |
-
# TEXT INPUT
|
| 119 |
# ------------------------------
|
| 120 |
with tab1:
|
| 121 |
input_text = st.text_area("Enter text")
|
| 122 |
|
| 123 |
# ------------------------------
|
| 124 |
-
# VOICE INPUT
|
| 125 |
# ------------------------------
|
| 126 |
with tab2:
|
| 127 |
-
audio_file = st.file_uploader(
|
| 128 |
-
"Upload audio file (wav/mp3)",
|
| 129 |
-
type=["wav", "mp3"]
|
| 130 |
-
)
|
| 131 |
|
| 132 |
if audio_file:
|
| 133 |
st.audio(audio_file)
|
|
@@ -144,7 +161,7 @@ with tab2:
|
|
| 144 |
target_lang = st.selectbox("Target Language", list(lang_map.keys()))
|
| 145 |
|
| 146 |
if st.button("Translate"):
|
| 147 |
-
if
|
| 148 |
st.warning("Please provide input text or audio")
|
| 149 |
else:
|
| 150 |
with st.spinner("Translating..."):
|
|
|
|
| 13 |
# ==============================
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
|
| 16 |
+
if HF_TOKEN is None:
|
| 17 |
+
st.error("β HF_TOKEN not found. Add it in Hugging Face Secrets.")
|
| 18 |
+
st.stop()
|
| 19 |
+
|
| 20 |
+
client = InferenceClient(token=HF_TOKEN)
|
| 21 |
|
| 22 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 23 |
|
|
|
|
| 26 |
# ==============================
|
| 27 |
dimension = 384
|
| 28 |
index = faiss.IndexFlatL2(dimension)
|
|
|
|
| 29 |
memory_texts = []
|
| 30 |
|
| 31 |
def embed(text):
|
|
|
|
| 57 |
"German": "deu_Latn"
|
| 58 |
}
|
| 59 |
|
| 60 |
+
# langdetect β NLLB mapping
|
| 61 |
+
lang_detect_map = {
|
| 62 |
+
"en": "eng_Latn",
|
| 63 |
+
"ta": "tam_Taml",
|
| 64 |
+
"hi": "hin_Deva",
|
| 65 |
+
"fr": "fra_Latn",
|
| 66 |
+
"ar": "arb_Arab",
|
| 67 |
+
"es": "spa_Latn",
|
| 68 |
+
"de": "deu_Latn"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
# ==============================
|
| 72 |
# SPEECH TO TEXT
|
| 73 |
# ==============================
|
|
|
|
| 89 |
return text
|
| 90 |
|
| 91 |
# ==============================
|
| 92 |
+
# TRANSLATION FUNCTION (FIXED)
|
| 93 |
# ==============================
|
| 94 |
def translate(text, target_lang):
|
| 95 |
+
|
| 96 |
+
# Detect language
|
| 97 |
try:
|
| 98 |
+
detected = detect(text)
|
| 99 |
+
src_lang = lang_detect_map.get(detected, "eng_Latn")
|
| 100 |
except:
|
| 101 |
+
src_lang = "eng_Latn"
|
| 102 |
|
| 103 |
+
tgt_lang = lang_map[target_lang]
|
| 104 |
|
| 105 |
+
# RAG memory
|
| 106 |
+
memory = retrieve_memory(text)
|
|
|
|
| 107 |
|
| 108 |
+
try:
|
| 109 |
+
# β
Correct API usage
|
| 110 |
+
response = client.translation(
|
| 111 |
+
text=text,
|
| 112 |
+
src_lang=src_lang,
|
| 113 |
+
tgt_lang=tgt_lang,
|
| 114 |
+
model="facebook/nllb-200-distilled-600M"
|
| 115 |
+
)
|
| 116 |
|
| 117 |
+
translated = response["translation_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
except Exception as e:
|
| 120 |
+
translated = f"β Translation failed: {str(e)}"
|
| 121 |
|
| 122 |
store_memory(text, translated)
|
| 123 |
|
|
|
|
| 126 |
# ==============================
|
| 127 |
# UI
|
| 128 |
# ==============================
|
| 129 |
+
st.set_page_config(page_title="AI Translator", layout="wide")
|
| 130 |
+
|
| 131 |
st.title("π AI Translator with Voice")
|
| 132 |
|
| 133 |
tab1, tab2 = st.tabs(["π Text Input", "π€ Voice Input"])
|
| 134 |
|
| 135 |
+
input_text = ""
|
| 136 |
+
|
| 137 |
# ------------------------------
|
| 138 |
+
# TEXT INPUT
|
| 139 |
# ------------------------------
|
| 140 |
with tab1:
|
| 141 |
input_text = st.text_area("Enter text")
|
| 142 |
|
| 143 |
# ------------------------------
|
| 144 |
+
# VOICE INPUT
|
| 145 |
# ------------------------------
|
| 146 |
with tab2:
|
| 147 |
+
audio_file = st.file_uploader("Upload audio (wav/mp3)", type=["wav", "mp3"])
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
if audio_file:
|
| 150 |
st.audio(audio_file)
|
|
|
|
| 161 |
target_lang = st.selectbox("Target Language", list(lang_map.keys()))
|
| 162 |
|
| 163 |
if st.button("Translate"):
|
| 164 |
+
if not input_text.strip():
|
| 165 |
st.warning("Please provide input text or audio")
|
| 166 |
else:
|
| 167 |
with st.spinner("Translating..."):
|