File size: 2,645 Bytes
b33089c 2eae17f 2b89dc1 b33089c 6b041d9 3d48fe6 2b89dc1 9ed131e 3d37ad9 2b89dc1 b33089c 2b89dc1 b33089c 2b89dc1 b33089c 989332e cc7ccf6 2b89dc1 ce63942 6b041d9 b33089c b54fbdc 6b041d9 de997fa b33089c a8bcd0b b33089c de997fa a8bcd0b 2b89dc1 b33089c 3d37ad9 b33089c a23d270 b33089c |
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 66 67 68 69 |
import os
import time
import gradio as gr
import logging
from youtube_transcript_api import YouTubeTranscriptApi
from langchain.docstore.document import Document
from langchain_groq import ChatGroq
from langchain.chains.summarize.chain import load_summarize_chain
import chatops
logger = logging.getLogger(__name__)
DEVICE = 'cpu'
DEFAULT_CHAR_LENGTH = 1000
def youtube_link_dataloader(video_link,max_video_length=1000):
video_text = ""
meta_data = {"source": f"{video_link}"}
video_id = video_link.split("watch?v=")[1].split("&")[0]
srt = YouTubeTranscriptApi.get_transcript(video_id)
for text_data in srt:
video_text = video_text + " " + text_data.get("text")
if len(video_text) > max_video_length:
video_text = video_text[0:max_video_length]
document = [Document(page_content= video_text, metadata= meta_data)]
return document
def youtube_chat(API_key=None,llm_service='mistralai/Mistral-7B-v0.1',youtube_link=None,char_length=2000):
video_document = youtube_link_dataloader(video_link=youtube_link,max_video_length=char_length)
print("docuemt:",video_document)
if llm_service== 'mistralai/Mistral-7B-v0.1':
llm = chatops.get_hugging_face_model(
model_id="mistralai/Mistral-7B-v0.1",
API_key=API_key,
temperature=0.1,
max_tokens=2048
)
elif llm_service == 'OpenAI':
llm = chatops.get_openai_chat_model(API_key=API_key)
elif llm_service == 'llama3-8b-8192':
os.environ["GROQ_API_KEY"] = API_key
llm = ChatGroq(model="llama3-8b-8192")
summarize_chain = load_summarize_chain(llm=llm, chain_type='stuff', verbose = True )
results = summarize_chain.invoke({'input_documents':video_document})
return results['output_text']
iface = gr.Interface(
fn = youtube_chat,
inputs = [
gr.Textbox(label="Add API key", type="password"),
gr.Dropdown(['mistralai/Mistral-7B-v0.1','llama3-8b-8192'],label='Large Language Model',info='LLM Service'),
gr.Textbox(label='You tube link'),
gr.Slider(DEFAULT_CHAR_LENGTH,5000,label="Video link Length in seconds",info="Length of video in seconds")
],
outputs="text",
description ="""Summarize your You tube link using Large Language Models
The Objective of the space is to use the Large Language models to generate a small Summary of the You tube Link provided.
It Facilitates to generate notes if you are using you tube for Educational purposes.
""",
)
iface.launch()
|