File size: 3,668 Bytes
e59f509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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.")