Spaces:
Sleeping
Sleeping
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() |