deepsync commited on
Commit
8dd0141
·
verified ·
1 Parent(s): 1f95432

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +212 -0
app.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import json
4
+ import time
5
+ import requests
6
+ import anthropic
7
+ import google.auth
8
+ import gradio as gr
9
+ from uuid import uuid4
10
+ from dotenv import load_dotenv
11
+ from google.auth.transport.requests import Request
12
+
13
+
14
+ # Gemini
15
+ def get_google_token():
16
+ credentials, project = google.auth.load_credentials_from_dict(
17
+ json.loads(os.environ.get('GCP_FINETUNE_KEY')),
18
+ scopes=[
19
+ "https://www.googleapis.com/auth/cloud-platform",
20
+ "https://www.googleapis.com/auth/generative-language.tuning",
21
+ ],
22
+ )
23
+ request = Request()
24
+ credentials.refresh(request)
25
+ access_token = credentials.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 = {
68
+ "Authorization": f"Bearer {BEARER_TOKEN}",
69
+ "Content-Type": "application/json",
70
+ }
71
+ payload = {
72
+ "contents": [
73
+ {
74
+ "parts": [{"text": f"Translate the following text to Hindi: `{text}` Output: "}],
75
+ "role": "user",
76
+ }
77
+ ],
78
+ "generationConfig": {
79
+ "maxOutputTokens": 8192,
80
+ "temperature": 0.85,
81
+ },
82
+ "safetySettings": [
83
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
84
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
85
+ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
86
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
87
+ ],
88
+ }
89
+ result = requests.post(
90
+ url=API_URL,
91
+ headers=headers,
92
+ json=payload
93
+ )
94
+ response = result.json()
95
+ response_content = response['candidates'][0]['content']['parts'][0]['text'].replace("translated:", "").replace("`", "").strip()
96
+ return response_content
97
+
98
+
99
+ # GPT models
100
+ def clean(result):
101
+ text = result["choices"][0]['message']["content"]
102
+ text = re.sub(r"\(.*?\)|\[.*?\]","", text)
103
+ text = text.strip("'").replace('"', "")
104
+ if "\n" in text.strip("\n"):
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",
113
+ "Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}"
114
+ }
115
+
116
+ messages = [
117
+ {"role": "system", "content": f"You are a language translation assistant."},
118
+ {"role": "user", "content": prompt}
119
+ ]
120
+
121
+ resp = None
122
+ while resp is None:
123
+ resp = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json={
124
+ "model": model,
125
+ "messages": messages
126
+ })
127
+ if resp.status_code != 200:
128
+ print(resp.text)
129
+ time.sleep(0.5)
130
+ response_json = resp.json()
131
+
132
+ result_text = clean(response_json)
133
+ return result_text
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"),
141
+ "Content-type": "application/json",
142
+ "X-ClientTraceId": str(uuid4()),
143
+ }
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)
152
+ response = request.json()
153
+ return response[0]["translations"][0]["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",
161
+ max_tokens=1000,
162
+ temperature=0.8,
163
+ system="You are an expert language translator.",
164
+ messages=[
165
+ {
166
+ "role": "user",
167
+ "content": [
168
+ {
169
+ "type": "text",
170
+ "text": "Translate the following English text into Hindi such that the meaning in unchanged. Return only the translated text: `The Electrons journey starts with Complex 1 or it can start with Complex 2 either of that.`. Output: "
171
+ }
172
+ ]
173
+ }
174
+ ]
175
+ )
176
+ return message.content[0].text
177
+
178
+
179
+ def render_translations(text):
180
+ dubpro = dubpro_english_to_hindi(text)
181
+ azure = azure_english_to_hindi(text)
182
+ gemini = gemini_english_to_hindi(text)
183
+ gpt_4 = openai_english_to_hindi(text, model="gpt-4")
184
+ claude_haiku = claude_english_to_hindi(text)
185
+ return gr.update(value=gpt_4), gr.update(value=dubpro), gr.update(value=gemini), gr.update(value=claude_haiku), gr.update(value=azure)
186
+
187
+
188
+ with gr.Blocks(title="English to Hindi Translation Tools", theme="gradio/monochrome") as demo:
189
+ gr.Markdown("# English to Hindi Translation Tools")
190
+ input_textbox = gr.Textbox(label="Input Text", info="Text to translate", value="When you did it, you must have attended your classes well or you must have done your daily revision. Now you feel scared.")
191
+ submit = gr.Button(label="Submit")
192
+ with gr.Row():
193
+ gr.Label(value="Dubpro's Model", scale=1)
194
+ dubpro_model_textbox = gr.Textbox(label="Translated Text", scale=2, interactive=False)
195
+ with gr.Row():
196
+ gr.Label(value="GPT 4", scale=1)
197
+ gpt_4_textbox = gr.Textbox(label="Translated Text", scale=2, interactive=False)
198
+ with gr.Row():
199
+ gr.Label(value="Google Gemini", scale=1)
200
+ google_textbox = gr.Textbox(label="Translated Text", scale=2, interactive=False)
201
+ with gr.Row():
202
+ gr.Label(value="Anthropic Claude 3", scale=1)
203
+ claude_textbox = gr.Textbox(label="Translated Text", scale=2, interactive=False)
204
+ with gr.Row():
205
+ gr.Label(value="Azure Translate", scale=1)
206
+ azure_textbox = gr.Textbox(label="Translated Text", scale=2, interactive=False)
207
+
208
+ submit.click(render_translations, input_textbox, [gpt_4_textbox, dubpro_model_textbox, google_textbox, claude_textbox, azure_textbox])
209
+
210
+
211
+ if __name__=="__main__":
212
+ demo.launch()