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