Spaces:
Runtime error
Runtime error
translate and tts
Browse files
sarvam.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import aiohttp
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
code_map = {
|
| 9 |
+
"hindi": "hi-IN",
|
| 10 |
+
"bengali": "bn-IN",
|
| 11 |
+
"kannada": "kn-IN",
|
| 12 |
+
"malayalam": "ml-IN",
|
| 13 |
+
"marathi": "mr-IN",
|
| 14 |
+
"odia": "od-IN",
|
| 15 |
+
"punjabi": "pa-IN",
|
| 16 |
+
"tamil": "ta-IN",
|
| 17 |
+
"telugu": "te-IN",
|
| 18 |
+
"english": "en-IN",
|
| 19 |
+
"gujarati": "gu-IN",
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
async def translator(text, src, dest):
|
| 24 |
+
async with aiohttp.ClientSession() as session:
|
| 25 |
+
url = "https://api.sarvam.ai/translate"
|
| 26 |
+
|
| 27 |
+
payload = {
|
| 28 |
+
"input": text,
|
| 29 |
+
"source_language_code": code_map[src],
|
| 30 |
+
"target_language_code": code_map[dest],
|
| 31 |
+
"speaker_gender": "Male",
|
| 32 |
+
"mode": "formal",
|
| 33 |
+
"model": "mayura:v1",
|
| 34 |
+
"enable_preprocessing": True,
|
| 35 |
+
}
|
| 36 |
+
headers = {"Content-Type": "application/json", "api-subscription-key": os.getenv("SARVAM_API_KEY")}
|
| 37 |
+
async with session.post(url, headers=headers, json=payload) as response:
|
| 38 |
+
if response.status == 200:
|
| 39 |
+
output = await response.text()
|
| 40 |
+
return output
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
async def speaker(text, dest):
|
| 44 |
+
async with aiohttp.ClientSession() as session:
|
| 45 |
+
url = "https://api.sarvam.ai/text-to-speech"
|
| 46 |
+
|
| 47 |
+
payload = {
|
| 48 |
+
"inputs": [text],
|
| 49 |
+
"target_language_code": code_map[dest],
|
| 50 |
+
"speaker": "meera",
|
| 51 |
+
"pitch": 0,
|
| 52 |
+
"pace": 1.65,
|
| 53 |
+
"loudness": 1.5,
|
| 54 |
+
"speech_sample_rate": 8000,
|
| 55 |
+
"enable_preprocessing": True,
|
| 56 |
+
"model": "bulbul:v1",
|
| 57 |
+
}
|
| 58 |
+
headers = {"Content-Type": "application/json", "api-subscription-key": os.getenv("SARVAM_API_KEY")}
|
| 59 |
+
async with session.post(url, headers=headers, json=payload) as response:
|
| 60 |
+
if response.status == 200:
|
| 61 |
+
output = await response.read()
|
| 62 |
+
return output
|