SUHHHH commited on
Commit
161ad03
Β·
verified Β·
1 Parent(s): 847b7a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -60
app.py CHANGED
@@ -1,45 +1,9 @@
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,
@@ -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()