Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# --- Page Configuration ---
|
| 6 |
+
# Sets the title and icon that appear in the browser tab.
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Multilingual Gen AI",
|
| 9 |
+
page_icon="🌍",
|
| 10 |
+
layout="centered",
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# --- App Styling ---
|
| 14 |
+
# You can inject custom CSS for styling if you want a more unique look.
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.stButton>button {
|
| 18 |
+
background-color: #4CAF50;
|
| 19 |
+
color: white;
|
| 20 |
+
border-radius: 20px;
|
| 21 |
+
border: 1px solid #4CAF50;
|
| 22 |
+
padding: 10px 24px;
|
| 23 |
+
font-size: 16px;
|
| 24 |
+
}
|
| 25 |
+
.stButton>button:hover {
|
| 26 |
+
background-color: white;
|
| 27 |
+
color: #4CAF50;
|
| 28 |
+
border: 1px solid #4CAF50;
|
| 29 |
+
}
|
| 30 |
+
</style>
|
| 31 |
+
""", unsafe_allow_html=True)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# --- Main Application UI ---
|
| 35 |
+
st.title("🌍 Multilingual Generative AI Application")
|
| 36 |
+
st.write("Powered by Google Gemini")
|
| 37 |
+
st.markdown("---")
|
| 38 |
+
st.write(
|
| 39 |
+
"This app allows you to generate text in various languages. "
|
| 40 |
+
"Enter your Google Gemini API key, type a prompt, select a target language, and see the magic happen!"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# --- Function to call Gemini API ---
|
| 44 |
+
def generate_text_in_language(api_key, prompt, language):
|
| 45 |
+
"""
|
| 46 |
+
Connects to the Gemini API and generates content based on the prompt and language.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
api_key (str): The user's Google Gemini API key.
|
| 50 |
+
prompt (str): The user's input prompt.
|
| 51 |
+
language (str): The target language for the output.
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
str: The generated text or an error message.
|
| 55 |
+
"""
|
| 56 |
+
try:
|
| 57 |
+
# Configure the Gemini client with the provided API key
|
| 58 |
+
genai.configure(api_key=api_key)
|
| 59 |
+
|
| 60 |
+
# Create the generative model instance
|
| 61 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 62 |
+
|
| 63 |
+
# Construct a more effective prompt for the model
|
| 64 |
+
full_prompt = f"Please provide the answer strictly in the {language} language for the following prompt: '{prompt}'"
|
| 65 |
+
|
| 66 |
+
# Generate content
|
| 67 |
+
response = model.generate_content(full_prompt)
|
| 68 |
+
|
| 69 |
+
return response.text
|
| 70 |
+
except Exception as e:
|
| 71 |
+
# Provide a user-friendly error message
|
| 72 |
+
return f"An error occurred: {str(e)}. Please check your API key and network connection."
|
| 73 |
+
|
| 74 |
+
# --- User Inputs ---
|
| 75 |
+
# Use a password field for the API key to keep it hidden
|
| 76 |
+
api_key = st.text_input(
|
| 77 |
+
"Enter your Google Gemini API Key:",
|
| 78 |
+
type="password",
|
| 79 |
+
help="You can get your API key from Google AI Studio."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# A text area for the user to enter their prompt
|
| 83 |
+
user_prompt = st.text_area(
|
| 84 |
+
"Enter your prompt here:",
|
| 85 |
+
height=150,
|
| 86 |
+
placeholder="e.g., Explain the theory of relativity in simple terms"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# A dropdown menu for language selection
|
| 90 |
+
languages = [
|
| 91 |
+
"English", "Spanish", "French", "German", "Italian", "Portuguese",
|
| 92 |
+
"Japanese", "Korean", "Chinese (Simplified)", "Hindi", "Arabic", "Russian", "Urdu"
|
| 93 |
+
]
|
| 94 |
+
selected_language = st.selectbox("Select the output language:", languages)
|
| 95 |
+
|
| 96 |
+
# --- Generate Button and Output ---
|
| 97 |
+
if st.button("✨ Generate Text"):
|
| 98 |
+
# Validate inputs before making an API call
|
| 99 |
+
if not api_key.strip():
|
| 100 |
+
st.error("Please enter a valid API Key.")
|
| 101 |
+
elif not user_prompt.strip():
|
| 102 |
+
st.error("Please enter a prompt.")
|
| 103 |
+
else:
|
| 104 |
+
with st.spinner(f"Generating text in {selected_language}..."):
|
| 105 |
+
# Call the generation function
|
| 106 |
+
generated_text = generate_text_in_language(api_key, user_prompt, selected_language)
|
| 107 |
+
|
| 108 |
+
# Display the result in an informative box
|
| 109 |
+
st.info(f"Generated Text in {selected_language}")
|
| 110 |
+
st.write(generated_text)
|
| 111 |
+
|
| 112 |
+
st.markdown("---")
|
| 113 |
+
st.write("Developed with ❤️ using Python, Streamlit, and Gemini.")
|