sachin commited on
Commit
a3f8a46
·
1 Parent(s): 17a7288

call-llm-server

Browse files
Files changed (1) hide show
  1. src/server/main.py +8 -70
src/server/main.py CHANGED
@@ -171,7 +171,6 @@ async def generate_audio(
171
  class ChatRequest(BaseModel):
172
  prompt: str
173
  src_lang: str = "kan_Knda" # Default to Kannada
174
- tgt_lang: str = "kan_Knda" # Default to Kannada
175
 
176
  @field_validator("prompt")
177
  def prompt_must_be_valid(cls, v):
@@ -183,40 +182,6 @@ class ChatResponse(BaseModel):
183
  response: str
184
 
185
 
186
- async def call_external_translation(sentences: List[str], src_lang: str, tgt_lang: str) -> List[str]:
187
- external_url = "https://gaganyatri-dhwani-server.hf.space/v1/translate"
188
- payload = {
189
- "sentences": sentences,
190
- "src_lang": src_lang,
191
- "tgt_lang": tgt_lang
192
- }
193
- try:
194
- response = requests.post(
195
- external_url,
196
- json=payload,
197
- headers={
198
- "accept": "application/json",
199
- "Content-Type": "application/json"
200
- },
201
- timeout=10
202
- )
203
- response.raise_for_status()
204
- translations = response.json().get("translations", [])
205
- if not translations or len(translations) != len(sentences):
206
- logger.warning(f"Unexpected response format: {response.json()}")
207
- raise ValueError("Invalid response from translation service")
208
- return translations
209
- except requests.Timeout:
210
- logger.error("Translation request timed out")
211
- raise HTTPException(status_code=504, detail="Translation service timeout")
212
- except requests.RequestException as e:
213
- logger.error(f"Error during translation: {str(e)}")
214
- raise HTTPException(status_code=500, detail=f"Translation failed: {str(e)}")
215
- except ValueError as e:
216
- logger.error(f"Invalid response: {str(e)}")
217
- raise HTTPException(status_code=500, detail=str(e))
218
-
219
-
220
  @app.post("/v1/chat", response_model=ChatResponse)
221
  @limiter.limit(settings.chat_rate_limit)
222
  async def chat(request: Request, chat_request: ChatRequest):
@@ -225,26 +190,13 @@ async def chat(request: Request, chat_request: ChatRequest):
225
  logger.info(f"Received prompt: {chat_request.prompt}, src_lang: {chat_request.src_lang}, tgt_lang: {chat_request.tgt_lang}")
226
 
227
  try:
228
- '''
229
- # Translate prompt to English if src_lang is not English
230
- if chat_request.src_lang != "eng_Latn":
231
- translated_prompt = await call_external_translation(
232
- sentences=[chat_request.prompt],
233
- src_lang=chat_request.src_lang,
234
- tgt_lang="eng_Latn"
235
- )
236
- prompt_to_process = translated_prompt[0]
237
- logger.info(f"Translated prompt to English: {prompt_to_process}")
238
- else:
239
- prompt_to_process = chat_request.prompt
240
- logger.info("Prompt already in English, no translation needed")
241
- '''
242
  # Call the external API instead of llm_manager.generate
243
  external_url = "https://gaganyatri-llm-indic-server.hf.space/v1/chat"
244
  payload = {
245
  "prompt": chat_request.prompt ,
246
- "src_lang": chat_request.src_lang, # Sending English prompt to the external API
247
- "tgt_lang": "eng_Latn" # Expecting English response initially
248
  }
249
 
250
  response = requests.post(
@@ -254,30 +206,16 @@ async def chat(request: Request, chat_request: ChatRequest):
254
  "accept": "application/json",
255
  "Content-Type": "application/json"
256
  },
257
- timeout=10
258
  )
259
  response.raise_for_status() # Raise an exception for bad status codes
260
 
261
  # Extract the response text from the API
262
  response_data = response.json()
263
- english_response = response_data.get("response", "")
264
- logger.info(f"Generated English response from external API: {english_response}")
265
-
266
- '''
267
- # Translate response to target language if tgt_lang is not English
268
- if chat_request.tgt_lang != "eng_Latn":
269
- translated_response = await call_external_translation(
270
- sentences=[english_response],
271
- src_lang="eng_Latn",
272
- tgt_lang=chat_request.tgt_lang
273
- )
274
- final_response = translated_response[0]
275
- logger.info(f"Translated response to {chat_request.tgt_lang}: {final_response}")
276
- else:
277
- final_response = english_response
278
- logger.info("Response kept in English, no translation needed")
279
- '''
280
- return ChatResponse(response=english_response)
281
 
282
  except requests.Timeout:
283
  logger.error("External chat API request timed out")
 
171
  class ChatRequest(BaseModel):
172
  prompt: str
173
  src_lang: str = "kan_Knda" # Default to Kannada
 
174
 
175
  @field_validator("prompt")
176
  def prompt_must_be_valid(cls, v):
 
182
  response: str
183
 
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  @app.post("/v1/chat", response_model=ChatResponse)
186
  @limiter.limit(settings.chat_rate_limit)
187
  async def chat(request: Request, chat_request: ChatRequest):
 
190
  logger.info(f"Received prompt: {chat_request.prompt}, src_lang: {chat_request.src_lang}, tgt_lang: {chat_request.tgt_lang}")
191
 
192
  try:
193
+
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  # Call the external API instead of llm_manager.generate
195
  external_url = "https://gaganyatri-llm-indic-server.hf.space/v1/chat"
196
  payload = {
197
  "prompt": chat_request.prompt ,
198
+ "src_lang": chat_request.src_lang,
199
+ "tgt_lang" : chat_request.src_lang
200
  }
201
 
202
  response = requests.post(
 
206
  "accept": "application/json",
207
  "Content-Type": "application/json"
208
  },
209
+ timeout=30
210
  )
211
  response.raise_for_status() # Raise an exception for bad status codes
212
 
213
  # Extract the response text from the API
214
  response_data = response.json()
215
+ response = response_data.get("response", "")
216
+ logger.info(f"Generated Chat response from external API: {response}")
217
+
218
+ return ChatResponse(response=response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  except requests.Timeout:
221
  logger.error("External chat API request timed out")