Spaces:
Sleeping
Sleeping
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 | |
import chatops | |
logger = logging.getLogger(__name__) | |
DEVICE = 'cpu' | |
MAX_NEW_TOKENS = 4096 | |
DEFAULT_TEMPERATURE = 0.1 | |
DEFAULT_MAX_NEW_TOKENS = 2048 | |
MAX_INPUT_TOKEN_LENGTH = 4000 | |
DEFAULT_CHAR_LENGTH = 1000 | |
EXAMPLES = ["https://www.youtube.com/watch?v=aircAruvnKk&ab_channel=3Blue1Brown", | |
"https://www.youtube.com/watch?v=Ilg3gGewQ5U", | |
"https://www.youtube.com/watch?v=WUvTyaaNkzM" | |
] | |
llm = None | |
def clear_chat(): | |
return [] | |
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(temperature=0.1,max_tokens=1096,API_key=None,llm_service='mistralai/Mistral-7B-v0.1',youtube_link=None,char_length=2000): | |
document = youtube_link_dataloader(link=youtube_link,char_length=char_length) | |
print("docuemt:",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=temperature, | |
max_tokens=max_tokens | |
) | |
elif llm_service == 'OpenAI': | |
llm = chatops.get_openai_chat_model(API_key=API_key) | |
elif llm_service == 'llama': | |
os.environ["GROQ_API_KEY"] = API_key | |
llm = ChatGroq(model="llama3-8b-8192") | |
return "Youtube link Processing completed ..." | |
iface = gr.Interface( | |
fn = youtube_chat, | |
inputs = [ | |
gr.Slider(0.01, 0.1, value=0.01, step=0.01 , label="temperature", info="Choose between 0.01 to 0.1"), | |
gr.Slider(512,MAX_INPUT_TOKEN_LENGTH,value=1024,step=512,label="max new tokens",info='Max new tokens'), | |
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(1000,5000,label="Video link Length in seconds",info="Length of video in seconds") | |
], | |
outputs="text", | |
description ="Summarize your You tube link ", | |
) | |
iface.launch() | |