Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e64ebe8

Browse files
Added app.py
1 parent 662c6b1 commit e64ebe8

File tree

1 file changed

+129
-0
lines changed
  • G/Story-Generator-Application-Using-Gemini

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import streamlit as st
2+
import google.generativeai as genai
3+
import os
4+
import pyperclip
5+
import time # for simulating progress
6+
7+
# Set up Gemini API key (replace with your own key)
8+
os.environ["API_KEY"] = 'GEMINI_API_KEY'
9+
genai.configure(api_key=os.environ["API_KEY"])
10+
11+
# Initialize a cache to store versions of the story
12+
if 'story_versions' not in st.session_state:
13+
st.session_state.story_versions = []
14+
if 'current_version' not in st.session_state:
15+
st.session_state.current_version = -1
16+
17+
# Function to call Gemini API to generate a story
18+
def generate_story(prompt):
19+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
20+
response = model.generate_content(prompt)
21+
return response.text
22+
23+
# Add a new story version to session state
24+
def add_story_version(new_story):
25+
st.session_state.story_versions.append(new_story)
26+
st.session_state.current_version = len(st.session_state.story_versions) - 1
27+
28+
# Navigate between story versions
29+
def navigate_story_version(direction):
30+
if direction == 'prev' and st.session_state.current_version > 0:
31+
st.session_state.current_version -= 1
32+
elif direction == 'next' and st.session_state.current_version < len(st.session_state.story_versions) - 1:
33+
st.session_state.current_version += 1
34+
35+
# UI Setup
36+
st.markdown(
37+
"""
38+
<style>
39+
.title {
40+
text-align: center;
41+
font-size: 36px;
42+
color: #4A90E2;
43+
margin-bottom: 20px;
44+
}
45+
.section-title {
46+
font-size: 24px;
47+
color: #333;
48+
}
49+
.expander-header {
50+
font-size: 18px;
51+
color: #2C3E50;
52+
}
53+
.sidebar .sidebar-content {
54+
background-color: #F7F9FA;
55+
padding: 20px;
56+
}
57+
</style>
58+
""",
59+
unsafe_allow_html=True
60+
)
61+
62+
# Sidebar for story settings
63+
st.sidebar.header("Story Settings")
64+
65+
# Sidebar options for user inputs
66+
age_group = st.sidebar.selectbox("Choose Age Group", ['Children', 'Teenagers', 'Adults'])
67+
genre = st.sidebar.selectbox("Choose Genre", ['Adventure', 'Mystery', 'Fantasy', 'Sci-Fi', 'Horror', 'Romance'])
68+
user_prompt = st.sidebar.text_input("Enter a story prompt or idea:")
69+
70+
71+
# Display app heading
72+
st.markdown('<div class="title">StoryCraft: Your Personal Story Generator</div>', unsafe_allow_html=True)
73+
74+
# Button to generate a story based on the user input
75+
if st.sidebar.button("Generate Story"):
76+
if user_prompt:
77+
prompt = f"Generate a {genre} story for {age_group}. {user_prompt}"
78+
79+
with st.spinner('Generating your story...'):
80+
# Simulate a loading progress bar
81+
progress = st.progress(0)
82+
for i in range(100):
83+
time.sleep(0.02) # Simulating the delay
84+
progress.progress(i + 1)
85+
86+
generated_story = generate_story(prompt)
87+
add_story_version(generated_story)
88+
else:
89+
st.sidebar.warning("Please enter a prompt to generate a story.")
90+
91+
# Show generated story if one exists
92+
if st.session_state.current_version >= 0:
93+
st.markdown('<div class="section-title">Generated Story</div>', unsafe_allow_html=True)
94+
95+
# Display collapsible story content
96+
with st.expander("Show/Hide Story", expanded=True):
97+
current_story = st.session_state.story_versions[st.session_state.current_version]
98+
st.text_area("Your Story", value=current_story, height=300)
99+
100+
# Copy the story to clipboard
101+
if st.button("Copy Story"):
102+
pyperclip.copy(current_story)
103+
st.success("Story copied to clipboard!")
104+
105+
# Show 'Modify the Story' section only after the first story has been generated
106+
st.markdown('<div class="section-title">Modify the Story</div>', unsafe_allow_html=True)
107+
with st.expander("Suggest Changes", expanded=False):
108+
modification_prompt = st.text_input("Suggest changes or add a new prompt to modify the story:")
109+
if st.button("Update Story"):
110+
if modification_prompt:
111+
with st.spinner('Updating the story...'):
112+
modified_story = generate_story(modification_prompt + " " + current_story)
113+
add_story_version(modified_story)
114+
st.success("Story updated!")
115+
else:
116+
st.warning("Please enter a modification prompt.")
117+
118+
# Show navigation buttons only if more than one version exists
119+
if len(st.session_state.story_versions) > 1:
120+
st.markdown('<div class="section-title">Version History</div>', unsafe_allow_html=True)
121+
col1, col2 = st.columns(2)
122+
with col1:
123+
if st.button("← Previous Version"):
124+
navigate_story_version('prev')
125+
with col2:
126+
if st.button("Next Version →"):
127+
navigate_story_version('next')
128+
else:
129+
st.info("Generate a story to start!")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /