Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -8,15 +8,44 @@ def clean_history(chat_history):
|
|
| 8 |
return [msg for msg in chat_history if isinstance(msg.get("content"), str)]
|
| 9 |
|
| 10 |
def save_image(image: Image.Image) -> str:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
async def text_to_speech(text: str, voice: str):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def web_search(query: str) -> str:
|
| 22 |
try:
|
|
|
|
| 8 |
return [msg for msg in chat_history if isinstance(msg.get("content"), str)]
|
| 9 |
|
| 10 |
def save_image(image: Image.Image) -> str:
|
| 11 |
+
try:
|
| 12 |
+
filename = f"output_{uuid.uuid4()}.png"
|
| 13 |
+
image.save(filename)
|
| 14 |
+
return filename
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error saving image: {str(e)}")
|
| 17 |
+
return None
|
| 18 |
|
| 19 |
async def text_to_speech(text: str, voice: str):
|
| 20 |
+
try:
|
| 21 |
+
output_file = f"speech_{uuid.uuid4()}.mp3"
|
| 22 |
+
# Add rate and volume parameters
|
| 23 |
+
communicate = edge_tts.Communicate(
|
| 24 |
+
text=text,
|
| 25 |
+
voice=voice,
|
| 26 |
+
rate="+0%", # Normal speed
|
| 27 |
+
volume="+0%" # Normal volume
|
| 28 |
+
)
|
| 29 |
+
await communicate.save(output_file)
|
| 30 |
+
return output_file
|
| 31 |
+
except ValueError as ve:
|
| 32 |
+
print(f"Voice error: {str(ve)}")
|
| 33 |
+
# Fallback to default voice if specified voice fails
|
| 34 |
+
try:
|
| 35 |
+
communicate = edge_tts.Communicate(
|
| 36 |
+
text=text,
|
| 37 |
+
voice="en-US-JennyNeural", # Fallback voice
|
| 38 |
+
rate="+0%",
|
| 39 |
+
volume="+0%"
|
| 40 |
+
)
|
| 41 |
+
await communicate.save(output_file)
|
| 42 |
+
return output_file
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Fallback voice error: {str(e)}")
|
| 45 |
+
return None
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"TTS error: {str(e)}")
|
| 48 |
+
return None
|
| 49 |
|
| 50 |
def web_search(query: str) -> str:
|
| 51 |
try:
|