File size: 4,313 Bytes
8f2b388
 
 
 
23e65ea
8f2b388
 
 
 
 
23e65ea
 
 
 
 
 
 
8f2b388
 
 
d65550f
 
 
23e65ea
 
 
8f2b388
d65550f
23e65ea
 
8f2b388
d65550f
23e65ea
 
d65550f
 
23e65ea
 
d65550f
 
 
 
 
 
23e65ea
 
 
 
 
 
 
 
 
 
 
 
 
d65550f
 
8f2b388
23e65ea
 
8f2b388
 
 
 
 
 
 
 
 
 
23e65ea
8f2b388
 
d65550f
8f2b388
23e65ea
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
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")
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)

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...")
    ],
    outputs=[
        gr.Textbox(label="Generated Headline"),
        gr.Audio(label="Generated Audio", type="numpy")
    ],

    examples=examples
)
interface.launch()