Update app.py
Browse files
app.py
CHANGED
@@ -1,154 +1,72 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# --- Constants & Setup ---
|
19 |
-
st.title("📖✨ Turn Images into Children's Stories")
|
20 |
-
|
21 |
-
# --- Model Loading (Cached) ---
|
22 |
-
@st.cache_resource(show_spinner=False)
|
23 |
-
def load_models():
|
24 |
-
# Image captioning model
|
25 |
-
captioner = pipeline(
|
26 |
-
"image-to-text",
|
27 |
-
model="Salesforce/blip-image-captioning-base",
|
28 |
-
device=0 if torch.cuda.is_available() else -1
|
29 |
)
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
storyteller = pipeline(
|
34 |
-
"text-generation",
|
35 |
-
model="Deepthoughtworks/gpt-neo-2.7B__low-cpu",
|
36 |
tokenizer=tokenizer,
|
37 |
-
|
38 |
-
torch_dtype=torch.float32, # Changed to float32 for better CPU compatibility
|
39 |
-
max_new_tokens=150, # Reduced length for faster generation
|
40 |
-
temperature=0.85,
|
41 |
-
top_k=40,
|
42 |
-
top_p=0.92,
|
43 |
-
repetition_penalty=1.15,
|
44 |
-
pad_token_id=tokenizer.eos_token_id # Added for padding control
|
45 |
)
|
46 |
-
|
47 |
-
return captioner, storyteller
|
48 |
-
|
49 |
-
caption_pipe, story_pipe = load_models()
|
50 |
-
|
51 |
-
# --- Main Application Flow ---
|
52 |
-
uploaded_image = st.file_uploader(
|
53 |
-
"Upload a children's book style image:",
|
54 |
-
type=["jpg", "jpeg", "png"]
|
55 |
-
)
|
56 |
-
|
57 |
-
if uploaded_image:
|
58 |
-
# Process image
|
59 |
-
image = Image.open(uploaded_image).convert("RGB")
|
60 |
-
st.image(image, use_container_width=True) # Fixed deprecated parameter
|
61 |
-
|
62 |
-
# Generate caption
|
63 |
-
with st.spinner("🔍 Analyzing image..."):
|
64 |
-
try:
|
65 |
-
caption_result = caption_pipe(image)
|
66 |
-
image_caption = caption_result[0].get("generated_text", "").strip()
|
67 |
-
except Exception as e:
|
68 |
-
st.error(f"❌ Image analysis failed: {str(e)}")
|
69 |
-
st.stop()
|
70 |
-
|
71 |
-
if not image_caption:
|
72 |
-
st.error("❌ Couldn't understand this image. Please try another!")
|
73 |
-
st.stop()
|
74 |
-
|
75 |
-
st.success(f"**Image Understanding:** {image_caption}")
|
76 |
-
|
77 |
-
# Create story prompt
|
78 |
-
story_prompt = f"""Write a 50 to 100 words children's story based on: {image_caption}
|
79 |
-
Requirements:
|
80 |
-
- Exclude your thinking process
|
81 |
-
Story:"""
|
82 |
-
|
83 |
-
# Generate story with progress
|
84 |
-
progress_bar = st.progress(0)
|
85 |
-
status_text = st.empty()
|
86 |
-
|
87 |
-
try:
|
88 |
-
with st.spinner("📝 Crafting magical story..."):
|
89 |
-
start_time = time.time()
|
90 |
-
|
91 |
-
def update_progress(step):
|
92 |
-
progress = min(step/5, 1.0)
|
93 |
-
progress_bar.progress(progress)
|
94 |
-
status_text.text(f"Step {int(step)}/5: {'📖'*int(step)}")
|
95 |
-
|
96 |
-
update_progress(1)
|
97 |
-
story_result = story_pipe(
|
98 |
-
story_prompt,
|
99 |
-
do_sample=True,
|
100 |
-
num_return_sequences=1
|
101 |
-
)
|
102 |
-
|
103 |
-
update_progress(4)
|
104 |
-
generation_time = time.time() - start_time
|
105 |
-
st.info(f"Story generated in {generation_time:.1f} seconds")
|
106 |
-
|
107 |
-
# Process output
|
108 |
-
raw_story = story_result[0]['generated_text']
|
109 |
-
clean_story = raw_story.split("Story:")[-1].strip()
|
110 |
-
clean_story = re.sub(r'\n+', '\n\n', clean_story) # Improve paragraph spacing
|
111 |
-
|
112 |
-
# Format story text
|
113 |
-
final_story = ""
|
114 |
-
for paragraph in clean_story.split('\n\n'):
|
115 |
-
paragraph = paragraph.strip()
|
116 |
-
if paragraph:
|
117 |
-
sentences = []
|
118 |
-
for sent in re.split(r'(?<=[.!?]) +', paragraph):
|
119 |
-
sent = sent.strip()
|
120 |
-
if sent:
|
121 |
-
if len(sent) > 1 and not sent.endswith(('.','!','?')):
|
122 |
-
sent += '.'
|
123 |
-
sentences.append(sent[0].upper() + sent[1:])
|
124 |
-
final_story += ' '.join(sentences) + '\n\n'
|
125 |
-
|
126 |
-
update_progress(5)
|
127 |
-
time.sleep(0.5)
|
128 |
-
|
129 |
-
except Exception as e:
|
130 |
-
st.error(f"❌ Story generation failed: {str(e)}")
|
131 |
-
st.stop()
|
132 |
-
|
133 |
-
finally:
|
134 |
-
progress_bar.empty()
|
135 |
-
status_text.empty()
|
136 |
-
|
137 |
-
# Display story
|
138 |
-
st.subheader("✨ Your Magical Story")
|
139 |
-
st.write(final_story.strip())
|
140 |
-
|
141 |
-
# Audio conversion
|
142 |
-
with st.spinner("🔊 Creating audio version..."):
|
143 |
-
try:
|
144 |
-
audio = gTTS(text=final_story, lang="en", slow=False)
|
145 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
146 |
-
audio.save(tmp_file.name)
|
147 |
-
st.audio(tmp_file.name, format="audio/mp3")
|
148 |
-
except Exception as e:
|
149 |
-
st.error(f"❌ Audio conversion failed: {str(e)}")
|
150 |
|
151 |
-
#
|
152 |
-
st.
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
3 |
+
from huggingface_hub import login
|
4 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
5 |
+
from keybert import KeyBERT
|
6 |
+
|
7 |
+
# ─── Cached resource: Sentiment Analysis Pipeline ────────────────────────────
|
8 |
+
@st.cache_resource
|
9 |
+
def load_sentiment_pipeline():
|
10 |
+
model_name = "mayf/amazon_reviews_bert_ft"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
12 |
+
model_name,
|
13 |
+
use_auth_token=True
|
14 |
+
)
|
15 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
16 |
+
model_name,
|
17 |
+
use_auth_token=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
+
return pipeline(
|
20 |
+
"sentiment-analysis",
|
21 |
+
model=model,
|
|
|
|
|
|
|
22 |
tokenizer=tokenizer,
|
23 |
+
return_all_scores=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# ─── Cached resource: KeyBERT Model ─────────────────────────────────────────
|
27 |
+
@st.cache_resource
|
28 |
+
def load_keybert_model():
|
29 |
+
return KeyBERT(model="all-MiniLM-L6-v2")
|
30 |
+
|
31 |
+
# ─── Main Application ───────────────────────────────────────────────────────
|
32 |
+
def main():
|
33 |
+
sentiment_pipeline = load_sentiment_pipeline()
|
34 |
+
kw_model = load_keybert_model()
|
35 |
+
|
36 |
+
st.set_page_config(page_title="Review Analyzer", layout="wide")
|
37 |
+
st.title("📊 Review Sentiment & Keyword Analyzer")
|
38 |
+
|
39 |
+
review = st.text_area("Enter your review:")
|
40 |
+
|
41 |
+
if st.button("Analyze Review"):
|
42 |
+
if not review:
|
43 |
+
st.warning("Please enter a review to analyze.")
|
44 |
+
return
|
45 |
+
|
46 |
+
# Sentiment Analysis
|
47 |
+
scores = sentiment_pipeline(review)[0]
|
48 |
+
sentiment_results = {item['label']: round(item['score'], 4) for item in scores}
|
49 |
+
st.subheader("Sentiment Scores")
|
50 |
+
st.json(sentiment_results)
|
51 |
+
|
52 |
+
# Keyword Extraction
|
53 |
+
keywords = kw_model.extract_keywords(
|
54 |
+
review,
|
55 |
+
keyphrase_ngram_range=(1, 2),
|
56 |
+
stop_words="english",
|
57 |
+
top_n=5
|
58 |
+
)
|
59 |
+
st.subheader("Top Keywords")
|
60 |
+
for kw, score in keywords:
|
61 |
+
st.write(f"- **{kw}** (Score: {score:.4f})")
|
62 |
+
|
63 |
+
# Composite Score
|
64 |
+
avg_sentiment = sum(sentiment_results.values()) / len(sentiment_results)
|
65 |
+
st.subheader("Composite Score with Keywords")
|
66 |
+
st.write(f"Average Sentiment Score: {avg_sentiment:.4f}")
|
67 |
+
st.write("Keywords used for this score:")
|
68 |
+
st.write(', '.join([kw for kw, _ in keywords]))
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|
72 |
|