Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,49 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 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 |
else:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
|
| 4 |
+
# Set up Gemini API
|
| 5 |
+
genai.configure(api_key="AIzaSyBQqw4RV3XdTFuPud7mkoHNPfxQm5EnOrc") # Replace with your Gemini API key
|
| 6 |
+
|
| 7 |
+
# Set up the model
|
| 8 |
+
generation_config = {
|
| 9 |
+
"temperature": 0.9, # Controls creativity (0 = strict, 1 = creative)
|
| 10 |
+
"top_p": 1,
|
| 11 |
+
"top_k": 1,
|
| 12 |
+
"max_output_tokens": 2048, # Maximum length of the response
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
safety_settings = [
|
| 16 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 17 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 18 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 19 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
model = genai.GenerativeModel(
|
| 23 |
+
model_name="gemini-pro",
|
| 24 |
+
generation_config=generation_config,
|
| 25 |
+
safety_settings=safety_settings,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Streamlit App
|
| 29 |
+
st.title("π Gemini Story Generator")
|
| 30 |
+
st.write("Enter a prompt, and Gemini AI will generate a story for you!")
|
| 31 |
+
|
| 32 |
+
# User input
|
| 33 |
+
user_prompt = st.text_area("Enter your story prompt:", "Once upon a time...")
|
| 34 |
+
|
| 35 |
+
# Generate story button
|
| 36 |
+
if st.button("Generate Story"):
|
| 37 |
+
if user_prompt.strip() == "":
|
| 38 |
+
st.warning("Please enter a prompt!")
|
| 39 |
else:
|
| 40 |
+
try:
|
| 41 |
+
# Generate response using Gemini AI
|
| 42 |
+
response = model.generate_content(
|
| 43 |
+
f"Write a creative and engaging story suitable for elementary school children based on this prompt: {user_prompt}"
|
| 44 |
+
)
|
| 45 |
+
st.write("### Here's Your Story:")
|
| 46 |
+
st.write(response.text)
|
| 47 |
+
st.success("π The End π")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"An error occurred: {e}. Please try again.")
|