هذا الكود الخاص بي:
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel
import os
import datetime
from dotenv import load_dotenv
import uvicorn
from io import BytesIO
import tempfile
import openai
from openai import OpenAI


app = FastAPI()

# تشفير سيزار
def caesar_encrypt(text, shift):
    def shift_char(char):
        if char.isalpha():
            shift_base = ord('a') if char.islower() else ord('A')
            return chr((ord(char) - shift_base + shift) % 26 + shift_base)
        return char
    return ''.join(map(shift_char, text))

def caesar_decrypt(text, shift):
    return caesar_encrypt(text, -shift)

# تحميل مفتاح API من البيئة
def load_api_key():
    load_dotenv()
    encrypted_api_key = os.getenv("API_KEY")
    return caesar_decrypt(encrypted_api_key, 3)

# إعداد عميل OpenAI
def create_openai_client(api_key):
    openai.api_key = api_key

# استخدام API Key
api_key = load_api_key()
create_openai_client(api_key)

# نموذج الطلب للدردشة
class ChatRequest(BaseModel):
    messages: list
    text: str

# تحويل الصوت إلى نص
@app.post("/speech_to_text/")
async def speech_to_text_conversion(audio_file: UploadFile = File(...)):
    try:
        audio_bytes = await audio_file.read()
        audio_bio = BytesIO(audio_bytes)
        audio_bio.name = "file.wav"

        transcription = openai.Audio.transcribe(
            model="whisper-1",  
            file=audio_bio  
        )
        return {"transcription": transcription["text"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail="Failed to transcribe audio.")

@app.post("/text_chat/")
async def text_chat(request: ChatRequest):
    """Generate a response using OpenAI based on the context provided."""
    messages = request.messages
    messages.append({"role": "user", "content": request.text})
    
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=messages
    )
    
    bot_response = response.choices[0].message["content"]
    messages.append({"role": "assistant", "content": bot_response})
    
    return {"response": bot_response, "messages": messages}

@app.post("/text_to_speech/")
async def text_to_speech_conversion(text: str = Form(...)):
    """Converts text to audio format message using OpenAI's text-to-speech model - tts-1."""
    if text:
        speech_file_path = datetime.datetime.now().strftime("%Y%m%d%H%M%S") + "_speech.webm"
        response = openai.audio.speech.create(
            model="tts-1",  
            voice="nova",  
            input=text  
        )
       
        response.stream_to_file(speech_file_path)  # Streaming synthesized speech to file
        # Read the audio file as binary data
        with open(speech_file_path, "rb") as audio_file:
            audio_data = audio_file.read()
        os.remove(speech_file_path)
        return audio_data



if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)

وهذا الخطأ عند محاولة استخدام text to speech api:
Traceback (most recent call last):
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 406, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 62, in wrapped_app
    raise exc
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 51, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\routing.py", line 288, in handle
    await self.app(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 62, in wrapped_app
    raise exc
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\_exception_handler.py", line 51, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\starlette\routing.py", line 73, in app
    response = await f(request)
               ^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\fastapi\routing.py", line 327, in app
    content = await serialize_response(
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\fastapi\routing.py", line 201, in serialize_response
    return jsonable_encoder(response_content)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\fastapi\encoders.py", line 289, in jsonable_encoder
    encoded_value = jsonable_encoder(
                    ^^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\fastapi\encoders.py", line 318, in jsonable_encoder
    return ENCODERS_BY_TYPE[type(obj)](obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ebrahim\anaconda3\Lib\site-packages\fastapi\encoders.py", line 59, in <lambda>
    bytes: lambda o: o.decode(),
                     ^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte