File size: 6,268 Bytes
ca3fb9c
 
978360d
0cd4e64
 
d48c324
978360d
0cd4e64
 
 
ca3fb9c
0cd4e64
 
 
ca3fb9c
 
 
d48c324
ca3fb9c
 
 
 
 
0cd4e64
 
55e1db0
2cf1d6e
 
 
ca3fb9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
978360d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca3fb9c
 
 
 
 
0cd4e64
d48c324
ca3fb9c
2cf1d6e
0cd4e64
ca3fb9c
 
 
 
 
 
 
 
 
 
0cd4e64
ca3fb9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cd4e64
 
d48c324
 
2cf1d6e
d48c324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cd4e64
e289f8d
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import tempfile
import requests
import streamlit as st
from src.pipeline.main import LearnableAI
from src.services.use_case_three.story_generator import StoryGenerator
from src.services.image_generation.image_gen import image_generation


def main():
    st.title("LearnableAI")

    learnable_ai = LearnableAI()

    st.sidebar.header("Configuration")
    use_case = st.sidebar.radio(
        "Select Use Case",
        ["Use Case: 1(Word-Sentences)", "Use Case: 2(Image Describer)", "Use Case: 3(Story Generator From Image)"]
    )

    difficulty = st.sidebar.selectbox(
        "Select Difficulty Level",
        ["Simple", "Easy", "Challenging", "Advanced"]
    )

    if use_case == "Use Case: 1(Word-Sentences)":
        st.header("Word to Sentence Generation")
        st.write(
            "Enter a list of words and the child interest to generate the content for eg.Words: Apple, Banana interest: Ironman")

        words = st.text_area(
            "Words:",
            "",
            help="Enter multiple words separated by commas"
        )

        interest = st.text_input("Child Interest:", "",
                                 help="Enter the interest area for the content ie. Superheroes, Animals, etc.")

        if st.button("Generate Content", key="text_process"):
            if words and interest:
                try:
                    word_list = [w.strip() for w in words.split(",")] if "," in words else [words.strip()]
                    result = learnable_ai.word_to_sentence(
                        words=word_list,
                        interest=interest,
                        difficulties=difficulty
                    )

                    st.success("Content Generated Successfully!")
                    st.write("### Results")
                    st.write(result.split("</think>")[1].split("Image Generation Prompt")[0])
                    image_prompt = result.split("Image Generation Prompt")[1]
                    input = {
                        "prompt": image_prompt,
                        "go_fast": True,
                        "megapixels": "1",
                        "num_outputs": 1,
                        "aspect_ratio": "1:1",
                        "output_format": "webp",
                        "output_quality": 80,
                        "num_inference_steps": 4
                    }
                    output_image_url = image_generation(input)
                    image_bytes = requests.get(output_image_url).content
                    st.write("### Generated Image")
                    st.image(image_bytes, caption="Generated Image")


                except Exception as e:
                    st.error(f"An error occurred: {str(e)}")
            else:
                st.warning("Please fill in both words and interest fields!")

    elif use_case == "Use Case: 2(Image Describer)":
        st.header("Image Description")
        st.write("Upload an image or capture from camera to generate to generate the description of the image")

        upload_method = st.radio(
            "Choose Upload Method",
            ["Upload File", "Capture from Camera"]
        )

        if upload_method == "Upload File":
            image_file = st.file_uploader(
                "Upload an image",
                type=["jpg", "jpeg", "png"]
            )
        else:
            image_file = st.camera_input("Take a picture")

        if image_file is not None:
            st.image(image_file, caption="Uploaded Image", use_column_width=True)

            if st.button("Generate Description", key="image_process"):
                try:  # temp image file
                    with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
                        temp_file.write(image_file.read())
                        temp_image_path = temp_file.name

                    result = learnable_ai.image_describer(
                        image_path=temp_image_path,
                        difficulties=difficulty
                    )

                    st.success("Image Processed Successfully!")
                    st.write("### Image Description Results")
                    st.write(result)

                    os.remove(temp_image_path)  # remove temp image file

                except Exception as e:
                    st.error(f"An error occurred while processing the image: {str(e)}")


    elif use_case == "Use Case: 3(Story Generator From Image)":
        st.title("Story Generator from Images")
        st.write("Story writer from the images. Upload up to 3 images to generate a children's story.")
        st.subheader("Upload up to 3 images and generate a children's story")

        uploaded_files = st.file_uploader(
            "Upload up to 3 images",
            type=["jpg", "jpeg", "png"],
            accept_multiple_files=True
        )

        if uploaded_files:
            if len(uploaded_files) > 3:
                st.error("You can upload a maximum of 3 images.")
            else:
                st.write("Uploaded Images:")
                for uploaded_file in uploaded_files:
                    st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)

                if st.button("Generate Story"):
                    story_generator = StoryGenerator()
                    story_parts = []

                    try:
                        for uploaded_file in uploaded_files:
                            with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
                                temp_file.write(uploaded_file.read())
                                temp_image_path = temp_file.name

                            story_part = story_generator.process_image(temp_image_path)
                            story_parts.append(story_part)

                            os.remove(temp_image_path)

                        full_story = "\n\n".join(story_parts)
                        st.success("Story Generated Successfully!")
                        st.write("### Your Story")
                        st.write(full_story)

                    except Exception as e:
                        st.error(f"An error occurred: {str(e)}")


if __name__ == "__main__":
    main()