sachin commited on
Commit
2f5178f
·
1 Parent(s): 1452c7a

add-translate

Browse files
Files changed (2) hide show
  1. docs/tranlate.md +66 -0
  2. src/server/main.py +60 -0
docs/tranlate.md ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ curl -X 'POST' \
4
+ 'https://gaganyatri-translate-indic-server-cpu.hf.space/translate?src_lang=eng_Latn&tgt_lang=kan_Knda' \
5
+ -H 'accept: application/json' \
6
+ -H 'Content-Type: application/json' \
7
+ -d '{
8
+ "sentences": [
9
+ "Hello, how are you?", "Good morning!"
10
+ ],
11
+ "src_lang": "eng_Latn",
12
+ "tgt_lang": "kan_Knda"
13
+ }'
14
+
15
+
16
+ {
17
+ "translations": [
18
+ "ಹಲೋ, ಹೇಗಿದ್ದೀರಿ? ",
19
+ "ಶುಭೋದಯ! "
20
+ ]
21
+ }
22
+
23
+
24
+
25
+
26
+ curl -X 'POST' \
27
+ 'https://gaganyatri-translate-indic-server-cpu.hf.space/translate?src_lang=kan_Knda&tgt_lang=eng_Latn' \
28
+ -H 'accept: application/json' \
29
+ -H 'Content-Type: application/json' \
30
+ -d '{
31
+ "sentences": [
32
+ "ನಮಸ್ಕಾರ, ಹೇಗಿದ್ದೀರಾ?", "ಶುಭೋದಯ!"
33
+ ],
34
+ "src_lang": "kan_Knda",
35
+ "tgt_lang": "eng_Latn"
36
+ }'
37
+
38
+
39
+ {
40
+ "translations": [
41
+ "Hello, how are you?",
42
+ "Good morning!"
43
+ ]
44
+ }
45
+
46
+
47
+
48
+ curl -X 'POST' \
49
+ 'https://gaganyatri-translate-indic-server-cpu.hf.space/translate?src_lang=kan_Knda&tgt_lang=hin_Deva' \
50
+ -H 'accept: application/json' \
51
+ -H 'Content-Type: application/json' \
52
+ -d '{
53
+ "sentences": [
54
+ "ನಮಸ್ಕಾರ, ಹೇಗಿದ್ದೀರಾ?", "ಶುಭೋದಯ!"
55
+ ],
56
+ "src_lang": "kan_Knda",
57
+ "tgt_lang": "hin_Deva"
58
+ }'
59
+
60
+
61
+ {
62
+ "translations": [
63
+ "हैलो, कैसा लग रहा है? ",
64
+ "गुड मॉर्निंग! "
65
+ ]
66
+ }
src/server/main.py CHANGED
@@ -307,6 +307,66 @@ async def chat_v2(
307
  except Exception as e:
308
  logger.error(f"Chat_v2 processing failed: {str(e)}", exc_info=True)
309
  raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
 
311
  if __name__ == "__main__":
312
  parser = argparse.ArgumentParser(description="Run the FastAPI server.")
 
307
  except Exception as e:
308
  logger.error(f"Chat_v2 processing failed: {str(e)}", exc_info=True)
309
  raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
310
+
311
+ class TranslationRequest(BaseModel):
312
+ sentences: list[str]
313
+ src_lang: str
314
+ tgt_lang: str
315
+
316
+ class TranslationResponse(BaseModel):
317
+ translations: list[str]
318
+
319
+ @app.post("/v1/translate", response_model=TranslationResponse)
320
+ async def translate(request: TranslationRequest):
321
+ logger.info(f"Received translation request: {request.dict()}")
322
+
323
+ # External API endpoint
324
+ external_url = f"https://gaganyatri-translate-indic-server-cpu.hf.space/translate?src_lang={request.src_lang}&tgt_lang={request.tgt_lang}"
325
+
326
+ # Prepare the payload matching the external API's expected format
327
+ payload = {
328
+ "sentences": request.sentences,
329
+ "src_lang": request.src_lang,
330
+ "tgt_lang": request.tgt_lang
331
+ }
332
+
333
+ try:
334
+ # Make the POST request to the external API
335
+ response = requests.post(
336
+ external_url,
337
+ json=payload,
338
+ headers={
339
+ "accept": "application/json",
340
+ "Content-Type": "application/json"
341
+ },
342
+ timeout=10 # Set a timeout to avoid hanging
343
+ )
344
+
345
+ # Raise an exception for bad status codes (4xx, 5xx)
346
+ response.raise_for_status()
347
+
348
+ # Extract translations from the response
349
+ response_data = response.json()
350
+ translations = response_data.get("translations", [])
351
+
352
+ if not translations or len(translations) != len(request.sentences):
353
+ logger.warning(f"Unexpected response format: {response_data}")
354
+ raise HTTPException(status_code=500, detail="Invalid response from translation service")
355
+
356
+ logger.info(f"Translation successful: {translations}")
357
+ return TranslationResponse(translations=translations)
358
+
359
+ except requests.Timeout:
360
+ logger.error("Translation request timed out")
361
+ raise HTTPException(status_code=504, detail="Translation service timeout")
362
+ except requests.RequestException as e:
363
+ logger.error(f"Error during translation: {str(e)}")
364
+ raise HTTPException(status_code=500, detail=f"Translation failed: {str(e)}")
365
+ except ValueError as e:
366
+ logger.error(f"Invalid JSON response: {str(e)}")
367
+ raise HTTPException(status_code=500, detail="Invalid response format from translation service")
368
+
369
+
370
 
371
  if __name__ == "__main__":
372
  parser = argparse.ArgumentParser(description="Run the FastAPI server.")