Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,33 +29,31 @@ def generate_content(image):
|
|
29 |
st.write("**π What's in the picture: π**")
|
30 |
st.write(caption)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
)
|
39 |
-
|
40 |
-
# Generate raw story
|
41 |
-
raw = storyer(
|
42 |
-
prompt,
|
43 |
-
max_new_tokens=150,
|
44 |
-
temperature=0.7,
|
45 |
-
top_p=0.9,
|
46 |
-
no_repeat_ngram_size=2,
|
47 |
-
return_full_text=False
|
48 |
-
)[0]["generated_text"].strip()
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
st.write("**π Your funny story: π**")
|
61 |
st.write(story)
|
@@ -81,7 +79,7 @@ uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "p
|
|
81 |
if uploaded_image is None:
|
82 |
st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here!", use_container_width=True)
|
83 |
else:
|
84 |
-
st.image(uploaded_image, caption="Your Picture
|
85 |
|
86 |
if st.button("Generate a story"):
|
87 |
if uploaded_image is not None:
|
|
|
29 |
st.write("**π What's in the picture: π**")
|
30 |
st.write(caption)
|
31 |
|
32 |
+
# Create prompt for story
|
33 |
+
prompt = (
|
34 |
+
f"Write a funny, interesting children's story centered on this scene: {caption}\n"
|
35 |
+
f"Story in third-person narrative, describing this scene exactly: {caption} "
|
36 |
+
f"Mention the exact place, location, or venue within {caption}. "
|
37 |
+
f"Avoid numbers, random letter combinations, and single-letter words.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Generate raw story with optimized parameters
|
40 |
+
raw = storyer(
|
41 |
+
prompt,
|
42 |
+
max_new_tokens=100,
|
43 |
+
temperature=0.6,
|
44 |
+
top_p=0.85,
|
45 |
+
no_repeat_ngram_size=0,
|
46 |
+
return_full_text=False
|
47 |
+
)[0]["generated_text"].strip()
|
48 |
+
|
49 |
+
# Combine cleaning and word trimming in one step
|
50 |
+
# Use regex to keep only allowed characters and remove single-letter words
|
51 |
+
allowed_pattern = re.compile(r'[a-zA-Z0-9.,!?"\'-]+\b(?<!\b\w\b)')
|
52 |
+
clean_raw = ' '.join(word for word in re.findall(allowed_pattern, raw) if len(word) > 1)
|
53 |
+
|
54 |
+
# Split into words and trim to 100 words
|
55 |
+
words = clean_raw.split()
|
56 |
+
story = " ".join(words[:100])
|
57 |
|
58 |
st.write("**π Your funny story: π**")
|
59 |
st.write(story)
|
|
|
79 |
if uploaded_image is None:
|
80 |
st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here!", use_container_width=True)
|
81 |
else:
|
82 |
+
st.image(uploaded_image, caption="Your Picture ", use_container_width=True)
|
83 |
|
84 |
if st.button("Generate a story"):
|
85 |
if uploaded_image is not None:
|