Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from io import BytesIO
|
|
4 |
from huggingface_hub import InferenceApi
|
5 |
from gtts import gTTS
|
6 |
import requests
|
|
|
7 |
import tempfile
|
8 |
import time
|
9 |
import threading
|
@@ -62,7 +63,7 @@ def process_image(uploaded_file):
|
|
62 |
st.error(f"Image processing error: {type(e).__name__}: {e}")
|
63 |
st.stop()
|
64 |
|
65 |
-
# —––––––– Helper: Generate Story with
|
66 |
def generate_story(prompt: str, caption: str) -> str:
|
67 |
api_url = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
68 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
@@ -79,9 +80,19 @@ def generate_story(prompt: str, caption: str) -> str:
|
|
79 |
}
|
80 |
retries = 0
|
81 |
max_retries = 5
|
|
|
82 |
while True:
|
83 |
try:
|
84 |
-
resp = requests.post(api_url, headers=headers, json=payload, timeout=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
except Exception as e:
|
86 |
st.error(f"🚨 Story magic failed: {type(e).__name__}: {e}")
|
87 |
st.stop()
|
@@ -100,7 +111,7 @@ def generate_story(prompt: str, caption: str) -> str:
|
|
100 |
|
101 |
# Model loading (cold start)
|
102 |
if resp.status_code == 503 and retries < max_retries:
|
103 |
-
wait = int(resp.json().get("estimated_time", 5))
|
104 |
st.info(f"Model loading; retrying in {wait}s (attempt {retries+1}/{max_retries})")
|
105 |
time.sleep(wait)
|
106 |
retries += 1
|
@@ -108,12 +119,12 @@ def generate_story(prompt: str, caption: str) -> str:
|
|
108 |
|
109 |
# Server-side generation error
|
110 |
if resp.status_code in (424, 500, 502) and retries < max_retries:
|
111 |
-
|
112 |
-
|
|
|
113 |
retries += 1
|
114 |
continue
|
115 |
if resp.status_code in (424, 500, 502):
|
116 |
-
# Fallback story using the caption, ensuring ~70 words
|
117 |
return (f"One day, {caption} woke up under a bright sky and decided to explore the garden. "
|
118 |
"It met a friendly ladybug and together they played hide-and-seek among the flowers. "
|
119 |
"At sunset, {caption} curled up by a daisy, purring happily as it dreamed of new adventures.")
|
@@ -177,3 +188,4 @@ if uploaded:
|
|
177 |
|
178 |
# Footer
|
179 |
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")
|
|
|
|
4 |
from huggingface_hub import InferenceApi
|
5 |
from gtts import gTTS
|
6 |
import requests
|
7 |
+
from requests.exceptions import ReadTimeout
|
8 |
import tempfile
|
9 |
import time
|
10 |
import threading
|
|
|
63 |
st.error(f"Image processing error: {type(e).__name__}: {e}")
|
64 |
st.stop()
|
65 |
|
66 |
+
# —––––––– Helper: Generate Story with improved retry and timeout —–––––––
|
67 |
def generate_story(prompt: str, caption: str) -> str:
|
68 |
api_url = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
69 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
|
|
80 |
}
|
81 |
retries = 0
|
82 |
max_retries = 5
|
83 |
+
timeout = 60 # allow up to 60s for large model
|
84 |
while True:
|
85 |
try:
|
86 |
+
resp = requests.post(api_url, headers=headers, json=payload, timeout=timeout)
|
87 |
+
except ReadTimeout:
|
88 |
+
if retries < max_retries:
|
89 |
+
wait = 2 ** retries
|
90 |
+
st.info(f"Request timed out; retrying in {wait}s (attempt {retries+1}/{max_retries})")
|
91 |
+
time.sleep(wait)
|
92 |
+
retries += 1
|
93 |
+
continue
|
94 |
+
st.error("🚨 Story magic failed: request timed out after multiple attempts.")
|
95 |
+
st.stop()
|
96 |
except Exception as e:
|
97 |
st.error(f"🚨 Story magic failed: {type(e).__name__}: {e}")
|
98 |
st.stop()
|
|
|
111 |
|
112 |
# Model loading (cold start)
|
113 |
if resp.status_code == 503 and retries < max_retries:
|
114 |
+
wait = int(resp.json().get("estimated_time", 5))
|
115 |
st.info(f"Model loading; retrying in {wait}s (attempt {retries+1}/{max_retries})")
|
116 |
time.sleep(wait)
|
117 |
retries += 1
|
|
|
119 |
|
120 |
# Server-side generation error
|
121 |
if resp.status_code in (424, 500, 502) and retries < max_retries:
|
122 |
+
wait = 2 ** retries
|
123 |
+
st.info(f"Server error {resp.status_code}; retrying in {wait}s (attempt {retries+1}/{max_retries})")
|
124 |
+
time.sleep(wait)
|
125 |
retries += 1
|
126 |
continue
|
127 |
if resp.status_code in (424, 500, 502):
|
|
|
128 |
return (f"One day, {caption} woke up under a bright sky and decided to explore the garden. "
|
129 |
"It met a friendly ladybug and together they played hide-and-seek among the flowers. "
|
130 |
"At sunset, {caption} curled up by a daisy, purring happily as it dreamed of new adventures.")
|
|
|
188 |
|
189 |
# Footer
|
190 |
st.markdown("---\n*Made with ❤️ by your friendly story wizard*")
|
191 |
+
|