Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,9 @@
|
|
1 |
-
|
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 |
-
#
|
9 |
-
def
|
10 |
-
|
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,
|
@@ -50,23 +14,3 @@ def textToSummary(text, OpenAIkey):
|
|
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()
|
|
|
1 |
+
import os
|
|
|
|
|
|
|
2 |
import openai
|
|
|
3 |
|
4 |
+
# νκ²½ λ³μμμ OpenAI API ν€λ₯Ό κ°μ Έμ€κΈ°
|
5 |
+
def textToSummary(text):
|
6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # νκ²½ λ³μμμ κ°μ Έμ€κΈ°
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
response = openai.Completion.create(
|
8 |
model="text-davinci-003",
|
9 |
prompt="Summarize this in 200 words or less:\n\n" + text,
|
|
|
14 |
presence_penalty=1
|
15 |
)
|
16 |
return response["choices"][0]["text"].replace("\n", " ").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|