Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,40 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, HTTPException
|
| 2 |
-
import os
|
| 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 |
-
|
| 42 |
-
# Run the FastAPI app within the existing event loop
|
| 43 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, HTTPException
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import nest_asyncio
|
| 5 |
+
import uvicorn
|
| 6 |
+
|
| 7 |
+
# تثبيت المكتبات المطلوبة
|
| 8 |
+
subprocess.check_call(["pip", "install", "nest_asyncio"])
|
| 9 |
+
subprocess.check_call(["pip", "install", "pydub"])
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
@app.post("/ask-question/")
|
| 14 |
+
async def ask_question(file: UploadFile):
|
| 15 |
+
try:
|
| 16 |
+
# Print some file information for debugging
|
| 17 |
+
print(f"File name: {file.filename}")
|
| 18 |
+
print(f"File content type: {file.content_type}")
|
| 19 |
+
|
| 20 |
+
# Ensure the directory exists
|
| 21 |
+
os.makedirs("/tmp", exist_ok=True)
|
| 22 |
+
|
| 23 |
+
# Define the file location
|
| 24 |
+
file_location = f"/tmp/{file.filename}"
|
| 25 |
+
with open(file_location, "wb") as f:
|
| 26 |
+
f.write(await file.read())
|
| 27 |
+
|
| 28 |
+
# Process the question from the audio file
|
| 29 |
+
answer, audio_response = process_audio_question(file_location)
|
| 30 |
+
|
| 31 |
+
return {"text_answer": answer, "audio_response": audio_response}
|
| 32 |
+
except Exception as e:
|
| 33 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
# Apply the patch
|
| 37 |
+
nest_asyncio.apply()
|
| 38 |
+
|
| 39 |
+
# Run the FastAPI app within the existing event loop
|
| 40 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|