qqwjq1981 commited on
Commit
c7ac97c
·
verified ·
1 Parent(s): eea784a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -4
app.py CHANGED
@@ -1,6 +1,55 @@
1
  import gradio as gr
2
  from datetime import datetime
3
  import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Mock functions for platform actions and analytics
6
  def mock_post_to_platform(platform, content_title):
@@ -17,9 +66,11 @@ def upload_and_manage(file, platform, language):
17
  if file is None:
18
  return "Please upload a video/audio file.", None, None, None
19
 
20
- # Mock transcription and translation process
21
- transcription = "This is a sample transcription of the uploaded content."
22
- translation = f"[{language}] This is a sample translation of the content."
 
 
23
 
24
  # Mock posting action
25
  post_message = mock_post_to_platform(platform, file.name)
@@ -48,7 +99,7 @@ def build_interface():
48
  with gr.Row():
49
  file_input = gr.File(label="Upload Video/Audio File")
50
  platform_input = gr.Dropdown(["YouTube", "Instagram"], label="Select Platform")
51
- language_input = gr.Dropdown(["English", "Spanish", "French", "Chinese"], label="Select Language")
52
 
53
  submit_button = gr.Button("Post and Process")
54
 
 
1
  import gradio as gr
2
  from datetime import datetime
3
  import random
4
+ from transformers import pipeline
5
+ from transformers.pipelines.audio_utils import ffmpeg_read
6
+
7
+ # Initialize the Whisper pipeline
8
+ whisper_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
9
+
10
+ def transcribe_audio_from_file(file_path):
11
+ """
12
+ Transcribes audio from a local file using the Whisper pipeline.
13
+
14
+ Args:
15
+ file_path (str): Path to the local media file.
16
+
17
+ Returns:
18
+ str: Transcription text if successful, otherwise None.
19
+ """
20
+ try:
21
+ # Transcribe the audio using Whisper
22
+ transcription = whisper_pipeline(file_path, return_timestamps=True)
23
+ logger.debug(f"Transcription: {transcription['text']}")
24
+ return transcription["text"]
25
+ except Exception as e:
26
+ logger.error(f"An error occurred during transcription: {e}")
27
+ return None
28
+
29
+ # Initialize the translation pipeline
30
+ translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-{target_language}")
31
+
32
+ def translate_text(text, target_language):
33
+ """
34
+ Translates the given text into the specified target language.
35
+
36
+ Args:
37
+ text (str): The text to translate.
38
+ target_language (str): The target language code (e.g., 'es' for Spanish, 'fr' for French).
39
+
40
+ Returns:
41
+ str: Translated text if successful, otherwise None.
42
+ """
43
+ try:
44
+ # Instantiate the pipeline for the target language dynamically
45
+ translator = pipeline("translation", model=f"Helsinki-NLP/opus-mt-en-{target_language}")
46
+ translation = translator(text, max_length=1000)
47
+ translated_text = translation[0]["translation_text"]
48
+ logger.debug(f"Translation to {target_language}: {translated_text}")
49
+ return translated_text
50
+ except Exception as e:
51
+ logger.error(f"An error occurred during translation: {e}")
52
+ return None
53
 
54
  # Mock functions for platform actions and analytics
55
  def mock_post_to_platform(platform, content_title):
 
66
  if file is None:
67
  return "Please upload a video/audio file.", None, None, None
68
 
69
+ # Transcribe audio from uploaded media file
70
+ transcription = transcribe_audio_from_media_file(file.name)
71
+
72
+ # Translate transcription to the selected language
73
+ translation = translate_text(transcription, language)
74
 
75
  # Mock posting action
76
  post_message = mock_post_to_platform(platform, file.name)
 
99
  with gr.Row():
100
  file_input = gr.File(label="Upload Video/Audio File")
101
  platform_input = gr.Dropdown(["YouTube", "Instagram"], label="Select Platform")
102
+ language_input = gr.Dropdown(["en", "es", "fr", "zh"], label="Select Language") # Language codes
103
 
104
  submit_button = gr.Button("Post and Process")
105