mdeniz1's picture
Add application file
95aa78d
raw
history blame
2.09 kB
import gradio as gr
from langchain_groq import ChatGroq
from youtube_transcript_api import YouTubeTranscriptApi
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os
def get_transcript(video_id):
def transcript_to_string(transcript):
transcript_text = '\n'.join([item['text'] for item in transcript])
return transcript_text
try:
transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
for transcript in transcripts:
if transcript.is_translatable:
transcript = transcript.fetch()
return transcript_to_string(transcript)
transcript = transcripts.find_manually_created_transcript(['en', 'tr']).fetch()
return transcript_to_string(transcript)
except Exception as e:
return "Sorry, the video cannot be transcribed."
def summarize_video(video_url, language):
video_id = video_url.split('v=')[-1].split('&')[0]
transcript = get_transcript(video_id)
model = ChatGroq(
model="llama-3.1-70b-versatile",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
system_template = """Below you will see a text. Read the text. First provide the major points that they discuss and then provide summaries for each major point. Your response should be in the language specified below.
Transcript: {transcript}
Language: {language}"""
prompt_template = ChatPromptTemplate.from_messages(
[("system", system_template)]
)
parser = StrOutputParser()
chain = prompt_template | model | parser
response = chain.invoke({"transcript": transcript, "language": language})
return response
iface = gr.Interface(
fn=summarize_video,
inputs=[
gr.Textbox(label="YouTube Video URL"),
gr.Textbox(label="Language for Summary")
],
outputs="text",
title="YouTube Video Summarizer",
description="Enter a YouTube video URL and the desired language for the summary."
)
iface.launch()