Spaces:
Running
Running
sachin
commited on
Commit
·
10e1e1f
1
Parent(s):
1d04e1e
fix-work
Browse files- src/server/main.py +2 -107
src/server/main.py
CHANGED
@@ -436,73 +436,6 @@ async def chat(
|
|
436 |
logger.error(f"Error processing request: {str(e)}")
|
437 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
438 |
|
439 |
-
@app.post("/v1/process_audio/",
|
440 |
-
response_model=AudioProcessingResponse,
|
441 |
-
summary="Process Audio File",
|
442 |
-
description="Process an uploaded audio file in the specified language. Rate limited to 100 requests per minute per user. Requires authentication.",
|
443 |
-
tags=["Audio"],
|
444 |
-
responses={
|
445 |
-
200: {"description": "Processed result", "model": AudioProcessingResponse},
|
446 |
-
401: {"description": "Unauthorized - Token required"},
|
447 |
-
429: {"description": "Rate limit exceeded"},
|
448 |
-
504: {"description": "Audio processing timeout"}
|
449 |
-
})
|
450 |
-
@limiter.limit(settings.chat_rate_limit)
|
451 |
-
async def process_audio(
|
452 |
-
request: Request,
|
453 |
-
file: UploadFile = File(..., description="Audio file to process"),
|
454 |
-
language: str = Query(..., description="Base64-encoded encrypted language of the audio (kannada, hindi, tamil after decryption)"),
|
455 |
-
credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme),
|
456 |
-
x_session_key: str = Header(..., alias="X-Session-Key")
|
457 |
-
):
|
458 |
-
user_id = await get_current_user(credentials)
|
459 |
-
session_key = base64.b64decode(x_session_key)
|
460 |
-
|
461 |
-
# Decrypt the language
|
462 |
-
try:
|
463 |
-
encrypted_language = base64.b64decode(language)
|
464 |
-
decrypted_language = decrypt_data(encrypted_language, session_key).decode("utf-8")
|
465 |
-
except Exception as e:
|
466 |
-
logger.error(f"Language decryption failed: {str(e)}")
|
467 |
-
raise HTTPException(status_code=400, detail="Invalid encrypted language")
|
468 |
-
|
469 |
-
# Validate language
|
470 |
-
allowed_languages = ["kannada", "hindi", "tamil"]
|
471 |
-
if decrypted_language not in allowed_languages:
|
472 |
-
raise HTTPException(status_code=400, detail=f"Language must be one of {allowed_languages}")
|
473 |
-
|
474 |
-
logger.info("Processing audio processing request", extra={
|
475 |
-
"endpoint": "/v1/process_audio",
|
476 |
-
"filename": file.filename,
|
477 |
-
"language": decrypted_language,
|
478 |
-
"client_ip": get_remote_address(request),
|
479 |
-
"user_id": user_id
|
480 |
-
})
|
481 |
-
|
482 |
-
start_time = time()
|
483 |
-
try:
|
484 |
-
file_content = await file.read()
|
485 |
-
files = {"file": (file.filename, file_content, file.content_type)}
|
486 |
-
|
487 |
-
external_url = f"{settings.external_api_base_url}/process_audio/?language={decrypted_language}"
|
488 |
-
response = requests.post(
|
489 |
-
external_url,
|
490 |
-
files=files,
|
491 |
-
headers={"accept": "application/json"},
|
492 |
-
timeout=60
|
493 |
-
)
|
494 |
-
response.raise_for_status()
|
495 |
-
|
496 |
-
processed_result = response.json().get("result", "")
|
497 |
-
logger.info(f"Audio processing completed in {time() - start_time:.2f} seconds")
|
498 |
-
return AudioProcessingResponse(result=processed_result)
|
499 |
-
|
500 |
-
except requests.Timeout:
|
501 |
-
raise HTTPException(status_code=504, detail="Audio processing service timeout")
|
502 |
-
except requests.RequestException as e:
|
503 |
-
logger.error(f"Audio processing request failed: {str(e)}")
|
504 |
-
raise HTTPException(status_code=500, detail=f"Audio processing failed: {str(e)}")
|
505 |
-
|
506 |
@app.post("/v1/transcribe/",
|
507 |
response_model=TranscriptionResponse,
|
508 |
summary="Transcribe Audio File",
|
@@ -564,44 +497,6 @@ async def transcribe_audio(
|
|
564 |
logger.error(f"Transcription request failed: {str(e)}")
|
565 |
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}")
|
566 |
|
567 |
-
@app.post("/v1/chat_v2",
|
568 |
-
response_model=TranscriptionResponse,
|
569 |
-
summary="Chat with Image (V2)",
|
570 |
-
description="Generate a response from a text prompt and optional image. Rate limited to 100 requests per minute per user. Requires authentication.",
|
571 |
-
tags=["Chat"],
|
572 |
-
responses={
|
573 |
-
200: {"description": "Chat response", "model": TranscriptionResponse},
|
574 |
-
400: {"description": "Invalid prompt"},
|
575 |
-
401: {"description": "Unauthorized - Token required"},
|
576 |
-
429: {"description": "Rate limit exceeded"}
|
577 |
-
})
|
578 |
-
@limiter.limit(settings.chat_rate_limit)
|
579 |
-
async def chat_v2(
|
580 |
-
request: Request,
|
581 |
-
prompt: str = Form(..., description="Text prompt for chat"),
|
582 |
-
image: UploadFile = File(default=None, description="Optional image to accompany the prompt"),
|
583 |
-
credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme)
|
584 |
-
):
|
585 |
-
user_id = await get_current_user(credentials)
|
586 |
-
if not prompt:
|
587 |
-
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
588 |
-
|
589 |
-
logger.info("Processing chat_v2 request", extra={
|
590 |
-
"endpoint": "/v1/chat_v2",
|
591 |
-
"prompt_length": len(prompt),
|
592 |
-
"has_image": bool(image),
|
593 |
-
"client_ip": get_remote_address(request),
|
594 |
-
"user_id": user_id
|
595 |
-
})
|
596 |
-
|
597 |
-
try:
|
598 |
-
image_data = Image.open(await image.read()) if image else None
|
599 |
-
response_text = f"Processed: {prompt}" + (" with image" if image_data else "")
|
600 |
-
return TranscriptionResponse(text=response_text)
|
601 |
-
except Exception as e:
|
602 |
-
logger.error(f"Chat_v2 processing failed: {str(e)}", exc_info=True)
|
603 |
-
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
604 |
-
|
605 |
@app.post("/v1/translate",
|
606 |
response_model=TranslationResponse,
|
607 |
summary="Translate Text",
|
@@ -998,7 +893,7 @@ async def speech_to_speech(
|
|
998 |
logger.error(f"External speech-to-speech API error: {str(e)}", extra={"user_id": user_id})
|
999 |
raise HTTPException(status_code=500, detail=f"External API error: {str(e)}")
|
1000 |
|
1001 |
-
|
1002 |
@app.post("/v1/speech_to_speech_v2",
|
1003 |
summary="Speech-to-Speech Conversion",
|
1004 |
description="Convert input encrypted speech to processed speech in the specified encrypted language by calling an external speech-to-speech API. Rate limited to 5 requests per minute per user. Requires authentication and X-Session-Key header.",
|
@@ -1071,7 +966,7 @@ async def speech_to_speech_v2(
|
|
1071 |
logger.error(f"External speech-to-speech API error: {str(e)}")
|
1072 |
raise HTTPException(status_code=500, detail=f"External API error: {str(e)}")
|
1073 |
|
1074 |
-
|
1075 |
if __name__ == "__main__":
|
1076 |
parser = argparse.ArgumentParser(description="Run the FastAPI server.")
|
1077 |
parser.add_argument("--port", type=int, default=settings.port, help="Port to run the server on.")
|
|
|
436 |
logger.error(f"Error processing request: {str(e)}")
|
437 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
438 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
439 |
@app.post("/v1/transcribe/",
|
440 |
response_model=TranscriptionResponse,
|
441 |
summary="Transcribe Audio File",
|
|
|
497 |
logger.error(f"Transcription request failed: {str(e)}")
|
498 |
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}")
|
499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
500 |
@app.post("/v1/translate",
|
501 |
response_model=TranslationResponse,
|
502 |
summary="Translate Text",
|
|
|
893 |
logger.error(f"External speech-to-speech API error: {str(e)}", extra={"user_id": user_id})
|
894 |
raise HTTPException(status_code=500, detail=f"External API error: {str(e)}")
|
895 |
|
896 |
+
'''
|
897 |
@app.post("/v1/speech_to_speech_v2",
|
898 |
summary="Speech-to-Speech Conversion",
|
899 |
description="Convert input encrypted speech to processed speech in the specified encrypted language by calling an external speech-to-speech API. Rate limited to 5 requests per minute per user. Requires authentication and X-Session-Key header.",
|
|
|
966 |
logger.error(f"External speech-to-speech API error: {str(e)}")
|
967 |
raise HTTPException(status_code=500, detail=f"External API error: {str(e)}")
|
968 |
|
969 |
+
'''
|
970 |
if __name__ == "__main__":
|
971 |
parser = argparse.ArgumentParser(description="Run the FastAPI server.")
|
972 |
parser.add_argument("--port", type=int, default=settings.port, help="Port to run the server on.")
|