File size: 5,518 Bytes
8f2b388
 
 
 
23e65ea
8f2b388
1ca780b
a9bc30d
1ca780b
 
8f2b388
1ca780b
 
8f2b388
 
23e65ea
 
 
 
 
 
 
8f2b388
1ca780b
8f2b388
 
1ca780b
 
 
 
23e65ea
 
8f2b388
1ca780b
 
23e65ea
8f2b388
1ca780b
d65550f
23e65ea
 
d65550f
1ca780b
d65550f
23e65ea
 
d65550f
1ca780b
d65550f
 
 
 
 
23e65ea
 
 
 
 
 
 
 
 
 
 
 
 
d65550f
e4d6b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65550f
8f2b388
23e65ea
 
8f2b388
 
 
 
 
 
e4d6b9f
8f2b388
 
 
23e65ea
8f2b388
e4d6b9f
 
8f2b388
e4d6b9f
1ca780b
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
import gradio as gr
from transformers import pipeline
import torch
from diffusers import DiffusionPipeline
from datasets import load_dataset

#Headline Generator Pipeline
headline_gen = pipeline("text2text-generation", model="Michau/t5-base-en-generate-headline", tokenizer="t5-base")

#Arabic to English translator Pipeline
ar_to_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")

#English to Arabic translator Pipeline
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)

#main function
def generate_headline(selected_language, text):
    if selected_language == "Arabic":
        translated_text = translate_ar_to_en(text)  #Translate Arabic text to English (so the headlines pipeline will understand the input)
        english_headline = generate_headline_english(translated_text)  #Generate headline in English based on the translated text
        arabic_headline = translate_en_to_ar(english_headline)  # Translate headline to Arabic (output will be arabic)
        sampling_rate, audio_data = text_to_speech_arabic(arabic_headline) #Arabic headline will be passed to the text-to-speech pipeline (arabic)
        return arabic_headline, (sampling_rate, audio_data)

    elif selected_language == "English":
        english_headline = generate_headline_english(text) #Generate headline in English based on the text
        sampling_rate, audio_data = text_to_speech_english(english_headline) #English headline will be passed to the text-to-speech pipeline (english)
        return english_headline, (sampling_rate, audio_data)

#function to translate Arabic Text to English
def translate_ar_to_en(text):
  var_ar_to_en = ar_to_en_translator(text)[0]['translation_text']
  return var_ar_to_en

#function to translate English Headline to Arabic
def translate_en_to_ar(text):
  var_en_to_ar = en_to_ar_translator(text)[0]['translation_text']
  return var_en_to_ar

#function to generate headline in english
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()