mayf commited on
Commit
6bc44b9
·
verified ·
1 Parent(s): ea2414f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -39
app.py CHANGED
@@ -40,17 +40,22 @@ else:
40
 
41
  # 2) Generate caption
42
  with st.spinner("🔍 Generating caption..."):
43
- buf = BytesIO()
44
- img.save(buf, format="PNG")
45
- cap_out = caption_client(data=buf.getvalue())
46
-
47
- # Handle caption response
48
- if isinstance(cap_out, list) and cap_out:
49
- cap_text = cap_out[0].get("generated_text", "").strip()
50
- elif isinstance(cap_out, dict):
51
- cap_text = cap_out.get("generated_text", "").strip()
52
- else:
53
- cap_text = str(cap_out).strip()
 
 
 
 
 
54
 
55
  if not cap_text:
56
  st.error("😕 Couldn’t generate a caption. Try another image.")
@@ -68,36 +73,40 @@ else:
68
  "Story:"
69
  )
70
 
71
- # 4) Generate story with proper parameter structure
72
- with st.spinner("✍️ Generating story..."):
73
- try:
74
- # Generation parameters must be nested under 'parameters'
75
- story_out = story_client(
76
- inputs=prompt,
77
- params={
78
- "max_new_tokens": 150,
79
- "do_sample": True,
80
- "temperature": 0.8,
81
- "top_p": 0.9,
82
- "top_k": 50,
83
- "repetition_penalty": 1.1,
84
- "no_repeat_ngram_size": 2
85
- }
86
- )
87
-
88
- # Handle different response formats
89
- if isinstance(story_out, list):
90
- story_text = story_out[0].get("generated_text", "")
91
- else:
92
- story_text = story_out.get("generated_text", "")
93
-
94
- # Extract story content after the prompt
95
- story = story_text.split("Story:")[-1].strip()
96
-
97
- except Exception as e:
98
- st.error(f"🚨 Story generation failed: {str(e)}")
 
99
  st.stop()
100
 
 
 
 
101
  # 5) Text-to-Speech
102
  with st.spinner("🔊 Converting to speech..."):
103
  try:
 
40
 
41
  # 2) Generate caption
42
  with st.spinner("🔍 Generating caption..."):
43
+ try:
44
+ buf = BytesIO()
45
+ img.save(buf, format="PNG")
46
+ cap_out = caption_client(data=buf.getvalue())
47
+
48
+ # Handle caption response
49
+ if isinstance(cap_out, list) and cap_out:
50
+ cap_text = cap_out[0].get("generated_text", "").strip()
51
+ elif isinstance(cap_out, dict):
52
+ cap_text = cap_out.get("generated_text", "").strip()
53
+ else:
54
+ cap_text = str(cap_out).strip()
55
+
56
+ except Exception as e:
57
+ st.error(f"🚨 Caption generation failed: {str(e)}")
58
+ st.stop()
59
 
60
  if not cap_text:
61
  st.error("😕 Couldn’t generate a caption. Try another image.")
 
73
  "Story:"
74
  )
75
 
76
+ # 4) Generate story
77
+ with st.spinner("✍️ Generating story..."):
78
+ try:
79
+ story_out = story_client(
80
+ inputs=prompt,
81
+ parameters={
82
+ "max_new_tokens": 250,
83
+ "temperature": 0.7,
84
+ "top_p": 0.9,
85
+ "repetition_penalty": 1.1,
86
+ "do_sample": True
87
+ }
88
+ )
89
+
90
+ # Parse response
91
+ if isinstance(story_out, list):
92
+ story_text = story_out[0].get("generated_text", "")
93
+ else:
94
+ story_text = story_out.get("generated_text", "")
95
+
96
+ # Clean up output
97
+ story = story_text.split("Story:")[-1].strip()
98
+
99
+ except Exception as e:
100
+ st.error(f"🚨 Story generation failed: {str(e)}")
101
+ st.stop()
102
+
103
+ if not story:
104
+ st.error("😕 Couldn’t generate a story. Please try again!")
105
  st.stop()
106
 
107
+ st.markdown("**Story:**")
108
+ st.write(story)
109
+
110
  # 5) Text-to-Speech
111
  with st.spinner("🔊 Converting to speech..."):
112
  try: