Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -14,11 +14,15 @@ def tts(text: str, language="ja") -> object:
|
|
| 14 |
html: autoplay object
|
| 15 |
"""
|
| 16 |
tts_object = gTTS(text=text, lang=language, slow=False)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def read_image_file(file) -> Image.Image:
|
|
@@ -26,11 +30,16 @@ def read_image_file(file) -> Image.Image:
|
|
| 26 |
return image
|
| 27 |
|
| 28 |
|
| 29 |
-
def pil_to_base64(img, format="jpeg"):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def base64_to_pil(img_str):
|
|
|
|
| 14 |
html: autoplay object
|
| 15 |
"""
|
| 16 |
tts_object = gTTS(text=text, lang=language, slow=False)
|
| 17 |
+
if encode:
|
| 18 |
+
bytes_object = BytesIO()
|
| 19 |
+
tts_object.write_to_fp(bytes_object)
|
| 20 |
+
bytes_object.seek(0)
|
| 21 |
+
b64 = base64.b64encode(bytes_object.getvalue()).decode()
|
| 22 |
+
return b64
|
| 23 |
+
else:
|
| 24 |
+
tts_object.save("temp.mp3")
|
| 25 |
+
return "temp.mp3"
|
| 26 |
|
| 27 |
|
| 28 |
def read_image_file(file) -> Image.Image:
|
|
|
|
| 30 |
return image
|
| 31 |
|
| 32 |
|
| 33 |
+
def pil_to_base64(img, format="jpeg", encode=False):
|
| 34 |
+
if encode:
|
| 35 |
+
buffer = BytesIO()
|
| 36 |
+
img.save(buffer, format)
|
| 37 |
+
buffer.seek(0)
|
| 38 |
+
return buffer
|
| 39 |
+
else:
|
| 40 |
+
temp_path = f"temp.{format}"
|
| 41 |
+
img.save(temp_path)
|
| 42 |
+
return temp_path
|
| 43 |
|
| 44 |
|
| 45 |
def base64_to_pil(img_str):
|