Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pinecone
|
| 2 |
+
import os
|
| 3 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 4 |
+
|
| 5 |
+
from flask import Flask, jsonify, render_template, request
|
| 6 |
+
import time
|
| 7 |
+
import ffmpeg
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
from flask_cors import CORS
|
| 11 |
+
API_KEY = os.environ['PINEAPI']
|
| 12 |
+
print("API_KEY",API_KEY)
|
| 13 |
+
INDEX_NAME = "videoindex250"
|
| 14 |
+
|
| 15 |
+
clip_processor = CLIPProcessor.from_pretrained(
|
| 16 |
+
"openai/clip-vit-base-patch32"
|
| 17 |
+
)
|
| 18 |
+
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
app = Flask(__name__,static_folder='static', static_url_path='/data')
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# r'/*' 是通配符,让本服务器所有的 URL 都允许跨域请求
|
| 25 |
+
CORS(app, resources=r'/*')
|
| 26 |
+
|
| 27 |
+
pinecone.init(api_key=API_KEY, environment="us-west1-gcp-free")
|
| 28 |
+
pinecode_index = pinecone.Index(index_name=INDEX_NAME)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@app.route('/')
|
| 34 |
+
def index():
|
| 35 |
+
return render_template('index.html')
|
| 36 |
+
|
| 37 |
+
@app.route('/api/search')
|
| 38 |
+
def search():
|
| 39 |
+
query = request.args.get('q')
|
| 40 |
+
embeddings = text_embeddings(query)
|
| 41 |
+
|
| 42 |
+
search_response = pinecode_index.query(
|
| 43 |
+
vector=embeddings.cpu().detach().numpy().tolist(),
|
| 44 |
+
include_metadata=True,
|
| 45 |
+
top_k=3,
|
| 46 |
+
)
|
| 47 |
+
movie_value = search_response["matches"]
|
| 48 |
+
movie_array = []
|
| 49 |
+
|
| 50 |
+
for var in movie_value:
|
| 51 |
+
var_temp = var.to_dict()
|
| 52 |
+
print("var_temp is ",var_temp)
|
| 53 |
+
|
| 54 |
+
start = var_temp["metadata"]["start"]
|
| 55 |
+
print("start is ",start)
|
| 56 |
+
start_time = time.strftime('%H:%M:%S', time.gmtime(start))
|
| 57 |
+
print("start_time is ",start_time)
|
| 58 |
+
|
| 59 |
+
end = var_temp["metadata"]["end"]
|
| 60 |
+
print("end is ",end)
|
| 61 |
+
end_time = time.strftime('%H:%M:%S', time.gmtime(end))
|
| 62 |
+
print("end_time is ",end_time)
|
| 63 |
+
input_file = "\"" + "./static/" + var_temp["metadata"]["index_id"] + ".mp4" + "\""
|
| 64 |
+
out_file = "\"" + "./static/" + var_temp["id"] + ".mp4" + "\""
|
| 65 |
+
|
| 66 |
+
if os.path.exists("./static/" + var_temp["id"] + ".mp4"):
|
| 67 |
+
cmd = f"ffmpeg -i {input_file} -ss {start_time} -to {end_time} -c copy -f {out_file}"
|
| 68 |
+
else:
|
| 69 |
+
cmd = f"ffmpeg -i {input_file} -ss {start_time} -to {end_time} -c copy {out_file}"
|
| 70 |
+
print("cmd is ",cmd)
|
| 71 |
+
os.system(cmd)
|
| 72 |
+
print("search_response is ",search_response.to_dict())
|
| 73 |
+
video_out = "http://video-indexer-app.alphatest.io:5000//data/" + var_temp["id"] + ".mp4"
|
| 74 |
+
movie_array.append(video_out)
|
| 75 |
+
|
| 76 |
+
print("movie_array is ",movie_array)
|
| 77 |
+
|
| 78 |
+
search_response_dict = search_response.to_dict()
|
| 79 |
+
search_response_dict["index_video"] = movie_array
|
| 80 |
+
print(search_response_dict)
|
| 81 |
+
return jsonify(search_response_dict)
|
| 82 |
+
|
| 83 |
+
@app.route('/api/similarity')
|
| 84 |
+
def similarity():
|
| 85 |
+
id = request.args.get('id')
|
| 86 |
+
stored_vector = pinecode_index.fetch(ids=[id])
|
| 87 |
+
|
| 88 |
+
search_response = pinecode_index.query(
|
| 89 |
+
vector=stored_vector.to_dict()['vectors'][id]['values'],
|
| 90 |
+
top_k=10,
|
| 91 |
+
include_metadata=True,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
return jsonify(search_response.to_dict())
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def text_embeddings(text: str):
|
| 100 |
+
inputs = clip_processor(text=text, return_tensors="pt", padding=True)
|
| 101 |
+
text_embeddings = clip_model.get_text_features(**inputs)
|
| 102 |
+
|
| 103 |
+
return text_embeddings
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
app.run(port=8080)
|
| 108 |
+
|
| 109 |
+
|