dini15 commited on
Commit
a1a1ecb
·
verified ·
1 Parent(s): f9d6eb0

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +108 -0
  2. config.json +1 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #1. Import libraries
2
+ import requests
3
+ import json
4
+ import yt_dlp
5
+ import time # Delay ditambahkan agar tidak terlalu cepat request ke API
6
+
7
+ #2. Load API KEY
8
+ def load_api_key():
9
+ try:
10
+ with open("config.json", "r") as f:
11
+ config = json.load(f)
12
+ return config.get("API_KEY", None)
13
+ except FileNotFoundError:
14
+ print("Error: config.json tidak ditemukan! Harap tambahkan API_KEY.")
15
+ exit()
16
+ except json.JSONDecodeError:
17
+ print("Error: config.json tidak valid!")
18
+ exit()
19
+
20
+ API_KEY = load_api_key()
21
+ if not API_KEY:
22
+ print("Error: API_KEY tidak ditemukan dalam config.json")
23
+ exit()
24
+
25
+ YOUTUBE_URL = "https://www.youtube.com/watch?v=_xIwjmCH6D4"
26
+
27
+ #3. Ambil audio dari URL YouTube
28
+ def get_audio_url(youtube_url):
29
+ ydl_opts = {
30
+ "format": "bestaudio/best", # Download hanya audio
31
+ "postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}], # Convert ke MP3
32
+ "noplaylist": True, # Hanya download satu video
33
+ "quiet": True, # Hilangkan log yang tidak perlu
34
+ "threads": 4 # Gunakan multi-threading
35
+ }
36
+
37
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
38
+ try:
39
+ info_dict = ydl.extract_info(youtube_url, download=False)
40
+ return info_dict.get('url', None)
41
+ except Exception as e:
42
+ print(f"Error: Gagal mengambil audio dari YouTube. {e}")
43
+ return None
44
+
45
+ #4. Upload audio ke AssemblyAI
46
+ def upload_audio(audio_url):
47
+ if not audio_url:
48
+ print("Error: Tidak ada audio yang bisa diupload.")
49
+ return None
50
+
51
+ headers = {"authorization": API_KEY, "content-type": "application/json"}
52
+ try:
53
+ response = requests.post(
54
+ "https://api.assemblyai.com/v2/transcript",
55
+ json={"audio_url": audio_url},
56
+ headers=headers
57
+ )
58
+ response_json = response.json()
59
+ return response_json.get('id', None)
60
+ except requests.RequestException as e:
61
+ print(f"Error: Gagal upload audio ke AssemblyAI. {e}")
62
+ return None
63
+
64
+ #5. Ambil hasil transkripsi
65
+ def get_transcription(transcript_id):
66
+ if not transcript_id:
67
+ return "Error: Tidak ada ID transkripsi."
68
+
69
+ headers = {'authorization': API_KEY}
70
+ url = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"
71
+
72
+ while True:
73
+ try:
74
+ response = requests.get(url, headers=headers)
75
+ response_json = response.json()
76
+ status = response_json.get('status', None)
77
+
78
+ if status == 'completed':
79
+ return response_json.get('text', "Transkripsi tidak ditemukan.")
80
+ elif status == 'failed':
81
+ return "Transcription failed!"
82
+ else:
83
+ print("Menunggu transkripsi selesai...")
84
+ time.sleep(5) # Tambahkan delay agar tidak spam request
85
+
86
+ except requests.RequestException as e:
87
+ print(f"Error: Gagal mengambil transkripsi. {e}")
88
+ return None
89
+
90
+ #6. Jalankan semua proses
91
+ print("\n=== Memproses Video ===")
92
+ audio_url = get_audio_url(YOUTUBE_URL)
93
+ if not audio_url:
94
+ print("Gagal mengambil audio dari video.")
95
+ exit()
96
+
97
+ print("Mengupload audio ke AssemblyAI...")
98
+ transcript_id = upload_audio(audio_url)
99
+ if not transcript_id:
100
+ print("Gagal upload audio ke AssemblyAI.")
101
+ exit()
102
+
103
+ print("Menunggu hasil transkripsi...")
104
+ transcription = get_transcription(transcript_id)
105
+
106
+ #7. Print hasil transkripsi
107
+ print("\n=== Transkripsi Video ===\n")
108
+ print(transcription)
config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"API_KEY": "3a5c38e449bf4d238390a5c3e1bbb5ae"}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ yt-dlp
3
+ assemblyai