Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import gradio as gr
|
6 |
+
from pydub import AudioSegment
|
7 |
+
import google.generativeai as genai
|
8 |
+
from google.colab import userdata
|
9 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
10 |
+
|
11 |
+
try:
|
12 |
+
import openai
|
13 |
+
except ImportError:
|
14 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "openai"])
|
15 |
+
import openai # Import the library after installing it
|
16 |
+
|
17 |
+
def DownloadScript(youtube_url, key):
|
18 |
+
video_id = youtube_url.split("v=")[-1]
|
19 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
20 |
+
full_text = " ".join([entry['text'] for entry in transcript])
|
21 |
+
return full_text
|
22 |
+
|
23 |
+
def GenQuiz(text, key):
|
24 |
+
prompt = "請依附內容,為我出五題選擇題"+text
|
25 |
+
openai.api_key = key
|
26 |
+
completion = openai.chat.completions.create(
|
27 |
+
model="gpt-4o",
|
28 |
+
messages=[{"role": "user", "content": prompt}]
|
29 |
+
)
|
30 |
+
return completion.choices[0].message.content
|
31 |
+
|
32 |
+
def setup_gradio_interface():
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown(
|
35 |
+
"""
|
36 |
+
<h1 style="text-align: center; font-size: 36px; color: #333;">YouTube 字幕考題生成器</h1>
|
37 |
+
"""
|
38 |
+
)
|
39 |
+
with gr.Tab("字幕考題生成"):
|
40 |
+
api_key_input = gr.Textbox(label="第一步:請輸入OpenAI API金鑰", placeholder="OpenAI API Key")
|
41 |
+
youtube_input = gr.Textbox(
|
42 |
+
label="第二步:請輸入 YouTube 影片連結",
|
43 |
+
placeholder="https://www.youtube.com/watch?v=example"
|
44 |
+
)
|
45 |
+
submit_button = gr.Button("載入字幕")
|
46 |
+
|
47 |
+
txt_cc_output = gr.Textbox(
|
48 |
+
label="第三步:下載字幕",
|
49 |
+
placeholder="字幕將顯示在此處",
|
50 |
+
interactive=False
|
51 |
+
)
|
52 |
+
|
53 |
+
submit2_button = gr.Button("考題生成")
|
54 |
+
|
55 |
+
txt_quiz_output = gr.Textbox(
|
56 |
+
label="第四步:考題生成",
|
57 |
+
placeholder="考題將顯示在此處",
|
58 |
+
interactive=False
|
59 |
+
)
|
60 |
+
|
61 |
+
# 連接按鈕到功能
|
62 |
+
submit_button.click(
|
63 |
+
DownloadScript,
|
64 |
+
inputs=[youtube_input, api_key_input],
|
65 |
+
outputs=[txt_cc_output]
|
66 |
+
)
|
67 |
+
|
68 |
+
submit2_button.click(
|
69 |
+
GenQuiz,
|
70 |
+
inputs=[txt_cc_output, api_key_input],
|
71 |
+
outputs=[txt_quiz_output]
|
72 |
+
)
|
73 |
+
|
74 |
+
return demo
|
75 |
+
|
76 |
+
# 執行界面
|
77 |
+
if __name__ == "__main__":
|
78 |
+
demo = setup_gradio_interface()
|
79 |
+
demo.launch()
|