File size: 1,971 Bytes
0e8d6fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c425f6c
 
0e8d6fe
 
b6e72f6
0e8d6fe
 
 
 
 
b6e72f6
0e8d6fe
 
c425f6c
0e8d6fe
 
 
 
 
 
 
 
c425f6c
0e8d6fe
c425f6c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os

import aiohttp
from dotenv import load_dotenv

load_dotenv()

code_map = {
    "hindi": "hi-IN",
    "bengali": "bn-IN",
    "kannada": "kn-IN",
    "malayalam": "ml-IN",
    "marathi": "mr-IN",
    "odia": "od-IN",
    "punjabi": "pa-IN",
    "tamil": "ta-IN",
    "telugu": "te-IN",
    "english": "en-IN",
    "gujarati": "gu-IN",
}


async def translator(text, src, dest):
    async with aiohttp.ClientSession() as session:
        url = "https://api.sarvam.ai/translate"

        payload = {
            "input": text,
            "source_language_code": code_map[src],
            "target_language_code": code_map[dest],
            "speaker_gender": "Male",
            "mode": "formal",
            "model": "mayura:v1",
            "enable_preprocessing": True,
        }
        headers = {"Content-Type": "application/json", "api-subscription-key": os.getenv("SARVAM_API_KEY")}
        async with session.post(url, headers=headers, json=payload) as response:
            if response.status == 200:
                output = await response.json()
                return {"text": output["translated_text"]}


async def speaker(text, src="hindi"):
    async with aiohttp.ClientSession() as session:
        url = "https://api.sarvam.ai/text-to-speech"

        payload = {
            "inputs": [text],
            "target_language_code": code_map[src],
            "speaker": "meera",
            "pitch": 0,
            "pace": 1.25,
            "loudness": 1.5,
            "speech_sample_rate": 8000,
            "enable_preprocessing": True,
            "model": "bulbul:v1",
        }
        headers = {"Content-Type": "application/json", "api-subscription-key": os.getenv("SARVAM_API_KEY")}
        async with session.post(url, headers=headers, json=payload) as response:
            if response.status == 200:
                output = await response.json()
                return output
            else:
                print(response.status)