Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
sachin
commited on
Commit
·
ce5948a
1
Parent(s):
2f5178f
connect to -llm indic server
Browse files- src/server/main.py +107 -25
src/server/main.py
CHANGED
@@ -167,43 +167,125 @@ async def generate_audio(
|
|
167 |
headers=headers
|
168 |
)
|
169 |
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
@limiter.limit(settings.chat_rate_limit)
|
172 |
-
async def
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
request: Request = None,
|
177 |
-
):
|
178 |
-
logger.info("Processing text generation request", extra={
|
179 |
-
"endpoint": "/v1/generate_text",
|
180 |
-
"filename": file.filename,
|
181 |
-
"client_ip": get_remote_address(request)
|
182 |
-
})
|
183 |
|
184 |
-
start_time = time()
|
185 |
try:
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
-
external_url = f"{settings.external_text_gen_url}/generate_text/?language={language}"
|
190 |
response = requests.post(
|
191 |
external_url,
|
192 |
-
|
193 |
-
headers={
|
|
|
|
|
|
|
194 |
timeout=10
|
195 |
)
|
196 |
-
response.raise_for_status()
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
except requests.Timeout:
|
203 |
-
|
|
|
204 |
except requests.RequestException as e:
|
205 |
-
logger.error(f"
|
206 |
-
raise HTTPException(status_code=500, detail=f"
|
|
|
|
|
|
|
207 |
|
208 |
@app.post("/v1/process_audio/", response_model=AudioProcessingResponse)
|
209 |
@limiter.limit(settings.chat_rate_limit)
|
|
|
167 |
headers=headers
|
168 |
)
|
169 |
|
170 |
+
|
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):
|
178 |
+
if len(v) > 1000:
|
179 |
+
raise ValueError("Prompt cannot exceed 1000 characters")
|
180 |
+
return v.strip()
|
181 |
+
|
182 |
+
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):
|
223 |
+
if not chat_request.prompt:
|
224 |
+
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
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 |
+
# Translate prompt to English if src_lang is not English
|
229 |
+
if chat_request.src_lang != "eng_Latn":
|
230 |
+
translated_prompt = await call_external_translation(
|
231 |
+
sentences=[chat_request.prompt],
|
232 |
+
src_lang=chat_request.src_lang,
|
233 |
+
tgt_lang="eng_Latn"
|
234 |
+
)
|
235 |
+
prompt_to_process = translated_prompt[0]
|
236 |
+
logger.info(f"Translated prompt to English: {prompt_to_process}")
|
237 |
+
else:
|
238 |
+
prompt_to_process = chat_request.prompt
|
239 |
+
logger.info("Prompt already in English, no translation needed")
|
240 |
+
|
241 |
+
# Call the external API instead of llm_manager.generate
|
242 |
+
external_url = "https://gaganyatri-llm-indic-server.hf.space/v1/chat"
|
243 |
+
payload = {
|
244 |
+
"prompt": prompt_to_process,
|
245 |
+
"src_lang": "eng_Latn", # Sending English prompt to the external API
|
246 |
+
"tgt_lang": "eng_Latn" # Expecting English response initially
|
247 |
+
}
|
248 |
|
|
|
249 |
response = requests.post(
|
250 |
external_url,
|
251 |
+
json=payload,
|
252 |
+
headers={
|
253 |
+
"accept": "application/json",
|
254 |
+
"Content-Type": "application/json"
|
255 |
+
},
|
256 |
timeout=10
|
257 |
)
|
258 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
259 |
|
260 |
+
# Extract the response text from the API
|
261 |
+
response_data = response.json()
|
262 |
+
english_response = response_data.get("response", "")
|
263 |
+
logger.info(f"Generated English response from external API: {english_response}")
|
264 |
+
|
265 |
+
# Translate response to target language if tgt_lang is not English
|
266 |
+
if chat_request.tgt_lang != "eng_Latn":
|
267 |
+
translated_response = await call_external_translation(
|
268 |
+
sentences=[english_response],
|
269 |
+
src_lang="eng_Latn",
|
270 |
+
tgt_lang=chat_request.tgt_lang
|
271 |
+
)
|
272 |
+
final_response = translated_response[0]
|
273 |
+
logger.info(f"Translated response to {chat_request.tgt_lang}: {final_response}")
|
274 |
+
else:
|
275 |
+
final_response = english_response
|
276 |
+
logger.info("Response kept in English, no translation needed")
|
277 |
+
|
278 |
+
return ChatResponse(response=final_response)
|
279 |
|
280 |
except requests.Timeout:
|
281 |
+
logger.error("External chat API request timed out")
|
282 |
+
raise HTTPException(status_code=504, detail="Chat service timeout")
|
283 |
except requests.RequestException as e:
|
284 |
+
logger.error(f"Error calling external chat API: {str(e)}")
|
285 |
+
raise HTTPException(status_code=500, detail=f"Chat failed: {str(e)}")
|
286 |
+
except Exception as e:
|
287 |
+
logger.error(f"Error processing request: {str(e)}")
|
288 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
289 |
|
290 |
@app.post("/v1/process_audio/", response_model=AudioProcessingResponse)
|
291 |
@limiter.limit(settings.chat_rate_limit)
|