SarahMarzouq commited on
Commit
408f738
·
verified ·
1 Parent(s): 55d5e5f

Create app.py

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