kareem2222 commited on
Commit
3942755
1 Parent(s): 15ee537

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -1,43 +1,40 @@
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)
 
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)