Bilalst's picture
Update app.py
057e55c
import gradio as gr
import requests
from sentence_transformers import SentenceTransformer
from youtube_transcript_api import YouTubeTranscriptApi
import numpy as np
import huggingface_hub
import os
import faiss
# Set up SentenceTransformer
model = SentenceTransformer('all-mpnet-base-v2')
playlist_id = 'PLD4EAA8F8C9148A1B'
api_key = 'AIzaSyBGuTvXcnliEh6yhTxugrAVM5YzcG9qr9U'
# Make a request to the YouTube Data API to retrieve the playlist items
url = f'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={playlist_id}&key={api_key}'
video_ids = []
while True:
response = requests.get(url)
data = response.json()
# Extract the video IDs from the response
for item in data['items']:
video_ids.append(item['snippet']['resourceId']['videoId'])
# Check if there are more pages of results
if 'nextPageToken' in data:
next_page_token = data['nextPageToken']
url = f'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={playlist_id}&key={api_key}&pageToken={next_page_token}'
else:
break
# Empty lists to store transcripts and video IDs
transcripts = []
ids = []
for video_id in video_ids:
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
transcript_text = ' '.join([t['text'] for t in transcript])
transcripts.append(transcript_text)
ids.append(video_id)
except Exception as e:
print(f"Error retrieving transcript for video {video_id}: {e}")
continue
# create sentence embeddings
sentence_embeddings = model.encode(transcripts)
# Set up FAISS
index = faiss.IndexFlatL2(768) # Create an index with L2 distance
# Convert list of embeddings to NumPy array
sentence_embeddings = np.array(sentence_embeddings)
# Add sentence embeddings to FAISS index
index.add(sentence_embeddings)
#---------------------------------------------
# Pause message from Dr. Joe's team
pause_message = "This app has been paused upon a request from Dr. Joe's team because they are working on implementing semantic search for testimonials. We appreciate your understanding and patience."
# Create a function that returns the pause message
def pause_message_fn(Type_your_search):
return pause_message
# Create Gradio interface with the pause message
iface = gr.Interface(fn=pause_message_fn, inputs="text", outputs="text", title="Dr. Joe Dispenza Testimonials Search.\n\nThis app has been paused upon a request from Dr. Joe's team because they are working on implementing semantic search for testimonials. We appreciate your understanding and patience.")
iface.launch()