File size: 4,924 Bytes
e793ef5
eea784a
 
c7ac97c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6cf7f8
 
 
 
 
c7ac97c
 
 
 
 
 
 
eea784a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ac97c
 
 
 
 
eea784a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ac97c
eea784a
 
 
 
 
 
 
 
 
 
 
e793ef5
eea784a
 
 
 
 
 
 
 
e793ef5
eea784a
 
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
import gradio as gr
from datetime import datetime
import random
from transformers import pipeline
from transformers.pipelines.audio_utils import ffmpeg_read

# Initialize the Whisper pipeline
whisper_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-medium")

def transcribe_audio_from_file(file_path):
    """
    Transcribes audio from a local file using the Whisper pipeline.

    Args:
        file_path (str): Path to the local media file.

    Returns:
        str: Transcription text if successful, otherwise None.
    """
    try:
        # Transcribe the audio using Whisper
        transcription = whisper_pipeline(file_path, return_timestamps=True)
        logger.debug(f"Transcription: {transcription['text']}")
        return transcription["text"]
    except Exception as e:
        logger.error(f"An error occurred during transcription: {e}")
        return None

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

def translate_text(text, target_language):
    """
    Translates the given text into the specified target language.

    Args:
        text (str): The text to translate.
        target_language (str): The target language code (e.g., 'es' for Spanish, 'fr' for French).

    Returns:
        str: Translated text if successful, otherwise None.
    """
    try:
        # Dynamically construct the model ID
        model_id = f"Helsinki-NLP/opus-mt-en-{target_language}"

        # Initialize the translation pipeline for the target language
        translator = pipeline("translation", model=model_id)
        translation = translator(text, max_length=1000)
        translated_text = translation[0]["translation_text"]
        logger.debug(f"Translation to {target_language}: {translated_text}")
        return translated_text
    except Exception as e:
        logger.error(f"An error occurred during translation: {e}")
        return None

# 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

    # Transcribe audio from uploaded media file
    transcription = transcribe_audio_from_media_file(file.name)

    # Translate transcription to the selected language
    translation = translate_text(transcription, language)

    # Mock posting action
    post_message = mock_post_to_platform(platform, file.name)

    # Mock analytics generation
    analytics = mock_analytics()

    return post_message, transcription, translation, 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", interactive=False)
                translation_output = gr.Textbox(label="Translation", interactive=False)

            submit_button.click(upload_and_manage, 
                                inputs=[file_input, platform_input, language_input], 
                                outputs=[post_output, transcription_output, translation_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

demo = build_interface()
demo.launch()