|
| 1 | +from dotenv import dotenv_values |
| 2 | +import os |
| 3 | +from langchain.document_loaders import YoutubeLoader |
| 4 | +from langchain.chat_models import ChatOpenAI |
| 5 | +from langchain.llms import OpenAI |
| 6 | +from langchain.prompts.chat import ( |
| 7 | + ChatPromptTemplate, |
| 8 | + SystemMessagePromptTemplate, |
| 9 | + AIMessagePromptTemplate, |
| 10 | + HumanMessagePromptTemplate, |
| 11 | +) |
| 12 | + |
| 13 | +import streamlit as st |
| 14 | +from streamlit_chat import message |
| 15 | + |
| 16 | + |
| 17 | +api_keys=dotenv_values() |
| 18 | +os.environ['OPENAI_API_KEY'] = dotenv_values()['openai_api_key'] #set environment variable |
| 19 | + |
| 20 | +def summarize_video(video_url): |
| 21 | + loader = YoutubeLoader.from_youtube_url(video_url, add_video_info=False) |
| 22 | + system_prompt_template = """You are Youtube Summarizer and you specialize in making short and consice summaries for any youtube videos. Use the provided transcription to create a summary of what the video is about. |
| 23 | + """ |
| 24 | + human_prompt_template = "Provide a short and concise summary of the following transcript: {text}." |
| 25 | + system_message_prompt = SystemMessagePromptTemplate.from_template(system_prompt_template) |
| 26 | + human_message_prompt = HumanMessagePromptTemplate.from_template(human_prompt_template) |
| 27 | + # delete the gpt-4 model_name to use the default gpt-3.5 turbo for faster results |
| 28 | + gpt_4 = ChatOpenAI(temperature=.02, model_name='gpt-4') |
| 29 | + conversation = [system_message_prompt, human_message_prompt] |
| 30 | + chat_prompt = ChatPromptTemplate.from_messages(conversation) |
| 31 | + response = gpt_4(chat_prompt.format_prompt( |
| 32 | + text=loader.load()[0].page_content).to_messages()) |
| 33 | + return response |
| 34 | + |
| 35 | +st.set_page_config( |
| 36 | + page_title="Youtube TL;DR", |
| 37 | + page_icon=":robot:" |
| 38 | +) |
| 39 | + |
| 40 | +st.header("Youtube TL;DR :robot_face:") |
| 41 | + |
| 42 | +if 'ai' not in st.session_state: |
| 43 | + st.session_state['ai'] = [] |
| 44 | +if 'human' not in st.session_state: |
| 45 | + st.session_state['human'] = [] |
| 46 | + |
| 47 | +def get_text(): |
| 48 | + input_text = st.text_input('Enter Youtube video URL to get TL;DR:', key='input', placeholder='Youtube URL') |
| 49 | + return input_text |
| 50 | + |
| 51 | + |
| 52 | +user_input=get_text() |
| 53 | +if user_input: |
| 54 | + output = summarize_video(user_input).content |
| 55 | + |
| 56 | + st.session_state['human'].append(user_input) |
| 57 | + st.session_state['ai'].append(output) |
| 58 | + |
| 59 | +if st.session_state['ai']: |
| 60 | + for i in range(len(st.session_state['ai']) -1, -1, -1): |
| 61 | + message(st.session_state['ai'][i], key=str(i)) |
| 62 | + message(st.session_state['human'][i], is_user=True, key=str(i) + '_user') |
| 63 | + |
| 64 | + |
0 commit comments