Spaces:
Sleeping
Sleeping
File size: 7,272 Bytes
d4f7d23 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
import gradio as gr
from openai import OpenAI
import pandas as pd
import io
import tempfile
import shutil
import google.generativeai as genai
import os
api_key = os.getenv("OPENAI_API_KEY")
gemni_api_key = os.getenv("GEMNI_API_KEY")
supported_languages = ['English', 'Brazilian Portuguese', 'Latin American Spanish', 'French', 'European Portuguese', 'Castilian Spanish', 'German', 'Italian', 'Czech', 'Danish', 'Dutch', 'Finnish', 'Norwegian', 'Swedish', 'Hungarian', 'Greek', 'Romanian', 'Polish', 'Arabic', 'Urdu']
# OPENAI
client = OpenAI(api_key = api_key)
def translate_text_openai(source_language, target_language, TEXT, max_characters):
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
temperature = 0.1,
messages=[
{"role": "system", "content": "You are a multilingual translator for movies subtitles."},
{"role": "system", "content": "The number of input characters and output characters should be the same despite the change in language."},
{"role": "system", "content": f"In response, maximum characters allowed are {max_characters}"},
{"role": "system", "content": "You SHOULD NOT SKIP ANY LINE OR ANY INFORMATION"},
{"role": "system", "content": "The Tranlation should be error proof"},
{"role": "user", "content": f"""Translate the text from {source_language} language to {target_language} language.:
\nTEXT: {TEXT}
NOTE: THE OUTPUT SHOULD BE IN {target_language} language.
\nREMEMBER: MAXIMUM output chaeracters should be {max_characters}
END NOTE: THE OUTPUT SHOULD BE IN {target_language} language.
REMEMBER: THE OUTPUT SHOULD BE IN {target_language} language.
Hey on some instances you give response in languages other than {target_language}, which is wrong
"""},
]
)
return response.choices[0].message.content
def translate_text_correct_openai(source_language, target_language, TEXT, max_characters):
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
temperature = 0.1,
messages=[
{"role": "system", "content": "You reduce the size of the sentences."},
{"role": "system", "content": f"The maximuim output should not be more than {max_characters} characters."},
{"role": "user", "content": f"""
DO NOT CHANGE THE LANGUAGE
Reduce the size of the text to less than {max_characters} characters even if there is a change in meaning.
REMEMBER LLM, THE INPUT AND THE OUTPUT SHOULD BE IN THE SAME LANGUAGE
\nWrite the sentence in shortest possible manner
\nTEXT: {TEXT}\
REMEMBER LLM, THE INPUT AND THE OUTPUT SHOULD BE IN THE SAME LANGUAGE
"""},
]
)
return response.choices[0].message.content
# GEMNI
genai.configure(api_key=gemni_api_key)
model = genai.GenerativeModel('gemini-1.5-flash')
def translate_text_gemni(source_language, target_language, TEXT, max_characters):
response = model.generate_content(f'''You are a multilingual translator for movies subtitles.
The number of input characters and output characters should be the same despite the change in language.
In response, maximum characters allowed are {max_characters}.
You SHOULD NOT SKIP ANY LINE OR ANY INFORMATION.
The Tranlation should be error proof.
Translate the text from {source_language} language to {target_language} language.:
\nTEXT: {TEXT}
NOTE: THE OUTPUT SHOULD BE IN {target_language} language.
\nREMEMBER: MAXIMUM output chaeracters should be {max_characters}
END NOTE: THE OUTPUT SHOULD BE IN {target_language} language.
REMEMBER: THE OUTPUT SHOULD BE IN {target_language} language.
Hey on some instances you give response in languages other than {target_language}, which is wrong
'''
)
return response.text
def translate_text_correct_gemni(source_language, target_language, TEXT, max_characters):
response = model.generate_content(f"""
You reduce the size of the sentences.
The maximuim output should not be more than {max_characters} characters.
DO NOT CHANGE THE LANGUAGE
Reduce the size of the text to less than {max_characters} characters even if there is a change in meaning.
REMEMBER LLM, THE INPUT AND THE OUTPUT SHOULD BE IN THE SAME LANGUAGE
\nWrite the sentence in shortest possible manner
\nTEXT: {TEXT}\
REMEMBER LLM, THE INPUT AND THE OUTPUT SHOULD BE IN THE SAME LANGUAGE
"""
)
return response.text
def check_conditon_openai(source_language, target_language, response, max_characters):
length = len(response)
if length > int(max_characters):
response = translate_text_correct_openai(source_language, target_language, response, max_characters)
return check_conditon_openai(source_language, target_language, response, max_characters)
return response
def check_conditon_gemni(source_language, target_language, response, max_characters):
length = len(response)
if length > int(max_characters):
response = translate_text_correct_gemni(source_language, target_language, response, max_characters)
return check_conditon_gemni(source_language, target_language, response, max_characters)
return response
def get_translation(source_language, target_language, TEXT, max_characters):
response_openai = translate_text_openai(source_language, target_language, TEXT, max_characters)
response_openai = check_conditon_openai(source_language, target_language, response_openai, max_characters)
response_gemni = translate_text_gemni(source_language, target_language, TEXT, max_characters)
response_gemni = check_conditon_gemni(source_language, target_language, response_gemni, max_characters)
excel_data_path = create_excel(TEXT, response_openai, response_gemni)
return excel_data_path
def create_excel(TEXT, response_openai, response_gemni):
# Create a DataFrame from the input data
df = pd.DataFrame({"Original Text": TEXT, "OpenAI Translated": response_openai, "Gemni Translated": response_gemni}, index = [1])
# Create a temporary file to store the Excel data
with tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') as temp_file:
# Write the DataFrame to the temporary file as an Excel file
with pd.ExcelWriter(temp_file, engine='xlsxwriter') as writer:
df.to_excel(writer, index=False, sheet_name='Sheet1')
# Return the path to the temporary file
temp_file_path = temp_file.name
return temp_file_path
iface = gr.Interface(
fn=get_translation,
inputs=[
gr.Dropdown(choices= supported_languages, label="Source Language"), # Add more languages as needed
gr.Dropdown(choices= supported_languages, label="Target Language"),
gr.Textbox(lines=2, label="Input Text"),
gr.Textbox(lines=1, label="Difine number of output characters"),
],
outputs=gr.File(label="Download Excel File"),
title="MVP Multilingual Translation",
description="MVP Multilingual Translation by Farhan",
)
iface.launch(share=True, debug=True) |