Spaces:
Runtime error
Runtime error
lukecurtin32
commited on
Commit
•
0f25099
1
Parent(s):
9ba7c98
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
import re
|
|
|
|
|
4 |
|
5 |
# Initialize the translation pipeline
|
6 |
-
translator = pipeline(
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Define the custom glossary
|
9 |
custom_glossary = {
|
@@ -102,7 +107,6 @@ custom_glossary = {
|
|
102 |
"Retaining Wall": "Muro de Contención",
|
103 |
"Screw Anchor": "Anclaje de Tornillo",
|
104 |
"Rain Screen": "Pantalla de Lluvia",
|
105 |
-
"Finishing": "Acabado",
|
106 |
"Facade Treatment": "Tratamiento de Fachada",
|
107 |
"Fireproofing": "Protección contra Incendios",
|
108 |
}
|
@@ -112,7 +116,7 @@ def preprocess_text(text, glossary):
|
|
112 |
sorted_terms = sorted(glossary.keys(), key=len, reverse=True)
|
113 |
for term in sorted_terms:
|
114 |
placeholder = f"[[{term}]]"
|
115 |
-
text = re.sub(rf'\b{re.escape(term)}\b', placeholder, text)
|
116 |
return text
|
117 |
|
118 |
# Postprocess text to replace placeholders with glossary terms
|
@@ -133,7 +137,7 @@ def translate_text(text):
|
|
133 |
final_translation = postprocess_text(translated, custom_glossary)
|
134 |
return final_translation
|
135 |
except Exception as e:
|
136 |
-
return f"An error occurred: {e}"
|
137 |
|
138 |
# Create the Gradio interface
|
139 |
iface = gr.Interface(
|
@@ -141,28 +145,8 @@ iface = gr.Interface(
|
|
141 |
inputs=gr.Textbox(lines=5, label="Input Text (English)"),
|
142 |
outputs=gr.Textbox(label="Translated Text (Spanish)"),
|
143 |
title="English to Spanish Translator",
|
144 |
-
description="Enter English text to translate it into Spanish, with accurate translation of construction and project management terms."
|
145 |
)
|
146 |
-
import os
|
147 |
-
import subprocess
|
148 |
-
|
149 |
-
# Install the transformers library if not already installed
|
150 |
-
try:
|
151 |
-
import transformers
|
152 |
-
except ImportError:
|
153 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "transformers"])
|
154 |
-
import transformers
|
155 |
-
|
156 |
-
from transformers import pipeline
|
157 |
-
import gradio as gr
|
158 |
-
import re
|
159 |
-
import torch
|
160 |
-
print(torch.__version__)
|
161 |
-
print(torch.cuda.is_available())
|
162 |
-
from transformers import pipeline
|
163 |
-
|
164 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-es")
|
165 |
-
print(translator("Hello, how are you?"))
|
166 |
|
167 |
# Launch the interface
|
168 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import re
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline
|
5 |
|
6 |
# Initialize the translation pipeline
|
7 |
+
translator = pipeline(
|
8 |
+
"translation",
|
9 |
+
model="Helsinki-NLP/opus-mt-tc-big-en-es",
|
10 |
+
device=0 if torch.cuda.is_available() else -1, # Use GPU if available, otherwise CPU
|
11 |
+
)
|
12 |
|
13 |
# Define the custom glossary
|
14 |
custom_glossary = {
|
|
|
107 |
"Retaining Wall": "Muro de Contención",
|
108 |
"Screw Anchor": "Anclaje de Tornillo",
|
109 |
"Rain Screen": "Pantalla de Lluvia",
|
|
|
110 |
"Facade Treatment": "Tratamiento de Fachada",
|
111 |
"Fireproofing": "Protección contra Incendios",
|
112 |
}
|
|
|
116 |
sorted_terms = sorted(glossary.keys(), key=len, reverse=True)
|
117 |
for term in sorted_terms:
|
118 |
placeholder = f"[[{term}]]"
|
119 |
+
text = re.sub(rf'\b{re.escape(term)}\b', placeholder, text, flags=re.IGNORECASE)
|
120 |
return text
|
121 |
|
122 |
# Postprocess text to replace placeholders with glossary terms
|
|
|
137 |
final_translation = postprocess_text(translated, custom_glossary)
|
138 |
return final_translation
|
139 |
except Exception as e:
|
140 |
+
return f"An error occurred during translation: {e}"
|
141 |
|
142 |
# Create the Gradio interface
|
143 |
iface = gr.Interface(
|
|
|
145 |
inputs=gr.Textbox(lines=5, label="Input Text (English)"),
|
146 |
outputs=gr.Textbox(label="Translated Text (Spanish)"),
|
147 |
title="English to Spanish Translator",
|
148 |
+
description="Enter English text to translate it into Spanish, with accurate translation of construction and project management terms.",
|
149 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
# Launch the interface
|
152 |
if __name__ == "__main__":
|