JaMugen commited on
Commit
03efdb0
·
1 Parent(s): 3549787

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import subprocess
2
  import sys
3
 
@@ -7,53 +8,62 @@ def install_libraries():
7
  subprocess.check_call(['pip', 'install', 'youtube_transcript_api'])
8
  subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch'])
9
  subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'transformers'])
10
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'sentencepiece'])
11
  print("Libraries installed successfully!")
12
  except subprocess.CalledProcessError as e:
13
  print("Error during installation:", e)
14
 
15
- def install_and_download(url):
16
- install_libraries()
17
 
18
- import streamlit as st
19
- import pandas as pd
20
  import Milestone5API
21
-
22
- @st.experimental_memo
23
- def download_transcripts(url):
24
- return Milestone5API.download_video_transcript(url)
25
-
26
  st.title("Translate YouTube Video to French")
 
27
  url = st.text_input("Enter YouTube Video URL")
28
-
 
29
  if url:
30
- cached_data = download_transcripts(url)
31
-
32
- captions, translated_captions, _, _ = cached_data
33
-
34
  if captions and translated_captions:
35
- captions_list = []
36
- translated_text = []
37
-
38
- for line, translated_line in zip(captions, translated_captions):
39
- lang, timestamp, text = line.split(" ", 2)
40
  lang_trans, _, text_trans = translated_line.split(" ", 2)
41
  if lang.startswith("en:") or lang.startswith("fr:") and lang_trans.startswith("en:") or lang_trans.startswith("fr:"):
42
- captions_list.append({"Timestamp": timestamp[4:], "Original": text})
43
- translated_text.append(text_trans)
 
 
 
44
 
45
  start_index = st.session_state.get('start_index', 0)
46
  end_index = start_index + 6
47
 
 
48
  st.write("## Caption Table")
49
- df = pd.DataFrame(captions_list[start_index:end_index], columns=["Timestamp", "Original"])
50
- df['Translated'] = translated_text[start_index:end_index]
51
-
52
  with open("styles.css", "r") as f:
53
  css = f.read()
54
  st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
55
- st.write(df)
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
 
57
  col1, col2, col3 = st.columns([1, 8, 1])
58
  if col1.button("Previous") and start_index > 0:
59
  start_index -= 6
@@ -66,4 +76,4 @@ def install_and_download(url):
66
  st.session_state['start_index'] = start_index
67
 
68
  if __name__ == "__main__":
69
- install_and_download(None) # Call the function to start the app
 
1
+ import streamlit as st
2
  import subprocess
3
  import sys
4
 
 
8
  subprocess.check_call(['pip', 'install', 'youtube_transcript_api'])
9
  subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch'])
10
  subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'transformers'])
11
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'sentencepiece'])
12
  print("Libraries installed successfully!")
13
  except subprocess.CalledProcessError as e:
14
  print("Error during installation:", e)
15
 
16
+ install_libraries()
 
17
 
18
+ def main():
19
+ # Importing after ensuring dependencies are installed
20
  import Milestone5API
21
+
22
+ # Title
 
 
 
23
  st.title("Translate YouTube Video to French")
24
+
25
  url = st.text_input("Enter YouTube Video URL")
26
+ captions_list = [] # Initialize empty list for captions
27
+
28
  if url:
29
+ captions, translated_captions, original_file, translated_file = Milestone5API.download_video_transcript(url)
30
+
 
 
31
  if captions and translated_captions:
32
+ # Parse captions and translated captions
33
+ for original_line, translated_line in zip(captions, translated_captions):
34
+ lang, timestamp, text = original_line.split(" ", 2)
 
 
35
  lang_trans, _, text_trans = translated_line.split(" ", 2)
36
  if lang.startswith("en:") or lang.startswith("fr:") and lang_trans.startswith("en:") or lang_trans.startswith("fr:"):
37
+ captions_list.append({
38
+ "Timestamp": timestamp[4:],
39
+ "Original": text,
40
+ "Translated": text_trans
41
+ })
42
 
43
  start_index = st.session_state.get('start_index', 0)
44
  end_index = start_index + 6
45
 
46
+ # Display caption table with new columns
47
  st.write("## Caption Table")
 
 
 
48
  with open("styles.css", "r") as f:
49
  css = f.read()
50
  st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
51
+ st.write(
52
+ "<table><tr><th class='timestamp'>Timestamp</th><th class='original'>Original</th><th class='translated'>Translated</th></tr>",
53
+ unsafe_allow_html=True,
54
+ )
55
+
56
+ for i in range(start_index, min(end_index, len(captions_list))):
57
+ st.write(
58
+ f"<tr><td class='timestamp'>{captions_list[i]['Timestamp']}</td>"
59
+ f"<td class='original'>{captions_list[i]['Original']}</td>"
60
+ f"<td class='translated'>{captions_list[i]['Translated']}</td></tr>",
61
+ unsafe_allow_html=True,
62
+ )
63
+
64
+ st.write("</table>", unsafe_allow_html=True)
65
 
66
+ # Navigation buttons
67
  col1, col2, col3 = st.columns([1, 8, 1])
68
  if col1.button("Previous") and start_index > 0:
69
  start_index -= 6
 
76
  st.session_state['start_index'] = start_index
77
 
78
  if __name__ == "__main__":
79
+ main()