Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# URL To YoutubeID
|
2 |
+
from urllib.parse import urlparse
|
3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
4 |
+
from youtube_transcript_api.formatters import TextFormatter
|
5 |
+
import openai
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# μ νλΈ λΉλμ€ ID μΆμΆ ν¨μ
|
9 |
+
def get_yt_video_id(url):
|
10 |
+
from urllib.parse import urlparse, parse_qs
|
11 |
+
|
12 |
+
if url.startswith(('youtu', 'www')):
|
13 |
+
url = 'http://' + url
|
14 |
+
|
15 |
+
query = urlparse(url)
|
16 |
+
|
17 |
+
if 'youtube' in query.hostname:
|
18 |
+
if query.path == '/watch':
|
19 |
+
return parse_qs(query.query)['v'][0]
|
20 |
+
elif query.path.startswith(('/embed/', '/v/')):
|
21 |
+
return query.path.split('/')[2]
|
22 |
+
elif 'youtu.be' in query.hostname:
|
23 |
+
return query.path[1:]
|
24 |
+
else:
|
25 |
+
raise ValueError("μ ν¨ν μ νλΈ λ§ν¬κ° μλλλ€.")
|
26 |
+
|
27 |
+
# μ νλΈ μμμ μλ§μ κ°μ Έμ€λ ν¨μ
|
28 |
+
def transcribe(youtubeId):
|
29 |
+
transcription = YouTubeTranscriptApi.get_transcript(youtubeId)
|
30 |
+
return transcription
|
31 |
+
|
32 |
+
# μλ§μ ν
μ€νΈλ‘ λ³ννλ ν¨μ
|
33 |
+
formatter = TextFormatter()
|
34 |
+
|
35 |
+
def transcriptToText(transcript):
|
36 |
+
text = formatter.format_transcript(transcript)
|
37 |
+
text = text.replace("\n", " ")
|
38 |
+
return text
|
39 |
+
|
40 |
+
# ν
μ€νΈλ₯Ό μμ½νλ ν¨μ (OpenAI API μ¬μ©)
|
41 |
+
def textToSummary(text, OpenAIkey):
|
42 |
+
openai.api_key = OpenAIkey
|
43 |
+
response = openai.Completion.create(
|
44 |
+
model="text-davinci-003",
|
45 |
+
prompt="Summarize this in 200 words or less:\n\n" + text,
|
46 |
+
temperature=0.7,
|
47 |
+
max_tokens=400,
|
48 |
+
top_p=1.0,
|
49 |
+
frequency_penalty=0.0,
|
50 |
+
presence_penalty=1
|
51 |
+
)
|
52 |
+
return response["choices"][0]["text"].replace("\n", " ").strip()
|
53 |
+
|
54 |
+
# μ 체 μμ½ νλ‘μΈμ€λ₯Ό μ²λ¦¬νλ ν¨μ
|
55 |
+
def summarize(url, OpenAIkey):
|
56 |
+
try:
|
57 |
+
videoId = get_yt_video_id(url)
|
58 |
+
transcript = transcribe(videoId)
|
59 |
+
text = transcriptToText(transcript)
|
60 |
+
summary = textToSummary(text, OpenAIkey)
|
61 |
+
return summary
|
62 |
+
except Exception as e:
|
63 |
+
return f"μμ½μ μ€ν¨νμ΅λλ€: {str(e)}"
|
64 |
+
|
65 |
+
# Gradio μΈν°νμ΄μ€ μ€μ
|
66 |
+
description = "μμ½ν μ νλΈ λμμ λ§ν¬λ₯Ό μ
λ ₯νμΈμ"
|
67 |
+
|
68 |
+
gr.Interface(fn=summarize,
|
69 |
+
inputs=["text", "text"],
|
70 |
+
outputs="textbox",
|
71 |
+
description=description
|
72 |
+
).launch()
|