Spaces:
Sleeping
Sleeping
Commit
·
978360d
1
Parent(s):
d48c324
add: image generation and refactor prompt
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
-
|
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():
|
@@ -47,7 +47,23 @@ def main():
|
|
47 |
|
48 |
st.success("Content Generated Successfully!")
|
49 |
st.write("### Results")
|
50 |
-
st.write(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
except Exception as e:
|
53 |
st.error(f"An error occurred: {str(e)}")
|
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
+
import requests
|
4 |
import streamlit as st
|
|
|
5 |
from src.pipeline.main import LearnableAI
|
6 |
from src.services.use_case_three.story_generator import StoryGenerator
|
7 |
+
from src.services.image_generation.image_gen import image_generation
|
8 |
|
9 |
|
10 |
def main():
|
|
|
47 |
|
48 |
st.success("Content Generated Successfully!")
|
49 |
st.write("### Results")
|
50 |
+
st.write(result.split("</think>")[1].split("Image Generation Prompt")[0])
|
51 |
+
image_prompt = result.split("Image Generation Prompt")[1]
|
52 |
+
input = {
|
53 |
+
"prompt": image_prompt,
|
54 |
+
"go_fast": True,
|
55 |
+
"megapixels": "1",
|
56 |
+
"num_outputs": 1,
|
57 |
+
"aspect_ratio": "1:1",
|
58 |
+
"output_format": "webp",
|
59 |
+
"output_quality": 80,
|
60 |
+
"num_inference_steps": 4
|
61 |
+
}
|
62 |
+
output_image_url = image_generation(input)
|
63 |
+
image_bytes = requests.get(output_image_url).content
|
64 |
+
st.write("### Generated Image")
|
65 |
+
st.image(image_bytes, caption="Generated Image")
|
66 |
+
|
67 |
|
68 |
except Exception as e:
|
69 |
st.error(f"An error occurred: {str(e)}")
|
src/services/image_generation/__init__.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
project @ LearnableAI
|
3 |
+
created @ 2025-01-17
|
4 |
+
author @ github.com/ishworrsubedii
|
5 |
+
"""
|
src/services/image_generation/image_gen.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import replicate
|
2 |
+
|
3 |
+
|
4 |
+
def image_generation(prompt: dict):
|
5 |
+
output = replicate.run(
|
6 |
+
"black-forest-labs/flux-schnell",
|
7 |
+
input=prompt,
|
8 |
+
)
|
9 |
+
|
10 |
+
return output[0]
|
src/services/use_case_one/word_to_sentence.py
CHANGED
@@ -89,13 +89,13 @@ class UseCaseOne:
|
|
89 |
completion = self.client.chat.completions.create(
|
90 |
messages=[{
|
91 |
"role": "system",
|
92 |
-
"content": "You are an expert in creating educational content for children with ADHD."
|
93 |
},
|
94 |
{
|
95 |
"role": "user",
|
96 |
"content": prompt
|
97 |
}],
|
98 |
-
model="
|
99 |
temperature=1.0, # if increase temperature, it will generate more creative content
|
100 |
max_tokens=2000,
|
101 |
top_p=1,
|
|
|
89 |
completion = self.client.chat.completions.create(
|
90 |
messages=[{
|
91 |
"role": "system",
|
92 |
+
"content": "You are an expert in creating educational content for children with ADHD. and you need to generate some educational content for children to understand the concept of the given words."
|
93 |
},
|
94 |
{
|
95 |
"role": "user",
|
96 |
"content": prompt
|
97 |
}],
|
98 |
+
model="deepseek-r1-distill-llama-70b",
|
99 |
temperature=1.0, # if increase temperature, it will generate more creative content
|
100 |
max_tokens=2000,
|
101 |
top_p=1,
|
src/services/use_case_three/story_generator.py
CHANGED
@@ -9,7 +9,7 @@ class StoryGenerator:
|
|
9 |
def process_image(self, image_path):
|
10 |
try:
|
11 |
img = Image.open(image_path)
|
12 |
-
prompt = self.
|
13 |
|
14 |
response = self.model.generate_content([prompt, img])
|
15 |
return response.text
|
@@ -17,17 +17,17 @@ class StoryGenerator:
|
|
17 |
except Exception as e:
|
18 |
return f"Error processing image: {str(e)}"
|
19 |
|
20 |
-
def
|
21 |
-
return """
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
9 |
def process_image(self, image_path):
|
10 |
try:
|
11 |
img = Image.open(image_path)
|
12 |
+
prompt = self.createstory_prompt()
|
13 |
|
14 |
response = self.model.generate_content([prompt, img])
|
15 |
return response.text
|
|
|
17 |
except Exception as e:
|
18 |
return f"Error processing image: {str(e)}"
|
19 |
|
20 |
+
def createstory_prompt(self):
|
21 |
+
return """You are a skilled children's story writer. Your task is to create a simple, engaging children's story that naturally incorporates the provided image captions into a cohesive narrative.
|
22 |
+
Context:
|
23 |
+
- Target audience: Young children
|
24 |
+
- Output format: Markdown file
|
25 |
+
- Story length: 4-5 sentences in line by line separated by \n
|
26 |
+
Image Captions to incorporate:
|
27 |
+
{captions}
|
28 |
+
Writing Guidelines:
|
29 |
+
- Use age-appropriate vocabulary
|
30 |
+
- Avoid complex sentence structures
|
31 |
+
- Create clear connections between events
|
32 |
+
- Keep the narrative positive and engaging
|
33 |
+
Your response should be formatted as a proper Markdown file"""
|