Project2 / app.py
SarahMarzouq's picture
Update app.py
cb9f6a6 verified
raw
history blame
3.25 kB
import gradio as gr
from transformers import pipeline
import torch
from diffusers import DiffusionPipeline
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")
image_gen = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
def generate_headline(selected_language, text):
if selected_language == "Arabic":
translated_text = ar_to_en_translator(text)[0]['translation_text'] #Translate Arabic text to english, to use the model
result1 = headline_gen(translated_text, max_length=100, truncation=True) #Translated text will go to the model, and return the proper heading in english
english_headline = result1[0]['generated_text'] #The heading in english
arabic_headline = en_to_ar_translator(english_headline)[0]['translation_text'] #Translate the heading to Arabic
image = image_gen(english_headline).images[0]
return arabic_headline, image
elif selected_language == "English":
result1 = headline_gen(text, max_length=100, truncation=True)
english_headline = result1[0]['generated_text']
image = image_gen(english_headline).images[0]
return english_headline, image
examples = [
#First parameter is for the dropdown menu, and the second parameter is for the starter of the poem
["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."],
["Arabic", "تعتبر انبعاثات الغازات الدفيئة، مثل ثاني أكسيد الكربون (CO2) والميثان (CH4)، من الأسباب الرئيسية لتغير المناخ العالمي. تؤدي الأنشطة البشرية، مثل حرق الوقود الأحفوري لإنتاج الطاقة وإزالة الغابات والعمليات الصناعية، إلى زيادة كبيرة في تركيز هذه الغازات في الغلاف الجوي. وفقًا للهيئة الحكومية الدولية المعنية بتغير المناخ (IPCC)، ارتفعت مستويات ثاني أكسيد الكربون بأكثر من 50٪ منذ عصر ما قبل الصناعة، مما ساهم في ارتفاع درجات الحرارة العالمية."]
]
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.Image(label="Generated Image")
],
examples=examples
)
interface.launch()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)