the-confused-coder commited on
Commit
27a75c7
·
verified ·
1 Parent(s): 9a4705d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st, os
2
+ import tempfile
3
+ from pytube import YouTube
4
+ import whisper
5
+ from dotenv import load_dotenv
6
+ # langchain imports
7
+ from langchain_community.document_loaders import TextLoader
8
+ from langchain_community.vectorstores import FAISS
9
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
10
+ from langchain_openai import OpenAIEmbeddings
11
+ from langchain_openai import OpenAI
12
+ from langchain_core.prompts import ChatPromptTemplate
13
+
14
+
15
+ # function for getting transcribing YouTube video
16
+ def get_yt_trans(url):
17
+ if not os.path.exists("transcription.txt"):
18
+ yt_obj = YouTube(url)
19
+ st.write(f"Youtube object: {yt_obj} \n\n\n")
20
+ video_audio = yt_obj.streams.filter(only_audio=True).first()
21
+ st.write(f"Audio object: {video_audio} \n\n\n")
22
+ # define model for audio to text conversion
23
+ audio_to_text_model = whisper.load_model('base')
24
+ st.write(f"Model object: {audio_to_text_model} \n\n\n")
25
+ # store transcription in local file
26
+ with tempfile.TemporaryDirectory() as tmpdir:
27
+ audio_file = video_audio.download(output_path=tmpdir)
28
+ st.write(f"Audio file: {audio_file} \n\n\n")
29
+ transcription = audio_to_text_model.transcribe(audio_file, fp16=False)["text"].strip()
30
+ st.write(f"Transcription: {transcription} \n\n\n")
31
+ with open("transcription.txt", "w") as file:
32
+ file.write(transcription)
33
+
34
+
35
+ # define api keys
36
+ load_dotenv()
37
+ os.environ['OPEN_API_KEY'] = os.getenv('OPENAI_API_KEY')
38
+
39
+ # define LLM
40
+ llm = OpenAI(temperature=0)
41
+
42
+ # Set page config
43
+ st.set_page_config(
44
+ page_title="YouTube Talks",
45
+ page_icon='📽️'
46
+ )
47
+ st.header('Query YouTube Videos!')
48
+ video_url = st.text_input("Enter video URL")
49
+ submit_button = st.button("Submit")
50
+
51
+
52
+ # on button press
53
+ if submit_button:
54
+ get_yt_trans(video_url)
55
+
56
+