File size: 4,959 Bytes
3f7ece6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15a3d2e
 
bdc4782
 
 
 
 
 
 
 
 
 
725120b
bdc4782
 
 
725120b
bdc4782
 
 
 
 
725120b
bdc4782
 
 
 
 
725120b
bdc4782
 
725120b
bdc4782
 
 
 
3f7ece6
 
 
 
 
 
 
 
 
 
e93914c
3f7ece6
 
 
 
 
bdc4782
d8eeda5
3f7ece6
15a3d2e
 
d8eeda5
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
import gradio as gr
from transformers import pipeline
import torch
from diffusers import DiffusionPipeline
from datasets import load_dataset

headline_gen = pipeline("text2text-generation", model="Michau/t5-base-en-generate-headline", tokenizer="t5-base")
ar_to_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")
en_to_ar_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")

# Arabic: text-to-speech
synthesiser_arabic = pipeline("text-to-speech", model="facebook/mms-tts-ara")

# English: text-to-speech
synthesiser_english = pipeline("text-to-speech", model="microsoft/speecht5_tts")
embeddings_dataset_english = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
speaker_embedding_english = torch.tensor(embeddings_dataset_english[7306]["xvector"]).unsqueeze(0)

def generate_headline(selected_language, text):
    if selected_language == "Arabic":
        translated_text = translate_ar_to_en(text)  # Translate Arabic to English
        english_headline = generate_headline_english(translated_text)  # Generate headline in English
        arabic_headline = translate_en_to_ar(english_headline)  # Translate headline back to Arabic
        sampling_rate, audio_data = text_to_speech_arabic(arabic_headline)
        return arabic_headline, (sampling_rate, audio_data)

    elif selected_language == "English":
        english_headline = generate_headline_english(text)
        sampling_rate, audio_data = text_to_speech_english(english_headline)
        return english_headline, (sampling_rate, audio_data)

def translate_ar_to_en(text):
  var_ar_to_en = ar_to_en_translator(text)[0]['translation_text']
  return var_ar_to_en

def translate_en_to_ar(text):
  var_en_to_ar = en_to_ar_translator(text)[0]['translation_text']
  return var_en_to_ar

def generate_headline_english(text):
    result1 = headline_gen(text, max_length=100, truncation=True)
    result2 = result1[0]['generated_text']
    return result2

# Text-to-speech conversion for Arabic
def text_to_speech_arabic(text):
    speech = synthesiser_arabic(text)
    audio_data = speech["audio"][0]  # Flatten to 1D
    sampling_rate = speech["sampling_rate"]
    return (sampling_rate, audio_data)

# Text-to-speech conversion for English
def text_to_speech_english(text):
    speech = synthesiser_english(text, forward_params={"speaker_embeddings": speaker_embedding_english})
    audio_data = speech["audio"]
    sampling_rate = speech["sampling_rate"]
    return (sampling_rate, audio_data)

custom_css = """
body {
    background-color: #f4f4f9;
    color: #333;
}
.gradio-container {
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    background-color: #fff;
}
label {
    color: #9B59B6;  
    font-weight: bold;
}
input[type="text"], textarea {
    border: 1px solid #9B59B6;  
}
textarea {
    height: 150px;
}
button {
    background-color: #9B59B6;  
    color: #fff;
    border-radius: 5px;
    cursor: pointer;
}
button:hover {
    background-color: #8E44AD;  
}
.dropdown {
    border: 1px solid #9B59B6;  
    border-radius: 4px;
}
"""

examples = [
    #First parameter is for the dropdown menu, and the second parameter is for the starter of the poem
    ["Arabic", "تعتبر انبعاثات الغازات الدفيئة، مثل ثاني أكسيد الكربون (CO2) والميثان (CH4)، من الأسباب الرئيسية لتغير المناخ العالمي. تؤدي الأنشطة البشرية، مثل حرق الوقود الأحفوري لإنتاج الطاقة وإزالة الغابات والعمليات الصناعية، إلى زيادة كبيرة في تركيز هذه الغازات في الغلاف الجوي. وفقًا للهيئة الحكومية الدولية المعنية بتغير المناخ (IPCC)، ارتفعت مستويات ثاني أكسيد الكربون بأكثر من 50٪ منذ عصر ما قبل الصناعة، مما ساهم في ارتفاع درجات الحرارة العالمية."],
    ["English", "Greenhouse gas emissions, primarily carbon dioxide (CO2) and methane (CH4), are the main drivers of global climate change. Human activities, such as burning fossil fuels for energy, deforestation, and industrial processes, have significantly increased the concentration of these gases in the atmosphere. According to the Intergovernmental Panel on Climate Change (IPCC), CO2 levels have risen by over 50% since the pre-industrial era, contributing to rising global temperatures."]
]

interface = gr.Interface(
    fn=generate_headline,
    inputs=[
        gr.Dropdown(choices=["Arabic", "English"], label="Select Language"),
        gr.Textbox(lines=5, placeholder="Enter article text here...",label="Text/Article")
    ],
    outputs=[
        gr.Textbox(label="Generated Headline"),
        gr.Audio(label="Generated Audio", type="numpy")
    ],
    examples=examples,
    css=custom_css  
)

interface.launch()