LTH001 commited on
Commit
cb3f72b
·
verified ·
1 Parent(s): 893d08c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -1,30 +1,53 @@
 
 
1
  import streamlit as st
2
  from transformers import pipeline
 
 
3
 
4
- # Load the text classification model pipeline
5
- classifier = pipeline("text-classification",model='isom5240ust/bert-base-uncased-emotion', return_all_scores=True)
 
 
 
 
 
6
 
7
- # Streamlit application title
8
- st.title("Text Classification for you")
9
- st.write("Classification for 6 emotions: sadness, joy, love, anger, fear, surprise")
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Text input for user to enter the text to classify
12
- text = st.text_area("Enter the text to classify", "")
 
13
 
14
- # Perform text classification when the user clicks the "Classify" button
15
- if st.button("Classify"):
16
- # Perform text classification on the input text
17
- results = classifier(text)[0]
18
 
19
- # Display the classification result
20
- max_score = float('-inf')
21
- max_label = ''
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- for result in results:
24
- if result['score'] > max_score:
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()