Spaces:
Sleeping
Sleeping
Commit ·
1cd8de5
1
Parent(s): 308884c
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from urllib.parse import urlparse
|
| 2 |
+
def get_yt_video_id(url):
|
| 3 |
+
|
| 4 |
+
from urllib.parse import urlparse, parse_qs
|
| 5 |
+
|
| 6 |
+
if url.startswith(('youtu', 'www')):
|
| 7 |
+
url = 'http://' + url
|
| 8 |
+
|
| 9 |
+
query = urlparse(url)
|
| 10 |
+
|
| 11 |
+
if 'youtube' in query.hostname:
|
| 12 |
+
if query.path == '/watch':
|
| 13 |
+
return parse_qs(query.query)['v'][0]
|
| 14 |
+
elif query.path.startswith(('/embed/', '/v/')):
|
| 15 |
+
return query.path.split('/')[2]
|
| 16 |
+
elif 'youtu.be' in query.hostname:
|
| 17 |
+
return query.path[1:]
|
| 18 |
+
else:
|
| 19 |
+
raise ValueError
|
| 20 |
+
|
| 21 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 22 |
+
from youtube_transcript_api.formatters import TextFormatter
|
| 23 |
+
|
| 24 |
+
def transcribe(youtubeId):
|
| 25 |
+
transcription = YouTubeTranscriptApi.get_transcript(youtubeId)
|
| 26 |
+
return transcription
|
| 27 |
+
|
| 28 |
+
def transcriptToText(transcript):
|
| 29 |
+
text = formatter.format_transcript(transcript)
|
| 30 |
+
text = text.replace("\n", " ")
|
| 31 |
+
return text
|
| 32 |
+
|
| 33 |
+
import openai
|
| 34 |
+
def textToSummary(text):
|
| 35 |
+
response = openai.Completion.create(
|
| 36 |
+
model="text-davinci-003",
|
| 37 |
+
prompt= text + "\nTl;dr",
|
| 38 |
+
temperature=0.7,
|
| 39 |
+
max_tokens=200,
|
| 40 |
+
top_p=1.0,
|
| 41 |
+
frequency_penalty=0.0,
|
| 42 |
+
presence_penalty=1
|
| 43 |
+
)
|
| 44 |
+
return response["choices"][0]["text"]
|
| 45 |
+
|
| 46 |
+
def summarize(url,OpenAIkey):
|
| 47 |
+
openai.api_key = key
|
| 48 |
+
videoId = get_yt_video_id(url)
|
| 49 |
+
transcript = transcribe(videoId)
|
| 50 |
+
text = transcriptToText(transcript)
|
| 51 |
+
summary = textToSummary(text)
|
| 52 |
+
return summary
|
| 53 |
+
|
| 54 |
+
import gradio as gr
|
| 55 |
+
|
| 56 |
+
examples = [
|
| 57 |
+
"https://www.youtube.com/watch?v=wD79NZroV88",
|
| 58 |
+
"https://youtu.be/aircAruvnKk",
|
| 59 |
+
"https://www.youtube.com/watch?v=de2Hsvxaf8M&t=50s",
|
| 60 |
+
"https://www.youtube.com/watch?v=JT8f0n6E79g&ab_channel=Coffeezilla",
|
| 61 |
+
"https://www.youtube.com/watch?v=gv9cdTh8cUo"
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
description = "Enter a link for a YouTube video you want summarized"
|
| 65 |
+
|
| 66 |
+
gr.Interface(fn=summarize,
|
| 67 |
+
inputs=["text", "text"],
|
| 68 |
+
outputs=["textbox"],
|
| 69 |
+
description=description,
|
| 70 |
+
examples=examples
|
| 71 |
+
).launch(share=True)
|
| 72 |
+
|