Spaces:
Sleeping
Sleeping
Commit
·
d48c324
1
Parent(s):
ca3fb9c
add: use case three
Browse files- app.py +45 -2
- requirements.txt +2 -1
- src/pipeline/main.py +2 -2
- src/services/use_case_three/story_generator.py +33 -0
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import tempfile
|
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
from src.pipeline.main import LearnableAI
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def main():
|
|
@@ -14,7 +15,7 @@ def main():
|
|
| 14 |
st.sidebar.header("Configuration")
|
| 15 |
use_case = st.sidebar.radio(
|
| 16 |
"Select Use Case",
|
| 17 |
-
["Use Case: 1(Word-Sentences)", "Use Case: 2(Image Describer)"]
|
| 18 |
)
|
| 19 |
|
| 20 |
difficulty = st.sidebar.selectbox(
|
|
@@ -53,7 +54,7 @@ def main():
|
|
| 53 |
else:
|
| 54 |
st.warning("Please fill in both words and interest fields!")
|
| 55 |
|
| 56 |
-
|
| 57 |
st.header("Image Description")
|
| 58 |
|
| 59 |
upload_method = st.radio(
|
|
@@ -93,5 +94,47 @@ def main():
|
|
| 93 |
st.error(f"An error occurred while processing the image: {str(e)}")
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if __name__ == "__main__":
|
| 97 |
main()
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
from src.pipeline.main import LearnableAI
|
| 7 |
+
from src.services.use_case_three.story_generator import StoryGenerator
|
| 8 |
|
| 9 |
|
| 10 |
def main():
|
|
|
|
| 15 |
st.sidebar.header("Configuration")
|
| 16 |
use_case = st.sidebar.radio(
|
| 17 |
"Select Use Case",
|
| 18 |
+
["Use Case: 1(Word-Sentences)", "Use Case: 2(Image Describer)", "Use Case: 3(Story Generator From Image)"]
|
| 19 |
)
|
| 20 |
|
| 21 |
difficulty = st.sidebar.selectbox(
|
|
|
|
| 54 |
else:
|
| 55 |
st.warning("Please fill in both words and interest fields!")
|
| 56 |
|
| 57 |
+
elif use_case == "Use Case: 2(Image Describer)":
|
| 58 |
st.header("Image Description")
|
| 59 |
|
| 60 |
upload_method = st.radio(
|
|
|
|
| 94 |
st.error(f"An error occurred while processing the image: {str(e)}")
|
| 95 |
|
| 96 |
|
| 97 |
+
elif use_case == "Use Case: 3(Story Generator From Image)":
|
| 98 |
+
st.title("Story Generator from Images")
|
| 99 |
+
st.subheader("Upload up to 3 images and generate a children's story")
|
| 100 |
+
|
| 101 |
+
uploaded_files = st.file_uploader(
|
| 102 |
+
"Upload up to 3 images",
|
| 103 |
+
type=["jpg", "jpeg", "png"],
|
| 104 |
+
accept_multiple_files=True
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
if uploaded_files:
|
| 108 |
+
if len(uploaded_files) > 3:
|
| 109 |
+
st.error("You can upload a maximum of 3 images.")
|
| 110 |
+
else:
|
| 111 |
+
st.write("Uploaded Images:")
|
| 112 |
+
for uploaded_file in uploaded_files:
|
| 113 |
+
st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)
|
| 114 |
+
|
| 115 |
+
if st.button("Generate Story"):
|
| 116 |
+
story_generator = StoryGenerator()
|
| 117 |
+
story_parts = []
|
| 118 |
+
|
| 119 |
+
try:
|
| 120 |
+
for uploaded_file in uploaded_files:
|
| 121 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
|
| 122 |
+
temp_file.write(uploaded_file.read())
|
| 123 |
+
temp_image_path = temp_file.name
|
| 124 |
+
|
| 125 |
+
story_part = story_generator.process_image(temp_image_path)
|
| 126 |
+
story_parts.append(story_part)
|
| 127 |
+
|
| 128 |
+
os.remove(temp_image_path)
|
| 129 |
+
|
| 130 |
+
full_story = "\n\n".join(story_parts)
|
| 131 |
+
st.success("Story Generated Successfully!")
|
| 132 |
+
st.write("### Your Story")
|
| 133 |
+
st.write(full_story)
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
st.error(f"An error occurred: {str(e)}")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
if __name__ == "__main__":
|
| 140 |
main()
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
groq
|
| 2 |
streamlit
|
| 3 |
-
google-generativeai
|
|
|
|
|
|
| 1 |
groq
|
| 2 |
streamlit
|
| 3 |
+
google-generativeai
|
| 4 |
+
replicate
|
src/pipeline/main.py
CHANGED
|
@@ -14,8 +14,8 @@ from src.services.use_case_two.image_describer import ImageDescriptionGenerator
|
|
| 14 |
|
| 15 |
class LearnableAI:
|
| 16 |
def __init__(self):
|
| 17 |
-
self.groq_client = Groq(api_key=os.getenv(
|
| 18 |
-
self.genai = genai.configure(api_key=os.getenv(
|
| 19 |
|
| 20 |
self.use_case_one = UseCaseOne(client=self.groq_client)
|
| 21 |
|
|
|
|
| 14 |
|
| 15 |
class LearnableAI:
|
| 16 |
def __init__(self):
|
| 17 |
+
self.groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 18 |
+
self.genai = genai.configure(api_key=os.getenv("GENAI_API_KEY"))
|
| 19 |
|
| 20 |
self.use_case_one = UseCaseOne(client=self.groq_client)
|
| 21 |
|
src/services/use_case_three/story_generator.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class StoryGenerator:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.model = genai.GenerativeModel('gemini-1.5-flash')
|
| 8 |
+
|
| 9 |
+
def process_image(self, image_path):
|
| 10 |
+
try:
|
| 11 |
+
img = Image.open(image_path)
|
| 12 |
+
prompt = self._create_story_prompt()
|
| 13 |
+
|
| 14 |
+
response = self.model.generate_content([prompt, img])
|
| 15 |
+
return response.text
|
| 16 |
+
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error processing image: {str(e)}"
|
| 19 |
+
|
| 20 |
+
def _create_story_prompt(self):
|
| 21 |
+
return """
|
| 22 |
+
Create a delightful and imaginative children's story inspired by the following captions from up to 4 images. Combine all captions into a single storyline that flows naturally. Use simple, child-friendly language and a playful tone suitable for children aged 4–8.
|
| 23 |
+
|
| 24 |
+
Ensure the story has:
|
| 25 |
+
|
| 26 |
+
A central character (e.g., a curious child, an animal, or a magical creature) that ties the events together.
|
| 27 |
+
A clear beginning, middle, and end with an engaging plot.
|
| 28 |
+
Elements of wonder and adventure to capture a child's imagination.
|
| 29 |
+
A gentle life lesson or moral, encouraging themes like kindness, courage, or curiosity.
|
| 30 |
+
Include vivid descriptions of settings and actions to match the imagery, and ensure the captions inspire key moments in the story.
|
| 31 |
+
|
| 32 |
+
Please incorporate all these captions seamlessly into the storyline
|
| 33 |
+
"""
|