import streamlit as st import google.generativeai as genai import os # Configure the Google API key from Hugging Face secrets # The os.environ.get() function safely retrieves the key api_key = os.environ.get("GOOGLE_API_KEY") if not api_key: st.error("API key not found. Please add your GOOGLE_API_KEY as a secret in Hugging Face Spaces.") else: genai.configure(api_key=api_key) @st.cache_resource def get_model(): """ Initializes and returns the Gemini model. """ # You can specify the exact Gemini model you want to use here model = genai.GenerativeModel('gemini-1.5-flash') return model model = get_model() # Create the Streamlit interface st.title("Multilingual Translator with Gemini 🌎") text_input = st.text_area("Enter text to translate:", height=200) if st.button("Translate"): if text_input: with st.spinner("Translating..."): try: # Use the model's generate_content method for translation # The prompt needs to instruct the model to perform translation prompt = f"Translate the following text into Urdu: '{text_input}'" response = model.generate_content(prompt) # Display the translated text st.success("Translation complete!") st.write(response.text) except Exception as e: st.error(f"An error occurred: {e}") else: st.warning("Please enter some text to translate.")