Evoblack commited on
Commit
b7393db
·
verified ·
1 Parent(s): 61fb24c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -1,13 +1,19 @@
1
- """
2
- Marathi Translation API with AI4Bharat IndicTrans2
 
 
 
 
 
 
 
 
3
  High-quality translation service for English <-> Marathi
4
  """
5
-
6
  from flask import Flask, request, jsonify
7
  from flask_cors import CORS
8
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
9
  from pathlib import Path
10
- import os
11
  import json
12
  import torch
13
 
@@ -28,6 +34,7 @@ LANG_CODE_MAP = {
28
  "mr": "mar_Deva"
29
  }
30
 
 
31
  def load_translations_dict():
32
  """Load the custom translations dictionary"""
33
  global translations_dict
@@ -46,6 +53,7 @@ def load_translations_dict():
46
 
47
  return translations_dict
48
 
 
49
  def load_model():
50
  """Load the IndicTrans2 translation model"""
51
  global model, tokenizer
@@ -80,6 +88,7 @@ def load_model():
80
  print(f"✗ Failed to load model: {e}")
81
  raise
82
 
 
83
  def translate_with_indictrans2(text, source_lang, target_lang):
84
  """
85
  Translate using IndicTrans2 model
@@ -125,6 +134,7 @@ def translate_with_indictrans2(text, source_lang, target_lang):
125
 
126
  return translation
127
 
 
128
  def translate_with_dict(text, source, target):
129
  """
130
  Translate using dictionary first, fallback to IndicTrans2
@@ -151,6 +161,7 @@ def translate_with_dict(text, source, target):
151
  print(f"Translation error: {e}")
152
  return text, False
153
 
 
154
  @app.route('/')
155
  def home():
156
  """Home endpoint with API information"""
@@ -166,6 +177,7 @@ def home():
166
  }
167
  })
168
 
 
169
  @app.route('/health')
170
  def health():
171
  """Health check endpoint"""
@@ -184,6 +196,7 @@ def health():
184
  "error": str(e)
185
  }), 503
186
 
 
187
  @app.route('/languages', methods=['GET'])
188
  def languages():
189
  """Get supported languages"""
@@ -200,6 +213,7 @@ def languages():
200
  }
201
  ])
202
 
 
203
  @app.route('/translate', methods=['POST'])
204
  def translate():
205
  """
@@ -266,21 +280,24 @@ def translate():
266
  }
267
 
268
  return jsonify(response), 200
269
-
270
  except Exception as e:
271
  print(f"Error in translate endpoint: {e}")
272
  import traceback
273
  traceback.print_exc()
274
  return jsonify({"error": str(e)}), 500
275
 
 
276
  @app.errorhandler(404)
277
  def not_found(e):
278
  return jsonify({"error": "Endpoint not found"}), 404
279
 
 
280
  @app.errorhandler(500)
281
  def server_error(e):
282
  return jsonify({"error": "Internal server error"}), 500
283
 
 
284
  if __name__ == '__main__':
285
  # Pre-load model
286
  print("=" * 60)
@@ -290,6 +307,7 @@ if __name__ == '__main__':
290
  try:
291
  load_translations_dict()
292
  load_model()
 
293
  print("\nStarting server...")
294
 
295
  # Get port from environment or use default (7860 for HF Spaces)
@@ -301,4 +319,4 @@ if __name__ == '__main__':
301
  print(f"\n✗ Failed to start server: {e}")
302
  import traceback
303
  traceback.print_exc()
304
- exit(1)
 
1
+ # --- CLEANUP CODE ADDED HERE ---
2
+ # This will run once when the Space starts up.
3
+ import os
4
+ print("🧹 Running cleanup command...")
5
+ os.system("huggingface-cli delete-cache && rm -rf ~/.cache/pip")
6
+ print("✅ Cleanup complete.")
7
+ # -----------------------------
8
+
9
+
10
+ """Marathi Translation API with AI4Bharat IndicTrans2
11
  High-quality translation service for English <-> Marathi
12
  """
 
13
  from flask import Flask, request, jsonify
14
  from flask_cors import CORS
15
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
16
  from pathlib import Path
 
17
  import json
18
  import torch
19
 
 
34
  "mr": "mar_Deva"
35
  }
36
 
37
+
38
  def load_translations_dict():
39
  """Load the custom translations dictionary"""
40
  global translations_dict
 
53
 
54
  return translations_dict
55
 
56
+
57
  def load_model():
58
  """Load the IndicTrans2 translation model"""
59
  global model, tokenizer
 
88
  print(f"✗ Failed to load model: {e}")
89
  raise
90
 
91
+
92
  def translate_with_indictrans2(text, source_lang, target_lang):
93
  """
94
  Translate using IndicTrans2 model
 
134
 
135
  return translation
136
 
137
+
138
  def translate_with_dict(text, source, target):
139
  """
140
  Translate using dictionary first, fallback to IndicTrans2
 
161
  print(f"Translation error: {e}")
162
  return text, False
163
 
164
+
165
  @app.route('/')
166
  def home():
167
  """Home endpoint with API information"""
 
177
  }
178
  })
179
 
180
+
181
  @app.route('/health')
182
  def health():
183
  """Health check endpoint"""
 
196
  "error": str(e)
197
  }), 503
198
 
199
+
200
  @app.route('/languages', methods=['GET'])
201
  def languages():
202
  """Get supported languages"""
 
213
  }
214
  ])
215
 
216
+
217
  @app.route('/translate', methods=['POST'])
218
  def translate():
219
  """
 
280
  }
281
 
282
  return jsonify(response), 200
283
+
284
  except Exception as e:
285
  print(f"Error in translate endpoint: {e}")
286
  import traceback
287
  traceback.print_exc()
288
  return jsonify({"error": str(e)}), 500
289
 
290
+
291
  @app.errorhandler(404)
292
  def not_found(e):
293
  return jsonify({"error": "Endpoint not found"}), 404
294
 
295
+
296
  @app.errorhandler(500)
297
  def server_error(e):
298
  return jsonify({"error": "Internal server error"}), 500
299
 
300
+
301
  if __name__ == '__main__':
302
  # Pre-load model
303
  print("=" * 60)
 
307
  try:
308
  load_translations_dict()
309
  load_model()
310
+
311
  print("\nStarting server...")
312
 
313
  # Get port from environment or use default (7860 for HF Spaces)
 
319
  print(f"\n✗ Failed to start server: {e}")
320
  import traceback
321
  traceback.print_exc()
322
+ exit(1)