Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
+
|
6 |
+
headline_gen = pipeline("text2text-generation", model="Michau/t5-base-en-generate-headline")
|
7 |
+
ar_to_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")
|
8 |
+
en_to_ar_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")
|
9 |
+
|
10 |
+
image_gen = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
|
11 |
+
|
12 |
+
def generate_headline(selected_language, text):
|
13 |
+
if selected_language == "Arabic":
|
14 |
+
translated_text = ar_to_en_translator(text)[0]['translation_text'] #Translate Arabic text to english, to use the model
|
15 |
+
result1 = headline_gen(translated_text, max_length=100, truncation=True) #Translated text will go to the model, and return the proper heading in english
|
16 |
+
english_headline = result1[0]['generated_text'] #The heading in english
|
17 |
+
arabic_headline = en_to_ar_translator(english_headline)[0]['translation_text'] #Translate the heading to Arabic
|
18 |
+
image = image_gen(english_headline).images[0]
|
19 |
+
return arabic_headline, image
|
20 |
+
|
21 |
+
elif selected_language == "English":
|
22 |
+
result1 = headline_gen(text, max_length=100, truncation=True)
|
23 |
+
english_headline = result1[0]['generated_text']
|
24 |
+
image = image_gen(english_headline).images[0]
|
25 |
+
return english_headline, image
|
26 |
+
|
27 |
+
examples = [
|
28 |
+
#First parameter is for the dropdown menu, and the second parameter is for the starter of the poem
|
29 |
+
["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."],
|
30 |
+
["Arabic", "تعتبر انبعاثات الغازات الدفيئة، مثل ثاني أكسيد الكربون (CO2) والميثان (CH4)، من الأسباب الرئيسية لتغير المناخ العالمي. تؤدي الأنشطة البشرية، مثل حرق الوقود الأحفوري لإنتاج الطاقة وإزالة الغابات والعمليات الصناعية، إلى زيادة كبيرة في تركيز هذه الغازات في الغلاف الجوي. وفقًا للهيئة الحكومية الدولية المعنية بتغير المناخ (IPCC)، ارتفعت مستويات ثاني أكسيد الكربون بأكثر من 50٪ منذ عصر ما قبل الصناعة، مما ساهم في ارتفاع درجات الحرارة العالمية."]
|
31 |
+
]
|
32 |
+
|
33 |
+
interface = gr.Interface(
|
34 |
+
fn=generate_headline,
|
35 |
+
inputs=[
|
36 |
+
gr.Dropdown(choices=["Arabic", "English"], label="Select Language"),
|
37 |
+
gr.Textbox(lines=5, placeholder="Enter article text here...")
|
38 |
+
],
|
39 |
+
outputs=[
|
40 |
+
gr.Textbox(label="Generated Headline"),
|
41 |
+
gr.Image(label="Generated Image")
|
42 |
+
],
|
43 |
+
|
44 |
+
examples=examples
|
45 |
+
)
|
46 |
+
interface.launch()
|