deepsync commited on
Commit
058254a
·
verified ·
1 Parent(s): 291c686

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -31
app.py CHANGED
@@ -26,42 +26,48 @@ def get_google_token():
26
  return access_token
27
 
28
 
29
- def dubpro_english_to_hindi(text):
30
- API_URL = os.environ.get("GEMINI_FINETUNED_ENG_HINDI_API")
31
- BEARER_TOKEN = get_google_token()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  headers = {
33
- "Authorization": f"Bearer {BEARER_TOKEN}",
34
  "Content-Type": "application/json",
 
35
  }
 
36
  payload = {
37
- "contents": [
38
- {
39
- "parts": [{"text": f"text: {text}"}],
40
- "role": "user",
41
- }
42
- ],
43
- "generationConfig": {
44
- "maxOutputTokens": 8192,
45
- "temperature": 0.85,
46
- },
47
- "safetySettings": [
48
- {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
49
- {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
50
- {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
51
- {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
52
- ],
53
  }
 
54
  result = requests.post(
55
  url=API_URL,
56
  headers=headers,
57
  json=payload
58
  )
59
  response = result.json()
60
- response_content = response['candidates'][0]['content']['parts'][0]['text'].replace("translated:", "").strip()
61
- return response_content
62
 
63
 
64
- def gemini_english_to_hindi(text):
65
  API_URL = os.environ.get("GEMINI_FINETUNED_ENG_HINDI_API")
66
  BEARER_TOKEN = get_google_token()
67
  headers = {
@@ -71,7 +77,7 @@ def gemini_english_to_hindi(text):
71
  payload = {
72
  "contents": [
73
  {
74
- "parts": [{"text": f"Translate the following text to Hindi: `{text}` Output: "}],
75
  "role": "user",
76
  }
77
  ],
@@ -105,8 +111,9 @@ def clean(result):
105
  text = text.split("\n")[-1]
106
  return text
107
 
108
- def openai_english_to_hindi(text, model):
109
- prompt = f"Translate the following English text into Hindi such that the meaning in unchanged. Return only the translated text: `{text}`. Output: "
 
110
 
111
  headers = {
112
  "Content-Type": "application/json",
@@ -134,7 +141,7 @@ def openai_english_to_hindi(text, model):
134
 
135
 
136
  # Azure translate
137
- def azure_english_to_hindi(text):
138
  headers = {
139
  "Ocp-Apim-Subscription-Key": os.environ.get("AZURE_TRANSLATE_KEY"),
140
  "Ocp-Apim-Subscription-Region": os.environ.get("AZURE_TRANSLATE_REGION"),
@@ -144,8 +151,8 @@ def azure_english_to_hindi(text):
144
  ENDPOINT = "https://api.cognitive.microsofttranslator.com/translate"
145
  params = {
146
  "api-version": "3.0",
147
- "from": "en-US",
148
- "to": "hi-IN",
149
  }
150
  texts = [{"text": text}]
151
  request = requests.post(ENDPOINT, headers=headers, params=params, json=texts)
@@ -154,7 +161,7 @@ def azure_english_to_hindi(text):
154
 
155
 
156
  # Anthopic Claude 3 Haiku
157
- def claude_english_to_hindi(text):
158
  client = anthropic.Anthropic()
159
  message = client.messages.create(
160
  model="claude-3-haiku-20240307",
@@ -167,7 +174,7 @@ def claude_english_to_hindi(text):
167
  "content": [
168
  {
169
  "type": "text",
170
- "text": f"Translate the following English text into Hindi such that the meaning in unchanged. Return only the translated text: `{text}`. Output: "
171
  }
172
  ]
173
  }
 
26
  return access_token
27
 
28
 
29
+ def clean(result):
30
+ text = result["choices"][0]['message']["content"]
31
+ text = re.sub(r"\(.*?\)|\[.*?\]","", text)
32
+ text = text.strip("'").replace('"', "")
33
+ if "\n" in text.strip("\n"):
34
+ text = text.split("\n")[-1]
35
+ return text
36
+
37
+
38
+ def dubpro_hindi_to_tamil(text):
39
+ API_URL = "https://api.openai.com/v1/chat/completions"
40
+ prompt = f"""Please convert the following Hindi text into Tamil, where it is important to maintain context, so please translate it in a colloquial manner, like that of a YouTube video. Just print the translation.
41
+ ___
42
+ {text}
43
+ ___
44
+ """
45
+
46
+ messages = [
47
+ {"role": "system", "content": f"You are a language translation assistant."},
48
+ {"role": "user", "content": prompt}
49
+ ]
50
+
51
  headers = {
 
52
  "Content-Type": "application/json",
53
+ "Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}"
54
  }
55
+
56
  payload = {
57
+ "model": "gpt-4",
58
+ "messages": messages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  }
60
+
61
  result = requests.post(
62
  url=API_URL,
63
  headers=headers,
64
  json=payload
65
  )
66
  response = result.json()
67
+ return clean(response_content)
 
68
 
69
 
70
+ def gemini_hindi_to_tamil(text):
71
  API_URL = os.environ.get("GEMINI_FINETUNED_ENG_HINDI_API")
72
  BEARER_TOKEN = get_google_token()
73
  headers = {
 
77
  payload = {
78
  "contents": [
79
  {
80
+ "parts": [{"text": f"Translate the following text to Tamil: `{text}` Output: "}],
81
  "role": "user",
82
  }
83
  ],
 
111
  text = text.split("\n")[-1]
112
  return text
113
 
114
+ def openai_hindi_to_tamil(text, model):
115
+ prompt =
116
+ f"Translate the following Hindi text into Tamil such that the meaning in unchanged. Return only the translated text: `{text}`. Output: "
117
 
118
  headers = {
119
  "Content-Type": "application/json",
 
141
 
142
 
143
  # Azure translate
144
+ def azure_hindi_to_tamil(text):
145
  headers = {
146
  "Ocp-Apim-Subscription-Key": os.environ.get("AZURE_TRANSLATE_KEY"),
147
  "Ocp-Apim-Subscription-Region": os.environ.get("AZURE_TRANSLATE_REGION"),
 
151
  ENDPOINT = "https://api.cognitive.microsofttranslator.com/translate"
152
  params = {
153
  "api-version": "3.0",
154
+ "from": "hi-IN",
155
+ "to": "ta-IN",
156
  }
157
  texts = [{"text": text}]
158
  request = requests.post(ENDPOINT, headers=headers, params=params, json=texts)
 
161
 
162
 
163
  # Anthopic Claude 3 Haiku
164
+ def claude_hindi_to_tamil(text):
165
  client = anthropic.Anthropic()
166
  message = client.messages.create(
167
  model="claude-3-haiku-20240307",
 
174
  "content": [
175
  {
176
  "type": "text",
177
+ "text": f"Translate the following Hindi text into Tamil such that the meaning in unchanged. Return only the translated text: `{text}`. Output: "
178
  }
179
  ]
180
  }