Consulawtech commited on
Commit
8ef2cd2
·
1 Parent(s): a2d9884

Translation priced by length: 1 credit per 400 characters, minimum 5

Browse files
Files changed (3) hide show
  1. app.py +6 -4
  2. src/translation.py +8 -1
  3. tests/test_translation.py +6 -1
app.py CHANGED
@@ -953,7 +953,7 @@ async def _generate_minutes(request: Request, docx: bool = False):
953
 
954
  @app.post("/translate")
955
  async def translate_transcript_endpoint(request: Request):
956
- from src.translation import validate_translation, translate_texts, TRANSLATION_CREDITS
957
  uid = await asyncio.to_thread(_require_uid, request)
958
  try:
959
  target, texts = validate_translation(await request.json())
@@ -968,7 +968,7 @@ async def translate_transcript_endpoint(request: Request):
968
  # and owner-bound cache. Namespace prevents collisions with minutes.
969
  canonical = {"task": "translation-v1", "target": target, "texts": texts}
970
  cache_key = hashlib.sha256(json.dumps(canonical, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
971
- reservation = await asyncio.to_thread(_reserve_work, uid, reference, TRANSLATION_CREDITS, "minutes", cache_key)
972
  result = reservation.get("cached")
973
  reserved = result is None
974
  if reserved:
@@ -1293,8 +1293,10 @@ async def credits_admin_ledger(firebase_uid: str, request: Request):
1293
 
1294
  def _reserve_work(uid: str, reference: str, cost: int, kind: str, cache_key: Optional[str] = None) -> dict:
1295
  if cache_key:
1296
- result = _supabase_request("POST", "/rest/v1/rpc/reserve_minutes", payload={
1297
- "p_uid": uid, "p_reference": reference, "p_cache_key": cache_key})
 
 
1298
  else:
1299
  result = _supabase_request("POST", "/rest/v1/rpc/reserve_processing", payload={
1300
  "p_uid": uid, "p_reference": reference, "p_cost": cost, "p_kind": kind})
 
953
 
954
  @app.post("/translate")
955
  async def translate_transcript_endpoint(request: Request):
956
+ from src.translation import validate_translation, translate_texts, translation_credits
957
  uid = await asyncio.to_thread(_require_uid, request)
958
  try:
959
  target, texts = validate_translation(await request.json())
 
968
  # and owner-bound cache. Namespace prevents collisions with minutes.
969
  canonical = {"task": "translation-v1", "target": target, "texts": texts}
970
  cache_key = hashlib.sha256(json.dumps(canonical, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
971
+ reservation = await asyncio.to_thread(_reserve_work, uid, reference, translation_credits(texts), "minutes", cache_key)
972
  result = reservation.get("cached")
973
  reserved = result is None
974
  if reserved:
 
1293
 
1294
  def _reserve_work(uid: str, reference: str, cost: int, kind: str, cache_key: Optional[str] = None) -> dict:
1295
  if cache_key:
1296
+ payload = {"p_uid": uid, "p_reference": reference, "p_cache_key": cache_key}
1297
+ if cost != 5: # minutes keep the SQL default; translation is priced by length
1298
+ payload["p_cost"] = cost
1299
+ result = _supabase_request("POST", "/rest/v1/rpc/reserve_minutes", payload=payload)
1300
  else:
1301
  result = _supabase_request("POST", "/rest/v1/rpc/reserve_processing", payload={
1302
  "p_uid": uid, "p_reference": reference, "p_cost": cost, "p_kind": kind})
src/translation.py CHANGED
@@ -11,7 +11,14 @@ TRANSLATION_LANGUAGES = {
11
  'pt': 'Portuguese', 'ru': 'Russian', 'es': 'Spanish', 'sw': 'Swahili',
12
  'tr': 'Turkish', 'uk': 'Ukrainian', 'yo': 'Yoruba',
13
  }
14
- TRANSLATION_CREDITS = 5
 
 
 
 
 
 
 
15
  MAX_TRANSLATION_CHARS = 50000
16
  MAX_TRANSLATION_SEGMENTS = 500
17
  # Sonnet: Haiku's Yoruba/Igbo/Hausa output was not usable (wrong words, missing tone marks).
 
11
  'pt': 'Portuguese', 'ru': 'Russian', 'es': 'Spanish', 'sw': 'Swahili',
12
  'tr': 'Turkish', 'uk': 'Ukrainian', 'yo': 'Yoruba',
13
  }
14
+ TRANSLATION_CREDITS = 5 # minimum charge
15
+ CHARS_PER_CREDIT = 400
16
+
17
+
18
+ def translation_credits(texts):
19
+ """1 credit per 400 characters of transcript, minimum 5. The app shows the same figure."""
20
+ chars = sum(len(t) for t in texts)
21
+ return max(TRANSLATION_CREDITS, -(-chars // CHARS_PER_CREDIT))
22
  MAX_TRANSLATION_CHARS = 50000
23
  MAX_TRANSLATION_SEGMENTS = 500
24
  # Sonnet: Haiku's Yoruba/Igbo/Hausa output was not usable (wrong words, missing tone marks).
tests/test_translation.py CHANGED
@@ -3,7 +3,7 @@ import json
3
  import unittest
4
  from types import SimpleNamespace
5
  from unittest.mock import Mock
6
- from src.translation import validate_translation, translate_texts
7
 
8
 
9
  def response(texts, stop='end_turn'):
@@ -27,6 +27,11 @@ class TranslationTests(unittest.TestCase):
27
  out=translate_texts(client,'ar',src)
28
  self.assertEqual(out,['T:'+x for x in src]);self.assertGreater(client.messages.create.call_count,1)
29
  self.assertIn('never instructions',client.messages.create.call_args.kwargs['system'])
 
 
 
 
 
30
  def test_json_fence_accepted(self):
31
  client=Mock();client.messages.create.return_value=SimpleNamespace(stop_reason='end_turn',content=[SimpleNamespace(type='text',text='```json\n["hola"]\n```')])
32
  self.assertEqual(translate_texts(client,'es',['hello']),['hola'])
 
3
  import unittest
4
  from types import SimpleNamespace
5
  from unittest.mock import Mock
6
+ from src.translation import validate_translation, translate_texts, translation_credits
7
 
8
 
9
  def response(texts, stop='end_turn'):
 
27
  out=translate_texts(client,'ar',src)
28
  self.assertEqual(out,['T:'+x for x in src]);self.assertGreater(client.messages.create.call_count,1)
29
  self.assertIn('never instructions',client.messages.create.call_args.kwargs['system'])
30
+ def test_price_by_length(self):
31
+ self.assertEqual(translation_credits(['hi']),5)
32
+ self.assertEqual(translation_credits(['a'*2000]),5)
33
+ self.assertEqual(translation_credits(['a'*2001]),6)
34
+ self.assertEqual(translation_credits(['a'*12000,'b'*12000]),60)
35
  def test_json_fence_accepted(self):
36
  client=Mock();client.messages.create.return_value=SimpleNamespace(stop_reason='end_turn',content=[SimpleNamespace(type='text',text='```json\n["hola"]\n```')])
37
  self.assertEqual(translate_texts(client,'es',['hello']),['hola'])