Spaces:
Build error
Build error
File size: 1,929 Bytes
94b764a fcd9551 af2f7f5 fcd9551 af2f7f5 fcd9551 5859c23 fcd9551 6f1abf4 fcd9551 | 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 | import streamlit as st
import google.generativeai as genai
import os
# Securely fetch the API key from environment variables
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
st.error("API key not found. Please set the GEMINI_API_KEY environment variable.")
st.stop()
# Set up Gemini API
genai.configure(api_key=api_key)
# Set up the model
generation_config = {
"temperature": 0.9, # Controls creativity (0 = strict, 1 = creative)
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048, # Maximum length of the response
}
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
]
model = genai.GenerativeModel(
model_name="gemini-2.0-pro-exp-02-05",
generation_config=generation_config,
safety_settings=safety_settings,
)
# Streamlit App
st.title("π Gemini Story Generator")
st.write("Enter a prompt, and Gemini AI will generate a story for you!")
# User input
user_prompt = st.text_area("Enter your story prompt:", "Once upon a time...")
# Generate story button
if st.button("Generate Story"):
if user_prompt.strip() == "":
st.warning("Please enter a prompt!")
else:
try:
# Generate response using Gemini AI
response = model.generate_content(
f"Write a creative and engaging story suitable for elementary school children based on this prompt: {user_prompt}"
)
st.write("### Here's Your Story:")
st.write(response.text)
st.success("π The End π")
except Exception as e:
st.error(f"An error occurred: {e}. Please try again.") |