wower99 commited on
Commit
125913a
·
1 Parent(s): e0f02e8

fixing the request type from get to post for generating translation

Browse files
Files changed (1) hide show
  1. utils.py +10 -6
utils.py CHANGED
@@ -7,6 +7,7 @@ from gradio_client import Client
7
 
8
 
9
  def clean_response(result):
 
10
  """A temporary fix to the output of predict which returns output of openai-whisper-large-v3-turbo as string
11
  but it outputs: AutomaticSpeechRecognitionOutput(text=" sometimes life <- like this the class name still remains
12
  in the response, ideally which should have started from "sometimes..." as in the given example """
@@ -16,31 +17,34 @@ def clean_response(result):
16
 
17
  # Extract the text using slicing
18
  cleaned_result = result[start_pos:end_pos]
19
-
20
  return cleaned_result
21
 
22
 
23
  def get_translation(text: str):
 
24
  # Input payload
25
- params = {"text": text}
26
 
27
  # Headers for authentication
28
  headers = {"Authorization": f"Bearer {constants.HF_TOKEN}"}
29
 
30
  try:
31
  # Make a GET request
32
- response = requests.get(constants.TRANSLATION_ENDPOINT, params=params, headers=headers)
33
-
34
  # Process response
35
  if response.status_code == 200:
36
  response_data = response.json()
 
37
  return response_data.get("output", "No output found.")
38
  else:
 
 
39
  print(f"Error: {response.status_code}, {response.text}")
40
- return None
41
  except Exception as e:
42
  print(f"An exception occurred: {e}")
43
- return None
44
 
45
 
46
 
 
7
 
8
 
9
  def clean_response(result):
10
+ print("\n\nStarted Cleaning Response")
11
  """A temporary fix to the output of predict which returns output of openai-whisper-large-v3-turbo as string
12
  but it outputs: AutomaticSpeechRecognitionOutput(text=" sometimes life <- like this the class name still remains
13
  in the response, ideally which should have started from "sometimes..." as in the given example """
 
17
 
18
  # Extract the text using slicing
19
  cleaned_result = result[start_pos:end_pos]
20
+ print("Returning Cleaned Result: ", cleaned_result)
21
  return cleaned_result
22
 
23
 
24
  def get_translation(text: str):
25
+ print('\n\nTranslating text: ', text, type(text))
26
  # Input payload
27
+ data = {"text_input": text}
28
 
29
  # Headers for authentication
30
  headers = {"Authorization": f"Bearer {constants.HF_TOKEN}"}
31
 
32
  try:
33
  # Make a GET request
34
+ response = requests.post(constants.TRANSLATION_ENDPOINT, json=data, headers=headers)
 
35
  # Process response
36
  if response.status_code == 200:
37
  response_data = response.json()
38
+ print("Returning Translation")
39
  return response_data.get("output", "No output found.")
40
  else:
41
+ print("Some Error Occured During Translation Request")
42
+ print(response)
43
  print(f"Error: {response.status_code}, {response.text}")
44
+ return {"error_occured" : response.text}
45
  except Exception as e:
46
  print(f"An exception occurred: {e}")
47
+ return {"error_occured" : e}
48
 
49
 
50