Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,53 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
-
|
15 |
-
if st.button("Classify"):
|
16 |
-
# Perform text classification on the input text
|
17 |
-
results = classifier(text)[0]
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
max_score = result['score']
|
26 |
-
max_label = result['label']
|
27 |
-
|
28 |
-
st.write("Text:", text)
|
29 |
-
st.write("Label:", max_label)
|
30 |
-
st.write("Score:", max_score)
|
|
|
1 |
+
# import part
|
2 |
+
!pip install streamlit pyngrok
|
3 |
import streamlit as st
|
4 |
from transformers import pipeline
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
|
8 |
+
|
9 |
+
# function part
|
10 |
+
def generate_image_caption(image):
|
11 |
+
"""Generates a caption for the given image using a pre-trained model."""
|
12 |
+
img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
13 |
+
result = img2caption(image)
|
14 |
+
return result[0]['generated_text']
|
15 |
|
16 |
+
def text2story(text):
|
17 |
+
"""Generates a children's story from text input with genre adaptation"""
|
18 |
+
story_prompt = f"Create a funny 100-word story for 8-year-olds about: {text}. Include: "
|
19 |
+
story_prompt += "1) A silly character 2) Magical object 3) Sound effects 4) Happy ending"
|
20 |
+
|
21 |
+
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
22 |
+
story_text = pipe(
|
23 |
+
story_prompt,
|
24 |
+
max_new_tokens=200,
|
25 |
+
temperature=0.9,
|
26 |
+
top_k=50
|
27 |
+
)[0]['generated_text']
|
28 |
+
return story_text.split("Happy ending")[-1].strip() # Clean output
|
29 |
|
30 |
+
def main():
|
31 |
+
st.title("📖 Image Story Generator")
|
32 |
+
st.write("Upload an image and get a magical children's story!")
|
33 |
|
34 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
35 |
|
36 |
+
if uploaded_image:
|
37 |
+
image = Image.open(uploaded_image).convert("RGB")
|
38 |
+
st.image(image, use_column_width=True)
|
39 |
+
|
40 |
+
with st.spinner("✨ Analyzing image..."):
|
41 |
+
caption = generate_image_caption(image)
|
42 |
+
|
43 |
+
st.subheader("Image Understanding")
|
44 |
+
st.write(caption)
|
45 |
+
|
46 |
+
with st.spinner("📖 Writing story..."):
|
47 |
+
story = text2story(caption)
|
48 |
+
|
49 |
+
st.subheader("Magical Story")
|
50 |
+
st.write(story)
|
51 |
|
52 |
+
if __name__ == "__main__":
|
53 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|