Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +18 -0
- app.py +74 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
WORKDIR interExblorer/code
|
4 |
+
|
5 |
+
COPY ./requirements.txt interExblorer/code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r interExblorer/code/requirements.txt
|
8 |
+
|
9 |
+
RUN useradd -m -u 1000 user
|
10 |
+
USER user
|
11 |
+
ENV HOME=/home/user \
|
12 |
+
PATH=/home/user/.local/bin:$PATH
|
13 |
+
|
14 |
+
WORKDIR $HOME/app
|
15 |
+
|
16 |
+
COPY . .
|
17 |
+
|
18 |
+
CMD ["python", "app.py", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from flask import Flask, render_template, request
|
3 |
+
import subprocess
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import sys
|
7 |
+
from sentence_transformers import SentenceTransformer
|
8 |
+
from pinecone import Pinecone
|
9 |
+
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
|
13 |
+
PINECONE_SPACE_NAME = os.environ.get('PINECONE_SPACE_NAME')
|
14 |
+
|
15 |
+
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
16 |
+
|
17 |
+
# Initialize Pinecone client
|
18 |
+
pc = Pinecone(api_key=PINECONE_API_KEY, environment='gcp-starter')
|
19 |
+
indexE = pc.Index(PINECONE_SPACE_NAME)
|
20 |
+
|
21 |
+
@app.route('/')
|
22 |
+
def index():
|
23 |
+
return render_template('index.html')
|
24 |
+
|
25 |
+
@app.route('/search', methods=['POST'])
|
26 |
+
def search():
|
27 |
+
if request.method == 'POST':
|
28 |
+
query = request.form['query']
|
29 |
+
youtube_links = []
|
30 |
+
|
31 |
+
try:
|
32 |
+
# Encode the user-provided input
|
33 |
+
query_vector = model.encode(query)
|
34 |
+
|
35 |
+
# Convert the query vector to a list
|
36 |
+
query_vector_list = query_vector.tolist()
|
37 |
+
|
38 |
+
# Query Pinecone with the vector using the 'Index' object
|
39 |
+
results = indexE.query(
|
40 |
+
namespace='',
|
41 |
+
top_k=3,
|
42 |
+
include_values=True,
|
43 |
+
include_metadata=True,
|
44 |
+
vector=query_vector_list,
|
45 |
+
)
|
46 |
+
|
47 |
+
if results and 'matches' in results and results['matches']:
|
48 |
+
# Access metadata from each match
|
49 |
+
for match in results['matches']:
|
50 |
+
metadata = match.get('metadata', {})
|
51 |
+
video_id = metadata.get('video_id')
|
52 |
+
start_time = metadata.get('start_time')
|
53 |
+
time = int(start_time)
|
54 |
+
|
55 |
+
# Create a clickable YouTube link
|
56 |
+
youtube_link = f'https://www.youtube.com/watch?v={video_id}&t={time}s'
|
57 |
+
youtube_links.append(youtube_link)
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
# Handle exceptions gracefully
|
61 |
+
error_message = str(e)
|
62 |
+
output = {'error': error_message}
|
63 |
+
print(json.dumps(output))
|
64 |
+
|
65 |
+
else:
|
66 |
+
# No exceptions, render the template with results
|
67 |
+
if youtube_links:
|
68 |
+
return render_template('results.html', query=query, youtube_links=youtube_links)
|
69 |
+
else:
|
70 |
+
no_results_message = "No results found."
|
71 |
+
return render_template('results.html', query=query, no_results_message=no_results_message)
|
72 |
+
|
73 |
+
if __name__ == '__main__':
|
74 |
+
app.run(host='0.0.0.0', port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask==3.0.1
|
2 |
+
sentence-transformers==2.2.2
|
3 |
+
pinecone-client==3.0.0
|
4 |
+
Gunicorn
|