-
Notifications
You must be signed in to change notification settings - Fork 29
/
app.py
98 lines (79 loc) · 2.26 KB
/
app.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
from dotenv import load_dotenv
from flask import Flask, request
from flask_cors import CORS
from videodb import connect, SearchError
load_dotenv()
# Flask config
app = Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY")
app.url_map.strict_slashes = False
CORS(app)
def get_connection():
conn = connect()
return conn
@app.route("/")
def hello():
return "StreamRAG: Your Go-To Video Search Agent"
@app.route("/videos", methods=["GET"])
def list_videos():
"""
Get a list of all videos in the database of your default collection.
"""
conn = get_connection()
all_videos = conn.get_collection().get_videos()
all_videos = [
{
"id": vid.id,
"title": vid.name,
"url": vid.stream_url,
"length": round(float(vid.length)),
}
for vid in all_videos
]
response = {"videos": all_videos}
return response
@app.route("/video/<id>", methods=["GET"])
def get_video(id):
"""
Get a single video by id from default collection
"""
conn = get_connection()
all_videos = conn.get_collection().get_videos()
vid = next(vid for vid in all_videos if vid.id == id)
print("vid", vid)
vid.get_transcript()
transcript_text = vid.transcript_text
response = {
"video": {
"id": vid.id,
"title": vid.name,
"url": vid.stream_url,
"length": round(float(vid.length)),
"transcript": transcript_text,
}
}
return response
@app.route("/search", methods=["POST"])
def search_videos():
"""
Search across videos in the database in default collection
"""
data = request.get_json()
query = data.get("query")
conn = get_connection()
try:
coll = conn.get_collection()
search_results = coll.search(query)
search_results.compile()
compilation_vid = search_results.player_url
except SearchError:
return "No Search Results found", 404
shots = [
{"text": shot.text, "video": shot.stream_url}
for shot in search_results.get_shots()
]
response = {"compilationVideo": compilation_vid, "chunks": shots}
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)