File size: 1,241 Bytes
3942755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI, UploadFile, HTTPException
import os
import subprocess
import nest_asyncio
import uvicorn

# تثبيت المكتبات المطلوبة
subprocess.check_call(["pip", "install", "nest_asyncio"])
subprocess.check_call(["pip", "install", "pydub"])

app = FastAPI()

@app.post("/ask-question/")
async def ask_question(file: UploadFile):
    try:
        # Print some file information for debugging
        print(f"File name: {file.filename}")
        print(f"File content type: {file.content_type}")

        # Ensure the directory exists
        os.makedirs("/tmp", exist_ok=True)

        # Define the file location
        file_location = f"/tmp/{file.filename}"
        with open(file_location, "wb") as f:
            f.write(await file.read())

        # Process the question from the audio file
        answer, audio_response = process_audio_question(file_location)

        return {"text_answer": answer, "audio_response": audio_response}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    # Apply the patch
    nest_asyncio.apply()

    # Run the FastAPI app within the existing event loop
    uvicorn.run(app, host="0.0.0.0", port=8000)