File size: 7,120 Bytes
e793ef5
eea784a
 
c7ac97c
 
9d68248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ac97c
 
 
 
2b22edd
 
 
 
 
 
 
 
 
 
a6cf7f8
9d68248
 
2b22edd
 
9d68248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eea784a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d68248
 
 
 
 
 
 
 
 
 
c7ac97c
9d68248
 
eea784a
9d68248
eea784a
 
 
 
 
9d68248
eea784a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ac97c
eea784a
 
 
 
 
9d68248
 
eea784a
 
 
9d68248
e793ef5
eea784a
 
 
 
 
 
 
 
e793ef5
9d68248
eea784a
9d68248
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import gradio as gr
from datetime import datetime
import random
from transformers import pipeline
from transformers.pipelines.audio_utils import ffmpeg_read
import moviepy.editor as mp
import speech_recognition as sr
import json
from nltk.tokenize import sent_tokenize

def transcribe_video(video_path):
    # Load the video file and extract audio
    video = mp.VideoFileClip(video_path)
    audio_path = "audio.wav"
    video.audio.write_audiofile(audio_path)

    # Initialize recognizer class (for recognizing the speech)
    recognizer = sr.Recognizer()

    # Use SpeechRecognition to transcribe audio
    with sr.AudioFile(audio_path) as source:
        audio_text = recognizer.record(source)
        transcript = recognizer.recognize_google(audio_text)

    # Split transcript into sentences
    sentences = sent_tokenize(transcript)
    
    # Create a list of timestamps for each sentence
    timestamps = []
    duration_per_sentence = len(audio_text.frame_data) / len(sentences) / 44100  # Approximate duration per sentence in seconds
    
    for i, sentence in enumerate(sentences):
        start_time = i * duration_per_sentence
        timestamps.append({"start": start_time, "text": sentence})

    return timestamps

def save_transcript_to_json(timestamps, json_file):
    with open(json_file, 'w') as f:
        json.dump(timestamps, f, indent=4)

# Initialize the translation pipeline
translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-{target_language}")

# Function to get the appropriate translation model based on target language
def get_translation_model(target_language):
    # Map of target languages to their corresponding model names
    model_map = {
        "es": "Helsinki-NLP/opus-mt-en-es",  # English to Spanish
        "fr": "Helsinki-NLP/opus-mt-en-fr",  # English to French
        "zh": "Helsinki-NLP/opus-mt-en-zh",  # English to Chinese
        # Add more languages as needed
    }
    return model_map.get(target_language, "Helsinki-NLP/opus-mt-en-fr")  # Default to French if not found

def translate_text(timestamps_json, target_language):
    # Load the translation model for the specified target language
    translation_model_id = get_translation_model(target_language)
    translator = pipeline("translation", model=translation_model_id)

    # Parse the input JSON
    timestamps = json.loads(timestamps_json)

    # Prepare output structure
    translated_timestamps = []

    # Translate each sentence and store it with its start time
    for entry in timestamps:
        original_text = entry["text"]
        translated_text = translator(original_text)[0]['translation_text']
        translated_timestamps.append({
            "start": entry["start"],
            "original": original_text,
            "translated": translated_text
        })

    # Return the translated timestamps as a JSON string
    return json.dumps(translated_timestamps, indent=4)

def add_transcript_to_video(video_path, timestamps, output_path):
    # Load the video file
    video = mp.VideoFileClip(video_path)

    # Create text clips based on timestamps
    text_clips = []
    
    for entry in timestamps:
        # Create a text clip for each sentence
        txt_clip = mp.TextClip(entry["text"], fontsize=24, color='white', bg_color='black', size=video.size)
        
        # Set the start time and duration for each text clip
        txt_clip = txt_clip.set_start(entry["start"]).set_duration(3).set_position(('bottom')).set_opacity(0.7)  # Display each sentence for 3 seconds
        
        # Append the text clip to the list
        text_clips.append(txt_clip)

    # Overlay all text clips on the original video
    final_video = mp.CompositeVideoClip([video] + text_clips)

    # Write the result to a file
    final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')

# Mock functions for platform actions and analytics
def mock_post_to_platform(platform, content_title):
    return f"Content '{content_title}' successfully posted on {platform}!"

def mock_analytics():
    return {
        "YouTube": {"Views": random.randint(1000, 5000), "Engagement Rate": f"{random.uniform(5, 15):.2f}%"},
        "Instagram": {"Views": random.randint(500, 3000), "Engagement Rate": f"{random.uniform(10, 20):.2f}%"},
    }

# Core functionalities
def upload_and_manage(file, platform, language):
    if file is None:
        return "Please upload a video/audio file.", None, None, None

    # Define paths for audio and output files
    audio_path = "audio.wav"
    json_file = "transcript.json"
    output_video_path = "output_video.mp4"

    # Transcribe audio from uploaded media file and get timestamps
    timestamps = transcribe_video(file.name)

    # Save transcript to JSON
    save_transcript_to_json(timestamps, json_file)

    # Add transcript to video based on timestamps
    add_transcript_to_video(file.name, timestamps, output_video_path)

    # Mock posting action (you can implement this as needed)
    post_message = mock_post_to_platform(platform, file.name)

    # Mock analytics generation
    analytics = mock_analytics()

    return post_message, timestamps, json_file, analytics

def generate_dashboard(analytics):
    if not analytics:
        return "No analytics available."

    dashboard = "Platform Analytics:\n"
    for platform, data in analytics.items():
        dashboard += f"\n{platform}:\n"
        for metric, value in data.items():
            dashboard += f"  {metric}: {value}\n"
    return dashboard

# Gradio Interface with Tabs
def build_interface():
    with gr.Blocks() as demo:
        with gr.Tab("Content Management"):
            gr.Markdown("## Integrated Content Management")
            with gr.Row():
                file_input = gr.File(label="Upload Video/Audio File")
                platform_input = gr.Dropdown(["YouTube", "Instagram"], label="Select Platform")
                language_input = gr.Dropdown(["en", "es", "fr", "zh"], label="Select Language")  # Language codes
            
            submit_button = gr.Button("Post and Process")
            
            with gr.Row():
                post_output = gr.Textbox(label="Posting Status", interactive=False)
                transcription_output = gr.Textbox(label="Transcription Timestamps (JSON)", interactive=False)
                json_output = gr.Textbox(label="Transcript JSON File", interactive=False)

            submit_button.click(upload_and_manage, 
                                inputs=[file_input, platform_input, language_input], 
                                outputs=[post_output, transcription_output, json_output, gr.State()])
        
        with gr.Tab("Analytics Dashboard"):
            gr.Markdown("## Content Performance Analytics")
            analytics_output = gr.Textbox(label="Dashboard", interactive=False)
            generate_dashboard_button = gr.Button("Generate Dashboard")

            generate_dashboard_button.click(generate_dashboard, inputs=[gr.State()], outputs=[analytics_output])

    return demo

# Launch the Gradio interface
demo = build_interface()
demo.launch()