Spaces:
Sleeping
Sleeping
Download app.py from ritujadebnath/Language_translator: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/ritujadebnath/Language_translator/resolve/main/app.py
- Command line
-
hf download hf://spaces/ritujadebnath/Language_translator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ritujadebnath/Language_translator/resolve/main/app.py
1.11 kB
| import streamlit as st | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| st.title("Language Translator") | |
| def translate_text(text, target_language, api_key): | |
| llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=api_key) | |
| prompt = ( | |
| f"Translate the following text into {target_language} and provide the phonetic transcription (how it can be written in English):\n\n" | |
| f"{text}\n\n" | |
| f"Translation:" | |
| ) | |
| response = llm.invoke(prompt) | |
| return response.content | |
| with st.form("translation_form"): | |
| text = st.text_area("Enter the text to translate") | |
| target_language = st.text_input("Enter the target language: ") | |
| submitted = st.form_submit_button("Translate") | |
| if submitted and text and target_language: | |
| api_key = st.secrets["GOOGLE_API_KEY"] | |
| translation = translate_text(text, target_language, api_key) | |
| st.info(translation) | |
| elif submitted and not text: | |
| st.error("Please enter text to translate.") | |
| elif submitted and not target_language: | |
| st.error("Please enter the target language.") | |