beweinreich commited on
Commit
6b96454
1 Parent(s): 8243c17

switch to fastapi

Browse files
Files changed (2) hide show
  1. app.py +22 -20
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,38 +1,40 @@
1
- from flask import Flask, request, jsonify
 
2
  from audio_analyzer import AudioAnalyzer
3
  from video_analyzer import VideoAnalyzer
4
 
5
- app = Flask(__name__)
6
 
7
- @app.route('/', methods=['GET'])
8
- def hello_world():
9
- return jsonify({"message": "Hello, World!"})
10
 
11
- @app.route('/v1/analyze_audio', methods=['POST'])
12
- def analyze_audio():
13
- data = request.get_json()
14
- audio_url = data.get('audio_url')
 
 
 
 
 
 
15
 
16
  if not audio_url:
17
- return jsonify({"error": "audio_url is required"}), 400
18
 
19
  analyzer = AudioAnalyzer(media_url=audio_url, media_type="audio")
20
  traits = analyzer.retrieve_traits()
21
 
22
- return jsonify(traits)
23
 
24
- @app.route('/v1/analyze_video', methods=['POST'])
25
- def analyze_video():
26
- data = request.get_json()
27
- video_url = data.get('video_url')
28
 
29
  if not video_url:
30
- return jsonify({"error": "video_url is required"}), 400
31
 
32
  analyzer = VideoAnalyzer(video_url=video_url)
33
  traits = analyzer.retrieve_traits()
34
 
35
- return jsonify(traits)
36
-
37
- if __name__ == '__main__':
38
- app.run(debug=True)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  from audio_analyzer import AudioAnalyzer
4
  from video_analyzer import VideoAnalyzer
5
 
6
+ app = FastAPI()
7
 
8
+ class AudioRequest(BaseModel):
9
+ audio_url: str
 
10
 
11
+ class VideoRequest(BaseModel):
12
+ video_url: str
13
+
14
+ @app.get("/")
15
+ async def hello_world():
16
+ return {"message": "Hello, World!"}
17
+
18
+ @app.post("/v1/analyze_audio")
19
+ async def analyze_audio(request: AudioRequest):
20
+ audio_url = request.audio_url
21
 
22
  if not audio_url:
23
+ raise HTTPException(status_code=400, detail="audio_url is required")
24
 
25
  analyzer = AudioAnalyzer(media_url=audio_url, media_type="audio")
26
  traits = analyzer.retrieve_traits()
27
 
28
+ return traits
29
 
30
+ @app.post("/v1/analyze_video")
31
+ async def analyze_video(request: VideoRequest):
32
+ video_url = request.video_url
 
33
 
34
  if not video_url:
35
+ raise HTTPException(status_code=400, detail="video_url is required")
36
 
37
  analyzer = VideoAnalyzer(video_url=video_url)
38
  traits = analyzer.retrieve_traits()
39
 
40
+ return traits
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- flask==3.0.3
2
  moviepy==1.0.3
3
  transformers==4.42.4
4
  openai==1.34.0
 
1
+ fastapi==0.111.1
2
  moviepy==1.0.3
3
  transformers==4.42.4
4
  openai==1.34.0