Spaces:
Sleeping
Sleeping
Optimize performance: use lighter models and pre-cache models in Dockerfile
Browse files- Dockerfile +4 -0
- app.py +16 -12
Dockerfile
CHANGED
|
@@ -8,6 +8,10 @@ COPY requirements.txt .
|
|
| 8 |
# Install dependencies
|
| 9 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Copy the rest of the application
|
| 12 |
COPY . .
|
| 13 |
|
|
|
|
| 8 |
# Install dependencies
|
| 9 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
|
| 11 |
+
# Pre-download models to cache them in the Docker image
|
| 12 |
+
RUN python -c "from transformers import pipeline; pipeline('summarization', model='sshleifer/distilbart-cnn-6-6')"
|
| 13 |
+
RUN python -c "from transformers import pipeline; pipeline('translation', model='facebook/m2m100_418M')"
|
| 14 |
+
|
| 15 |
# Copy the rest of the application
|
| 16 |
COPY . .
|
| 17 |
|
app.py
CHANGED
|
@@ -27,13 +27,10 @@ async def serve_html():
|
|
| 27 |
|
| 28 |
# Load pre-trained models from Hugging Face Hub
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"es": pipeline("translation", model="Helsinki-NLP/opus-mt-en-es"),
|
| 35 |
-
"ar": pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")
|
| 36 |
-
}
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Error loading models: {str(e)}")
|
| 39 |
raise e
|
|
@@ -60,19 +57,26 @@ async def translate(request: Request):
|
|
| 60 |
data = await request.json()
|
| 61 |
text = data["text"]
|
| 62 |
lang = data["lang"]
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return {"error": "Language not supported"}
|
| 65 |
|
| 66 |
-
# Perform translation
|
| 67 |
-
result =
|
| 68 |
print(f"Translation result for {lang}: {result}") # Debugging
|
| 69 |
|
| 70 |
# Check if result is a list and has at least one item
|
| 71 |
if not isinstance(result, list) or len(result) == 0:
|
| 72 |
return {"error": "Translation failed: empty or invalid result"}
|
| 73 |
|
| 74 |
-
# Extract the translated text
|
| 75 |
-
translation = result[0]
|
| 76 |
if translation is None:
|
| 77 |
return {"error": "Translation failed: 'translation_text' not found in result"}
|
| 78 |
|
|
|
|
| 27 |
|
| 28 |
# Load pre-trained models from Hugging Face Hub
|
| 29 |
try:
|
| 30 |
+
# Use a lighter summarization model
|
| 31 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-6-6")
|
| 32 |
+
# Use a single multilingual translation model
|
| 33 |
+
translator = pipeline("translation", model="facebook/m2m100_418M")
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error loading models: {str(e)}")
|
| 36 |
raise e
|
|
|
|
| 57 |
data = await request.json()
|
| 58 |
text = data["text"]
|
| 59 |
lang = data["lang"]
|
| 60 |
+
# Map language codes to M2M100 language codes
|
| 61 |
+
lang_map = {
|
| 62 |
+
"fr": "fr",
|
| 63 |
+
"de": "de",
|
| 64 |
+
"es": "es",
|
| 65 |
+
"ar": "ar"
|
| 66 |
+
}
|
| 67 |
+
if lang not in lang_map:
|
| 68 |
return {"error": "Language not supported"}
|
| 69 |
|
| 70 |
+
# Perform translation using M2M100
|
| 71 |
+
result = translator(text, src_lang="en", tgt_lang=lang_map[lang])
|
| 72 |
print(f"Translation result for {lang}: {result}") # Debugging
|
| 73 |
|
| 74 |
# Check if result is a list and has at least one item
|
| 75 |
if not isinstance(result, list) or len(result) == 0:
|
| 76 |
return {"error": "Translation failed: empty or invalid result"}
|
| 77 |
|
| 78 |
+
# Extract the translated text
|
| 79 |
+
translation = result[0]["translation_text"]
|
| 80 |
if translation is None:
|
| 81 |
return {"error": "Translation failed: 'translation_text' not found in result"}
|
| 82 |
|