Update app.py
Browse files
app.py
CHANGED
|
@@ -18,10 +18,10 @@ model = PeftModel.from_pretrained(base_model, adapter_model_name)
|
|
| 18 |
|
| 19 |
def translate(text, source_lang, target_lang):
|
| 20 |
if not text.strip():
|
| 21 |
-
return "
|
| 22 |
|
| 23 |
if source_lang == target_lang:
|
| 24 |
-
return
|
| 25 |
|
| 26 |
lang_map = {
|
| 27 |
"English": "eng_Latn",
|
|
@@ -46,78 +46,220 @@ def translate(text, source_lang, target_lang):
|
|
| 46 |
)
|
| 47 |
|
| 48 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
with gr.Blocks(theme=gr.themes.
|
| 60 |
-
gr.Markdown("# Oil & Gas Professional Translation")
|
| 61 |
-
gr.Markdown("English ↔ Norwegian translation specialized for petroleum industry")
|
| 62 |
-
|
| 63 |
-
with gr.Row():
|
| 64 |
-
source_lang = gr.Dropdown(
|
| 65 |
-
choices=["English", "Norwegian"],
|
| 66 |
-
label="Source Language",
|
| 67 |
-
value="English"
|
| 68 |
-
)
|
| 69 |
-
swap_btn = gr.Button("⇄ Swap", size="sm")
|
| 70 |
-
target_lang = gr.Dropdown(
|
| 71 |
-
choices=["English", "Norwegian"],
|
| 72 |
-
label="Target Language",
|
| 73 |
-
value="Norwegian"
|
| 74 |
-
)
|
| 75 |
|
| 76 |
-
with gr.Row():
|
| 77 |
-
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
input_text = gr.Textbox(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
-
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
output_text = gr.Textbox(
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
char_count = gr.Textbox(label="Statistics", value="0 characters", interactive=False)
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
gr.
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
inputs=[input_text, source_lang, target_lang],
|
| 103 |
-
|
| 104 |
)
|
| 105 |
|
| 106 |
-
|
| 107 |
fn=translate,
|
| 108 |
inputs=[input_text, source_lang, target_lang],
|
| 109 |
-
outputs=
|
| 110 |
)
|
| 111 |
|
| 112 |
swap_btn.click(
|
| 113 |
fn=swap_languages,
|
| 114 |
-
inputs=[source_lang, target_lang],
|
| 115 |
-
outputs=[source_lang, target_lang]
|
| 116 |
)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
fn=
|
| 120 |
-
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
demo.launch()
|
|
|
|
| 18 |
|
| 19 |
def translate(text, source_lang, target_lang):
|
| 20 |
if not text.strip():
|
| 21 |
+
return ""
|
| 22 |
|
| 23 |
if source_lang == target_lang:
|
| 24 |
+
return text
|
| 25 |
|
| 26 |
lang_map = {
|
| 27 |
"English": "eng_Latn",
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 49 |
+
return result
|
| 50 |
+
|
| 51 |
+
def swap_languages(src, tgt, input_txt, output_txt):
|
| 52 |
+
return tgt, src, output_txt, input_txt
|
| 53 |
+
|
| 54 |
+
def load_file(file):
|
| 55 |
+
if file is None:
|
| 56 |
+
return ""
|
| 57 |
|
| 58 |
+
try:
|
| 59 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
| 60 |
+
content = f.read()
|
| 61 |
+
return content
|
| 62 |
+
except:
|
| 63 |
+
try:
|
| 64 |
+
with open(file.name, 'r', encoding='latin-1') as f:
|
| 65 |
+
content = f.read()
|
| 66 |
+
return content
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Error reading file: {str(e)}"
|
| 69 |
|
| 70 |
+
def translate_file(file, source_lang, target_lang):
|
| 71 |
+
content = load_file(file)
|
| 72 |
+
if content and not content.startswith("Error"):
|
| 73 |
+
return translate(content, source_lang, target_lang)
|
| 74 |
+
return content
|
| 75 |
|
| 76 |
+
custom_css = """
|
| 77 |
+
.gradio-container {
|
| 78 |
+
max-width: 1400px !important;
|
| 79 |
+
margin: auto !important;
|
| 80 |
+
}
|
| 81 |
+
.lang-selector {
|
| 82 |
+
border-radius: 12px !important;
|
| 83 |
+
border: 1px solid #e0e0e0 !important;
|
| 84 |
+
font-size: 16px !important;
|
| 85 |
+
}
|
| 86 |
+
.swap-btn {
|
| 87 |
+
min-width: 48px !important;
|
| 88 |
+
height: 48px !important;
|
| 89 |
+
border-radius: 50% !important;
|
| 90 |
+
background: white !important;
|
| 91 |
+
border: 1px solid #dadce0 !important;
|
| 92 |
+
font-size: 20px !important;
|
| 93 |
+
}
|
| 94 |
+
.text-area {
|
| 95 |
+
border: none !important;
|
| 96 |
+
border-radius: 8px !important;
|
| 97 |
+
font-size: 16px !important;
|
| 98 |
+
line-height: 1.6 !important;
|
| 99 |
+
}
|
| 100 |
+
.translate-container {
|
| 101 |
+
background: white !important;
|
| 102 |
+
border-radius: 8px !important;
|
| 103 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;
|
| 104 |
+
}
|
| 105 |
+
.example-box {
|
| 106 |
+
border-left: 3px solid #1a73e8 !important;
|
| 107 |
+
padding-left: 12px !important;
|
| 108 |
+
margin: 8px 0 !important;
|
| 109 |
+
background: #f8f9fa !important;
|
| 110 |
+
border-radius: 4px !important;
|
| 111 |
+
}
|
| 112 |
+
"""
|
| 113 |
|
| 114 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
with gr.Row(elem_classes="translate-container"):
|
| 117 |
+
with gr.Column(scale=1):
|
| 118 |
+
with gr.Row():
|
| 119 |
+
source_lang = gr.Dropdown(
|
| 120 |
+
choices=["English", "Norwegian"],
|
| 121 |
+
value="English",
|
| 122 |
+
show_label=False,
|
| 123 |
+
container=False,
|
| 124 |
+
elem_classes="lang-selector"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
input_text = gr.Textbox(
|
| 128 |
+
placeholder="Enter text",
|
| 129 |
+
show_label=False,
|
| 130 |
+
lines=12,
|
| 131 |
+
container=False,
|
| 132 |
+
elem_classes="text-area"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
file_input = gr.File(
|
| 136 |
+
label="Upload text file",
|
| 137 |
+
file_types=[".txt", ".md", ".doc"],
|
| 138 |
+
type="filepath"
|
| 139 |
)
|
| 140 |
|
| 141 |
+
with gr.Column(scale=0, min_width=80):
|
| 142 |
+
gr.Markdown("<br><br>")
|
| 143 |
+
swap_btn = gr.Button("⇄", elem_classes="swap-btn", size="sm")
|
| 144 |
+
|
| 145 |
+
with gr.Column(scale=1):
|
| 146 |
+
with gr.Row():
|
| 147 |
+
target_lang = gr.Dropdown(
|
| 148 |
+
choices=["English", "Norwegian"],
|
| 149 |
+
value="Norwegian",
|
| 150 |
+
show_label=False,
|
| 151 |
+
container=False,
|
| 152 |
+
elem_classes="lang-selector"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
output_text = gr.Textbox(
|
| 156 |
+
placeholder="Translation",
|
| 157 |
+
show_label=False,
|
| 158 |
+
lines=12,
|
| 159 |
+
container=False,
|
| 160 |
+
elem_classes="text-area",
|
| 161 |
+
interactive=False
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
download_btn = gr.File(
|
| 165 |
+
label="Download translation",
|
| 166 |
+
visible=False
|
| 167 |
)
|
|
|
|
|
|
|
| 168 |
|
| 169 |
with gr.Row():
|
| 170 |
+
gr.Markdown(
|
| 171 |
+
"<center style='color: #666; font-size: 14px; margin-top: 20px;'>"
|
| 172 |
+
"Oil & Gas specialized translation • Powered by NLLB-200 + LoRA"
|
| 173 |
+
"</center>"
|
| 174 |
+
)
|
| 175 |
|
| 176 |
+
with gr.Accordion("Example Translations", open=False):
|
| 177 |
+
with gr.Row():
|
| 178 |
+
with gr.Column():
|
| 179 |
+
gr.Markdown(
|
| 180 |
+
"""
|
| 181 |
+
<div class='example-box'>
|
| 182 |
+
<b>Drilling Report Example:</b><br><br>
|
| 183 |
+
The drilling operation at well site A-15 encountered unexpected high-pressure zones at 3,247 meters depth.
|
| 184 |
+
The mud weight was increased from 1.65 to 1.82 specific gravity to maintain wellbore stability.
|
| 185 |
+
Pressure readings indicate a potential hydrocarbon-bearing formation with estimated reservoir pressure
|
| 186 |
+
of 4,850 psi. The drilling team implemented managed pressure drilling techniques to safely penetrate
|
| 187 |
+
the formation while maintaining kick tolerance within acceptable limits.
|
| 188 |
+
</div>
|
| 189 |
+
"""
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
with gr.Column():
|
| 193 |
+
gr.Markdown(
|
| 194 |
+
"""
|
| 195 |
+
<div class='example-box'>
|
| 196 |
+
<b>Reservoir Analysis Example:</b><br><br>
|
| 197 |
+
Reservoaret viser utmerket permeabilitet på 250 millidarcy og porøsitet på 22 prosent basert på
|
| 198 |
+
kjerneanalyse. Formasjonsvannsmetningen er estimert til 35 prosent, noe som indikerer betydelig
|
| 199 |
+
hydrokarbonpotensial. Seismiske data bekrefter en strukturell felle med estimert areal på 12
|
| 200 |
+
kvadratkilometer. Produktivitetstester viser stabilisert oljeproduksjon på 3,400 fat per dag ved
|
| 201 |
+
optimaliseringstrykk på 2,100 psi.
|
| 202 |
+
</div>
|
| 203 |
+
"""
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
with gr.Row():
|
| 207 |
+
with gr.Column():
|
| 208 |
+
gr.Markdown(
|
| 209 |
+
"""
|
| 210 |
+
<div class='example-box'>
|
| 211 |
+
<b>Safety Procedure Example:</b><br><br>
|
| 212 |
+
All personnel must complete H2S safety training before entering the drilling site. Emergency response
|
| 213 |
+
equipment including breathing apparatus and wind direction indicators must be positioned at designated
|
| 214 |
+
muster points. Wellhead pressure monitoring systems shall be checked every 4 hours during critical
|
| 215 |
+
operations. In case of gas detection exceeding 10 ppm, immediate evacuation procedures must be initiated
|
| 216 |
+
according to the site emergency response plan.
|
| 217 |
+
</div>
|
| 218 |
+
"""
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
with gr.Column():
|
| 222 |
+
gr.Markdown(
|
| 223 |
+
"""
|
| 224 |
+
<div class='example-box'>
|
| 225 |
+
<b>Equipment Specification Example:</b><br><br>
|
| 226 |
+
Undervannsproduktjonssystemet består av en vertikalt juletræ med maksimal arbeidstrykk på 10,000 psi
|
| 227 |
+
og temperaturbegrensning på 150 grader Celsius. Produksjonsrøret er 7-tommers krom-legering designet
|
| 228 |
+
for korrosive miljøer. Flytkontrollen opprettholdes ved bruk av subsea multifase meter og akustisk
|
| 229 |
+
sanddeteksjon. Systemet er utstyrt med redundante sikkerhetsfunksjoner inkludert automatiske
|
| 230 |
+
nedstengningsventiler og trykkovervåkingssystemer.
|
| 231 |
+
</div>
|
| 232 |
+
"""
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
input_text.change(
|
| 236 |
+
fn=translate,
|
| 237 |
+
inputs=[input_text, source_lang, target_lang],
|
| 238 |
+
outputs=output_text
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
source_lang.change(
|
| 242 |
+
fn=translate,
|
| 243 |
inputs=[input_text, source_lang, target_lang],
|
| 244 |
+
outputs=output_text
|
| 245 |
)
|
| 246 |
|
| 247 |
+
target_lang.change(
|
| 248 |
fn=translate,
|
| 249 |
inputs=[input_text, source_lang, target_lang],
|
| 250 |
+
outputs=output_text
|
| 251 |
)
|
| 252 |
|
| 253 |
swap_btn.click(
|
| 254 |
fn=swap_languages,
|
| 255 |
+
inputs=[source_lang, target_lang, input_text, output_text],
|
| 256 |
+
outputs=[source_lang, target_lang, input_text, output_text]
|
| 257 |
)
|
| 258 |
|
| 259 |
+
file_input.change(
|
| 260 |
+
fn=load_file,
|
| 261 |
+
inputs=file_input,
|
| 262 |
+
outputs=input_text
|
| 263 |
)
|
| 264 |
|
| 265 |
demo.launch()
|