Spaces:
Runtime error
Runtime error
File size: 5,997 Bytes
7f231be 0f25099 7f231be 5f80acb 7f231be d84920c 73c47d2 d84920c 53d46cf d84920c 7f231be 0f25099 7f231be d84920c 7f231be d84920c 7f231be d84920c 0f25099 7f231be 0f25099 7f231be 5f80acb |
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 |
import gradio as gr
import re
import torch
from transformers import pipeline
# Initialize the translation pipeline
def initialize_translator():
try:
device = 0 if torch.cuda.is_available() else -1 # Use GPU if available, otherwise CPU
return pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-es", device=device)
except Exception as e:
raise RuntimeError(f"Failed to initialize translation pipeline: {e}")
translator = initialize_translator()
# Define the custom glossary
custom_glossary = {
"Change Order": "Orden de Cambio",
"RFI (Request for Information)": "RFI (Solicitud de Información)",
"Submittals": "Envíos",
"Punch List": "Lista de Reparaciones",
"GMP (Guaranteed Maximum Price)": "GMP (Precio Máximo Garantizado)",
"Critical Path Method (CPM)": "Método de Ruta Crítica (CPM)",
"LEED Certification": "Certificación LEED",
"BIM (Building Information Modeling)": "BIM (Modelado de Información de Construcción)",
"Retainage": "Retención",
"Precast Concrete": "Hormigón Prefabricado",
"Aggregate": "Áridos",
"Allowance": "Asignación",
"Apprenticeship": "Aprendizaje",
"Backfilling": "Relleno",
"Beam": "Viga",
"Blueprint": "Plano",
"Bollard": "Bolardo",
"Bracing": "Arriostramiento",
"Brick Masonry": "Albañilería de Ladrillo",
"Budget Overrun": "Sobrecosto Presupuestario",
"Building Code": "Código de Construcción",
"Cantilever": "Voladizo",
"Caulking": "Sellado",
"Ceiling Joist": "Vigueta de Techo",
"Cement": "Cemento",
"Certificate of Occupancy": "Certificado de Ocupación",
"Chain Link Fence": "Cerca de Malla",
"Chipping": "Picado",
"Cladding": "Revestimiento",
"Clear Span": "Luz Libre",
"Construction Drawings": "Planos de Construcción",
"Construction Schedule": "Cronograma de Construcción",
"Construction Staging": "Escenario de Construcción",
"Contingency": "Contingencia",
"Contract Bond": "Fianza de Contrato",
"Curtain Wall": "Muro Cortina",
"Damp Proofing": "Impermeabilización",
"Dead Load": "Carga Muerta",
"Demolition": "Demolición",
"Design Development": "Desarrollo de Diseño",
"Detail Drawing": "Dibujo Detallado",
"Drilled Pier": "Pilote Perforado",
"Drywall": "Tablaroca",
"Earthmoving": "Movimiento de Tierras",
"Elevation": "Elevación",
"Emergency Egress": "Salida de Emergencia",
"Energy Modeling": "Modelado Energético",
"Excavation": "Excavación",
"Expansion Joint": "Junta de Expansión",
"Exterior Finish": "Acabado Exterior",
"Fasteners": "Fijaciones",
"Field Verification": "Verificación en Campo",
"Final Walkthrough": "Inspección Final",
"Finishing": "Acabado",
"Fire Barrier": "Barrera Contra Incendios",
"Flange": "Brida",
"Floor Slab": "Losa de Piso",
"Footing": "Zapata",
"Foundation Wall": "Muro de Cimentación",
"Framing": "Estructura",
"Frost Line": "Línea de Heladas",
"Grade Beam": "Viga de Cimentación",
"Grading": "Nivelación",
"Ground Penetrating Radar": "Radar de Penetración Terrestre",
"Hardscape": "Paisajismo Duro",
"HVAC Ductwork": "Conductos HVAC",
"I-Beam": "Viga en I",
"Insulation Rating": "Clasificación de Aislamiento",
"Interior Finish": "Acabado Interior",
"Joist": "Vigueta",
"Keyway": "Entalladura",
"Lath": "Listón",
"Load Testing": "Prueba de Carga",
"Louver": "Persiana",
"Masonry": "Albañilería",
"Moisture Barrier": "Barrera de Humedad",
"Mortar": "Mortero",
"Overhang": "Voladizo",
"Parapet": "Pretil",
"Pile": "Pilote",
"Plaster": "Yeso",
"Plumbing Fixtures": "Accesorios de Plomería",
"Post-Tension Slab": "Losa Postensada",
"Prefabricated Components": "Componentes Prefabricados",
"Safety Harness": "Arnés de Seguridad",
"Scaffolding": "Andamios",
"Structural Integrity": "Integridad Estructural",
"Structural Steel": "Acero Estructural",
"Temporary Fence": "Cerca Temporal",
"Zoning Ordinance": "Ordenanza de Zonificación",
"Thermal Insulation": "Aislamiento Térmico",
"Sealant": "Sellador",
"Retaining Wall": "Muro de Contención",
"Screw Anchor": "Anclaje de Tornillo",
"Rain Screen": "Pantalla de Lluvia",
"Facade Treatment": "Tratamiento de Fachada",
"Fireproofing": "Protección contra Incendios",
}
# Preprocess text to replace glossary terms with placeholders
def preprocess_text(text, glossary):
sorted_terms = sorted(glossary.keys(), key=len, reverse=True)
for term in sorted_terms:
placeholder = f"[[{term}]]"
text = re.sub(rf'\b{re.escape(term)}\b', placeholder, text, flags=re.IGNORECASE)
return text
# Postprocess text to replace placeholders with glossary terms
def postprocess_text(text, glossary):
for term, translation in glossary.items():
placeholder = f"[[{term}]]"
text = text.replace(placeholder, translation)
return text
# Translate text
def translate_text(text):
try:
# Preprocess the text
preprocessed_text = preprocess_text(text, custom_glossary)
# Translate the preprocessed text
translated = translator(preprocessed_text)[0]['translation_text']
# Postprocess the text
final_translation = postprocess_text(translated, custom_glossary)
return final_translation
except Exception as e:
return f"An error occurred during translation: {e}"
# Create the Gradio interface
iface = gr.Interface(
fn=translate_text,
inputs=gr.Textbox(lines=5, label="Input Text (English)"),
outputs=gr.Textbox(label="Translated Text (Spanish)"),
title="English to Spanish Translator",
description="Enter English text to translate it into Spanish, with accurate translation of construction and project management terms.",
)
# Launch the interface
if __name__ == "__main__":
iface.launch()
|