Spaces:
Runtime error
Runtime error
Commit
·
d90b6b8
1
Parent(s):
2e6d65c
TTS2
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ from langchain.utilities import WikipediaAPIWrapper
|
|
18 |
from langchain.python import PythonREPL
|
19 |
from langchain.chains import LLMMathChain
|
20 |
import azure.cognitiveservices.speech as speechsdk
|
21 |
-
|
22 |
|
23 |
import pinecone
|
24 |
from pinecone.core.client.configuration import Configuration as OpenApiConfiguration
|
@@ -225,14 +225,62 @@ def Text2Sound(text):
|
|
225 |
return speech_synthesis_result
|
226 |
pass
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
Text2Sound_tool = Tool(
|
229 |
name = "Text To Sound",
|
230 |
-
func = Text2Sound,
|
231 |
-
|
232 |
description = "Useful when you need to convert text into sound file."
|
233 |
)
|
234 |
|
235 |
-
|
236 |
Wikipedia = WikipediaAPIWrapper()
|
237 |
Netsearch = DuckDuckGoSearchRun()
|
238 |
Python_REPL = PythonREPL()
|
@@ -312,7 +360,6 @@ Text To Sound: Useful when you need to convert text into sound file."""
|
|
312 |
PREFIX = """Answer the following questions as best you can with details. You must always check internal vector database first and try to answer the question based on the information in internal vector database only.
|
313 |
Only when there is no information available from vector database, you can search information by using another tools.
|
314 |
You have access to the following tools:
|
315 |
-
|
316 |
"""
|
317 |
|
318 |
FORMAT_INSTRUCTIONS = """Use the following format:
|
|
|
18 |
from langchain.python import PythonREPL
|
19 |
from langchain.chains import LLMMathChain
|
20 |
import azure.cognitiveservices.speech as speechsdk
|
21 |
+
import requests
|
22 |
|
23 |
import pinecone
|
24 |
from pinecone.core.client.configuration import Configuration as OpenApiConfiguration
|
|
|
225 |
return speech_synthesis_result
|
226 |
pass
|
227 |
|
228 |
+
|
229 |
+
|
230 |
+
def get_azure_access_token():
|
231 |
+
azure_key = os.environ.get("OPENAI_API_KEY")
|
232 |
+
try:
|
233 |
+
response = requests.post(
|
234 |
+
"https://eastus.api.cognitive.microsoft.com/sts/v1.0/issuetoken",
|
235 |
+
headers={
|
236 |
+
"Ocp-Apim-Subscription-Key": azure_key
|
237 |
+
}
|
238 |
+
)
|
239 |
+
response.raise_for_status()
|
240 |
+
except requests.exceptions.RequestException as e:
|
241 |
+
print(f"Error: {e}")
|
242 |
+
return None
|
243 |
+
|
244 |
+
return response.text
|
245 |
+
|
246 |
+
|
247 |
+
def text_to_speech_2(text, voice_name='en-US-AriaNeural'):
|
248 |
+
access_token = get_azure_access_token()
|
249 |
+
|
250 |
+
if not access_token:
|
251 |
+
return None
|
252 |
+
|
253 |
+
try:
|
254 |
+
response = requests.post(
|
255 |
+
"https://eastus.tts.speech.microsoft.com/cognitiveservices/v1",
|
256 |
+
headers={
|
257 |
+
"Authorization": f"Bearer {access_token}",
|
258 |
+
"Content-Type": "application/ssml+xml",
|
259 |
+
"X-MICROSOFT-OutputFormat": "riff-24khz-16bit-mono-pcm",
|
260 |
+
"User-Agent": "TextToSpeechApp",
|
261 |
+
},
|
262 |
+
data=f"""
|
263 |
+
<speak version='1.0' xml:lang='en-US'>
|
264 |
+
<voice name='{voice_name}'>
|
265 |
+
{text}
|
266 |
+
</voice>
|
267 |
+
</speak>
|
268 |
+
""",
|
269 |
+
)
|
270 |
+
response.raise_for_status()
|
271 |
+
except requests.exceptions.RequestException as e:
|
272 |
+
print(f"Error: {e}")
|
273 |
+
return None
|
274 |
+
|
275 |
+
return response.content
|
276 |
+
|
277 |
Text2Sound_tool = Tool(
|
278 |
name = "Text To Sound",
|
279 |
+
# func = Text2Sound,
|
280 |
+
func = text_to_speech_2,
|
281 |
description = "Useful when you need to convert text into sound file."
|
282 |
)
|
283 |
|
|
|
284 |
Wikipedia = WikipediaAPIWrapper()
|
285 |
Netsearch = DuckDuckGoSearchRun()
|
286 |
Python_REPL = PythonREPL()
|
|
|
360 |
PREFIX = """Answer the following questions as best you can with details. You must always check internal vector database first and try to answer the question based on the information in internal vector database only.
|
361 |
Only when there is no information available from vector database, you can search information by using another tools.
|
362 |
You have access to the following tools:
|
|
|
363 |
"""
|
364 |
|
365 |
FORMAT_INSTRUCTIONS = """Use the following format:
|