Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain_groq import ChatGroq
|
3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
5 |
+
from langchain_core.output_parsers import StrOutputParser
|
6 |
+
import os
|
7 |
+
|
8 |
+
def get_transcript(video_id):
|
9 |
+
def transcript_to_string(transcript):
|
10 |
+
transcript_text = '\n'.join([item['text'] for item in transcript])
|
11 |
+
return transcript_text
|
12 |
+
|
13 |
+
try:
|
14 |
+
transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
|
15 |
+
|
16 |
+
for transcript in transcripts:
|
17 |
+
if transcript.is_translatable:
|
18 |
+
transcript = transcript.fetch()
|
19 |
+
return transcript_to_string(transcript)
|
20 |
+
|
21 |
+
transcript = transcripts.find_manually_created_transcript(['en', 'tr']).fetch()
|
22 |
+
return transcript_to_string(transcript)
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return "Sorry, the video cannot be transcribed."
|
26 |
+
|
27 |
+
|
28 |
+
def summarize_video(video_url, language):
|
29 |
+
video_id = video_url.split('v=')[-1].split('&')[0]
|
30 |
+
transcript = get_transcript(video_id)
|
31 |
+
|
32 |
+
model = ChatGroq(
|
33 |
+
model="llama-3.1-70b-versatile",
|
34 |
+
temperature=0,
|
35 |
+
max_tokens=None,
|
36 |
+
timeout=None,
|
37 |
+
max_retries=2,
|
38 |
+
)
|
39 |
+
|
40 |
+
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.
|
41 |
+
Transcript: {transcript}
|
42 |
+
Language: {language}"""
|
43 |
+
|
44 |
+
prompt_template = ChatPromptTemplate.from_messages(
|
45 |
+
[("system", system_template)]
|
46 |
+
)
|
47 |
+
|
48 |
+
parser = StrOutputParser()
|
49 |
+
|
50 |
+
chain = prompt_template | model | parser
|
51 |
+
|
52 |
+
response = chain.invoke({"transcript": transcript, "language": language})
|
53 |
+
return response
|
54 |
+
|
55 |
+
|
56 |
+
iface = gr.Interface(
|
57 |
+
fn=summarize_video,
|
58 |
+
inputs=[
|
59 |
+
gr.Textbox(label="YouTube Video URL"),
|
60 |
+
gr.Textbox(label="Language for Summary")
|
61 |
+
],
|
62 |
+
outputs="text",
|
63 |
+
title="YouTube Video Summarizer",
|
64 |
+
description="Enter a YouTube video URL and the desired language for the summary."
|
65 |
+
)
|
66 |
+
|
67 |
+
iface.launch()
|