-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (45 loc) · 2.17 KB
/
main.py
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
import streamlit as st
from text import generate_story_from_text
from image import generate_image_caption, generate_story_from_image_caption
st.set_page_config(
page_title="AI Story Generator", layout="wide", page_icon="📖"
)
st.markdown(
"""
<h1 style='text-align: center; color: #FF4500;'>My AI Storyteller 📚</h1>
""",
unsafe_allow_html=True
)
st.sidebar.markdown("### Select the genre/theme of the story:")
story_theme = st.sidebar.radio("Genre", ("Horror 👻", "Action 🏃♂️", "Romance ❤️", "Comedy 😂", "Historical ⏳"))
selected_theme = story_theme.split()[0].strip()
theme_based_prompts = {
"Horror": "Write a horror story that ends mysteriously using:\n",
"Action": "Write a story with lots of action using:\n ",
"Romance": "Write a romantic story using:\n ",
"Comedy": "Write a funny story using:\n ",
"Historical": "Write a story based on a historical event with the help of the input:\n ",
}
st.markdown("## Choose the input type for generating the story")
input_type = st.radio("Input type", ("Text ✏️", "Image 🖼️"))
if input_type == "Text ✏️":
st.markdown("### Enter the sentences you want to have your story revolve around:")
input_text = st.text_area("Enter the text here", height=100, value="Once upon a time in a faraway land...")
if st.button("🚀 Generate story"):
try:
story = generate_story_from_text(input_text)
st.markdown(f"**Generated Story:**\n\n{story}")
except Exception as e:
st.error(f"Error: {e}")
if input_type == "Image 🖼️":
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
if uploaded_file is not None:
st.image(uploaded_file, caption="Uploaded Image", use_column_width=False, width=500)
if st.button("🚀 Generate story"):
try:
image_bytes = uploaded_file.read()
caption = generate_image_caption(image_bytes)
story = generate_story_from_image_caption(caption)
st.markdown(f"**Generated Story from Image:**\n\n{story}")
except Exception as e:
st.error(f"Error: {e}")