File size: 2,116 Bytes
b9c76ce
 
1271735
b9c76ce
1b8c8af
b9c76ce
d20fb15
b9c76ce
070bea1
b9c76ce
 
 
 
070bea1
 
 
 
eb3b875
070bea1
 
 
b9c76ce
 
070bea1
 
 
b9c76ce
 
8af25c6
070bea1
 
 
b9c76ce
 
 
070bea1
8af25c6
070bea1
 
 
 
b9c76ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb3b875
b9c76ce
 
 
eb3b875
b9c76ce
 
dd513e5
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
import whisper
from pytube import YouTube
from langchain.chat_models import ChatOpenAI
import os
import streamlit as st 

key = st.sidebar.text_input("Provide Openai API Key to Chat", type = 'password')
@st.cache_resource
def get_whisper():
    model = whisper.load_model("base")
    return model

def get_audio(url):
    yt = YouTube(url)
    video = yt.streams.filter(only_audio=True).first()
    out_file=video.download(output_path=".")
    base, ext = os.path.splitext(out_file)
    new_file = 'input_audio'+'.mp3'
    os.rename(out_file, new_file)
    a = new_file
    return a

def get_text(url):
    model = get_whisper()
    result = model.transcribe(get_audio(url))
    return result['text']

def get_youtube_title(y_url):
    yt = YouTube(y_url)
    embed_url = f"https://wwww.youtube.com/embed/{yt.video_id}"
    emded_html = f'<iframe src>"{embed_url}" frameborder="0" allowfullscreen></iframe>'
    return yt.title, emded_html

y_url = st.sidebar.text_input("Enter youtube url")
if st.sidebar.button('Get Youtube Content'):
    if st.spinner('Loading...'):
        video_title, embed_html = get_youtube_title(y_url)
        st.markdown(f'## {video_title}')
        st.markdown(embed_html, unsafe_allow_html=True)
        content = get_text(y_url)
        st.session_state.content = content

template = """You are Adance AI that respond on given instruction based on available content
content: {content}
instruction: {prompt}
"""

if 'content' in st.session_state:
    template = template.format(content = st.session_state.content, prompt = '{prompt}')
else:
    template = template.format(content = '', prompt = '{prompt}')

if prompt := st.chat_input():
    if not key:
        st.markdown('Provide key to chat')
        st.stop()
    # st.session_state.messages.append({'role': 'user', 'content': prompt})
    st.chat_message('user').write(prompt)
    llm = ChatOpenAI(model_name = 'gpt-3.5-turbo', openai_api_key = key)
    msg = llm.predict(template.format(prompt = prompt))
    # st.session_state.messages.append({'role': 'assistant', 'content': msg})
    st.chat_message('assistant').write(msg)