bootcamp3 / app.py
ahmadsanafarooq's picture
Create app.py
e59f509 verified
import streamlit as st
import google.generativeai as genai
import os
# --- Page Configuration ---
# Sets the title and icon that appear in the browser tab.
st.set_page_config(
page_title="Multilingual Gen AI",
page_icon="🌍",
layout="centered",
)
# --- App Styling ---
# You can inject custom CSS for styling if you want a more unique look.
st.markdown("""
<style>
.stButton>button {
background-color: #4CAF50;
color: white;
border-radius: 20px;
border: 1px solid #4CAF50;
padding: 10px 24px;
font-size: 16px;
}
.stButton>button:hover {
background-color: white;
color: #4CAF50;
border: 1px solid #4CAF50;
}
</style>
""", unsafe_allow_html=True)
# --- Main Application UI ---
st.title("🌍 Multilingual Generative AI Application")
st.write("Powered by Google Gemini")
st.markdown("---")
st.write(
"This app allows you to generate text in various languages. "
"Enter your Google Gemini API key, type a prompt, select a target language, and see the magic happen!"
)
# --- Function to call Gemini API ---
def generate_text_in_language(api_key, prompt, language):
"""
Connects to the Gemini API and generates content based on the prompt and language.
Args:
api_key (str): The user's Google Gemini API key.
prompt (str): The user's input prompt.
language (str): The target language for the output.
Returns:
str: The generated text or an error message.
"""
try:
# Configure the Gemini client with the provided API key
genai.configure(api_key=api_key)
# Create the generative model instance
model = genai.GenerativeModel('gemini-2.0-flash')
# Construct a more effective prompt for the model
full_prompt = f"Please provide the answer strictly in the {language} language for the following prompt: '{prompt}'"
# Generate content
response = model.generate_content(full_prompt)
return response.text
except Exception as e:
# Provide a user-friendly error message
return f"An error occurred: {str(e)}. Please check your API key and network connection."
# --- User Inputs ---
# Use a password field for the API key to keep it hidden
api_key = st.text_input(
"Enter your Google Gemini API Key:",
type="password",
help="You can get your API key from Google AI Studio."
)
# A text area for the user to enter their prompt
user_prompt = st.text_area(
"Enter your prompt here:",
height=150,
placeholder="e.g., Explain the theory of relativity in simple terms"
)
# A dropdown menu for language selection
languages = [
"English", "Spanish", "French", "German", "Italian", "Portuguese",
"Japanese", "Korean", "Chinese (Simplified)", "Hindi", "Arabic", "Russian", "Urdu"
]
selected_language = st.selectbox("Select the output language:", languages)
# --- Generate Button and Output ---
if st.button("✨ Generate Text"):
# Validate inputs before making an API call
if not api_key.strip():
st.error("Please enter a valid API Key.")
elif not user_prompt.strip():
st.error("Please enter a prompt.")
else:
with st.spinner(f"Generating text in {selected_language}..."):
# Call the generation function
generated_text = generate_text_in_language(api_key, user_prompt, selected_language)
# Display the result in an informative box
st.info(f"Generated Text in {selected_language}")
st.write(generated_text)
st.markdown("---")
st.write("Developed with ❀️ using Python, Streamlit, and Gemini.")