alakxender's picture
r
b94b651
""" This app combines the Google Translator and the BART summarization model to translate and summarize text.
Note: The app have limitations on the text length. It can only handle up to 5000 characters. The app is designed to be used in the Dhivehi language. This is not a finetuned dhivehi model. this is a simple pipeline to utilize existing modes/models of translation and summarization.
"""
import spaces
import gradio as gr
import subprocess
import sys
import os
summary_type = {
"short":{
"description":"Best for: Executive overviews, abstracts, or capturing the essence of a text briefly.",
"min_length":20, "max_length":50
},
"medium":{
"description":"Best for: Detailed summaries, reports, or providing a comprehensive overview of a text.",
"min_length":50, "max_length":100
},
"detail":{
"description":"Best for: In-depth analysis, detailed reports, or providing a comprehensive overview of a text.",
"min_length":100, "max_length":200
}
}
@spaces.GPU
def translate_text(text, s_type):
if len(text) < 250:
gr.Warning("Text is too short. Please enter a longer text.")
return "Text is too short. Please enter a longer text.", "", ""
if len(text) > 4999:
gr.Warning("Text is too long. Please enter a shorter text.")
return "Text is too long. Please enter a shorter text.", "", ""
# Initialize the translator
translator = GoogleTranslator(source="dhivehi", target="english")
# Perform the translation
translation = translator.translate(text)
# Summarize the text
summary = summarize_text(translation, s_type)
# Translate the summary to Dhivehi
translator = GoogleTranslator(source="english", target="dhivehi")
summary_dhivehi = translator.translate(summary)
# Return the translated text
return translation, summary, summary_dhivehi
def summarize_text(text, s_type):
min_length = summary_type[s_type]["min_length"]
max_length = summary_type[s_type]["max_length"]
#print(f"Min length: {min_length}, Max length: {max_length}")
summary = pipe(text, max_length=max_length, min_length=min_length, do_sample=False)
return summary[0]['summary_text']
eg_text1 = """ގޭމިން ޕްލެޓްފޯމް ރޯބްލޮކްސް އިން ރިމޯޓް ޕޭރެންޓަލް ކޮންޓްރޯލް އާ އެކު ރައްކާތެރިކަމުގެ ބޮޑެތި އަޕްގްރޭޑްތަކެއް އިއުލާނުކޮށް، 13 އަހަރުން ދަށުގެ ކުދިންނަށް އެ ޕްލެޓްފޯމްގައި އެހެން މީހުންނާ މުއާސަލާތު ނުކުރެވޭ ގޮތް ހަދަން ނިންމައިފި އެވެ.
ރޯބްލޮކްސް އަކީ 2004 ވަނަ އަހަރު އުފެއްދި މަޝްހޫރު އޮންލައިން ގޭމިން އަދި ކްރިއޭޓިވިޓީއާ ގުޅުން ހުރި އެތައް ކަމަކަށް ހާއްސަ ޕްލެޓްފޯމެކެވެ.
ރޯބްލޮކްސް ބޭނުންކުރާ ކުދިންނަށް، އެހެން މީހުން އުފައްދާ ގޭމްތަކާއި "އެކްސްޕީރިއަންސް" ތަކުން ބޭނުން ކަމެއް އިޚްތިޔާރުކޮށް، އޮންލައިންކޮށް ގޭމް ކުޅެ އަދި ރިއަލް ޓައިމްގައި އެ މީހުންނާ އެކު ޗެޓް ކުރެވޭ އެވެ.
ރޯބްލޮކްސް ބޭނުންކުރާ މީހުން ދުނިޔޭގެ އެކި ކަންކޮޅުތަކުގައި އުޅެ އެވެ. ހާއްސަކޮށް، ކުޑަކުދިންނާއި ޒުވާނުންގެ އެތައް މިލިއަން ބަޔަކު ބޭނުންކުރާ އިރު، ފަހުގެ ދިރާސާތަކާއި މީޑިއާ ރިޕޯޓްތައް ބުނާ ގޮތުން އެ މި ޕްލެޓްފޯމްގައި ސްކޭމްކުރާ މީހުންނާއި ނުބައި ނިޔަތުގައި އުޅޭ މީހުން ގިނަ އެވެ. އަދި މީގައި ކުޑަކުދިން އެކަށީގެންވާ މިންވަރަށް ހިމާޔަތެއް ވެސް ނުކުރެ އެވެ.
ބްލޫމްބާގްއިން މިދިޔަ އަހަރު ނެރުނު ރިޕޯޓެއްގައި ބުނެފައިވާ ގޮތުގައި ރޯބްލޮކްސް ތެރެއިން ބައްދަލުވެ ކުދިން ގްރޫމްކޮށް، ޖިންސީ ގޯނާކޮށް ނުވަތަ ވަގަށް ނެގުން ފަދަ މައްސަަލަތައް ބޮޑެވެ. މިފަދަ މައްސަލަތަކުގައި 2018 ވަނަ އަހަރުން ފެށިގެން މިހާތަނަށް 24 މީހަކު ހައްޔަރުކޮށްފައިވާ ކަމަށް ވަނީ ބުނެފަ އެވެ.
އެ ރިޕޯޓްގައި ބުނެފައިވަނީ ކާކުކަން ނޭނގޭ އެކައުންޓްތަކުގެ ސަބަބުން، ނުބައި އަމަލުތައް ހިންގުމުގެ ނިޔަތުގައި އުޅޭ މީހުން ދެނެގަންނަން ދަތިވާ ކަމަށެވެ.
- އަލަށް އެޅި ފިޔަވަޅުތަކުގެ ސަބަބުން ބެލެނިވެރިންނަށް ދަރިންގެ ހަރަކާތްތައް މޮނިޓާކޮށް، އަމިއްލަ ޑިވައިސްތަކުން ވަގުތު މާ ގާތުން ބެލޭނެ.
- އަދި 13 އަހަރުން ދަށުގެ ކުދިންނަށް ގޭމްތަކުގެ ބޭރުން ޑައިރެކްޓް މެސެޖިންގެ ފުރުސަތު މިހާރު ނުލިބޭނެ ކަމަށާއި ޑިފޯލްޓްކޮށް ތަޖުރިބާގެ ތެރޭގައި ޕަބްލިކް ބްރޯޑްކާސްޓް ކުރާ މެސެޖުތަކަށް އެކަނި ވެސް އެ ކުންފުނިން ބުންޏެވެ.
"ރޯބްލޮކްސްގައި ކޮންމެ ކަމެއް ކުރާއިރު ވެސް އެންމެ ބޮޑު އިސްކަމެއް ދެނީ ކަސްޓަމަރުންގެ ރައްކާތެރިކަމަށް،" ޗީފް ސޭފްޓީ އޮފިސަރު މެޓް ކޯފްމަން ވިދާޅުވި އެވެ.
މި ގެންނަ ބަދަލުތަކުގެ ތެރޭގައި، ކޮންޓެންޓް ޓައިޕް ލޭބަލްތަކަށް ބަލައި އުމުރާ ގުޅޭ ގޮތުން ކޮންޓެންޓް ރޭޓިން ކަނޑައެޅުން ދޫކޮށްލުން ހިމެނެ އެވެ. މި އުސޫލު ދޫކޮށްލުމަށް ފަހު ހަދަނީ ނުވަ އަހަރުން ދަށުގެ ކަސްޓަމަރުން "މިނިމަލް" ނުވަތަ "މައިލްޑް" ކޮންޓެންޓަށް ބެލެނިވެރިން ހުއްދަ ދީގެން ނޫނީ ނުބެލޭ ގޮތަށެވެ.
"މި އަޕްޑޭޓްތަކުގެ ސަބަބުން، ދަރިފުޅަށް އެންމެ އެކަށީގެންވާ ކަންކަމާ ގުޅޭ ގޮތުން ރަނގަޅު ނިންމުންތައް ނިންމުމަށް ބެލެނިވެރިންނަށް އިތުރަށް ފަސޭހަވާނެ،" ކޯފްމަން ވިދާޅުވި އެވެ."""
eg_text2 = """ސަރުކާރުގެ އޮފީސްތަކުގެ އޮންލައިން ޕޯޓަލް ކަމަށްވާ އީފާސްގެ ފޭސް ރެކޮގްނިޝަންގެ އާ ވެންޑާއަކަށް ލެޓްވިއާގެ ކުންފުންޏެއް ސަރުކާރުން ހަމަޖައްސައިފި އެވެ.
އެންސީއައިޓީން މިއަދު ނެރުނު ނޫސްބަޔާނެއްގައި ބުނީ، އީފާސްގެ ފޭސް ރެކޮގްނިޝަން ހިދުމަތް ދިނުމަށް ކުރިން ހަވާލުކޮށްފައިވާ "ސަމް އެންޑް ސަބްސްޓެންސް ޕްރައިވެޑް ލިމިޓެޑް" (ސަމްސަބް) ކުންފުނި އެ މަސައްކަތުން ވަކިކޮށް، އާ ވެންޑާއަކާ މަސައްކަތް ހަވާލުކުރުމަށް ފަހު އަލުން އޮންލައިން ފޭސް ވެރިފިކޭޝަންގެ ހިދުމަތް މިހާރު ފަށާފައިވާ ކަމަށެވެ.
ސަމްސަބް އަކީ އިނގިރޭސިވިލާތުގައި ރަޖިސްޓްރީކޮށްފައި އޮންނަ އިޒްރޭލުގެ ބަޔަކު އުފައްދާފައިވާ ކުންފުންޏެކެވެ. އެހެންވެ، އެކަމުގައި އާންމުންގެ ފާޑުކިޔުން ބޮޑުވުމުން ސަރުކާރުން އެ ކުންފުނި ވަކިކުރީ އެވެ.
އާންމުންގެ ފާޑުކިޔުންތަކާ ގުޅިގެން އެންސީއައިޓީން އޭރު ބުނެފައި ވަނީ ސަމްސަބްއާ އެ ހިދުމަތް ދިނުމަށް ހަވާލުކުރީ އެ ކުންފުންޏަކީ އިނގިރޭސިވިލާތުގެ ގާނޫނުގެ ދަށުން ހިންގާ ބަޔަކަށް ވާތީ ކަމަށެވެ. އަދި ފޮޓޯ ފިޔަވާ އެއްވެސް ދިވެއްސެއްގެ ޒާތީ މައުލޫމާތު އެ ކުންފުންޏަށް ނޭނގޭނެ ކަމަށް އޭރު ބުނެފައިވެ އެވެ.
އެންސީއައިޓީގެ ބަޔާނުގައި ބުނީ އާ ވެންޑާއަކަށް ހަމަޖައްސާފައި ވަނީ ޔޫރަޕުގެ ގައުމެއް ކަމަށްވާ ލެޓްވިއާގައި ރަޖިސްޓަރީކޮށްފައިވާ ރެގިއުލާ ފޮރެންސިކްސް ނަމަކަށް ކިޔާ ކުންފުންޏެއް ކަމަށެވެ. ފޭސް ވެރިފިކޭޝަން ހެދުމަށް މިހާރު ބޭނުންކުރަނީ އެ ކުންފުނީގެ އެޕްލިކޭޝަނެއް ކަމަށް ވެސް އެ ބަޔާނުގައި ބުންޏެވެ.
"އަދި، މިކަން ހިނގާނީ ބޭރުގެ އެއްވެސް ފަރާތަކަށް އެއްވެސް ރައްޔިތެއްގެ ފޮޓޯއެއް ނުވަތަ އެ ނޫން ވެސް އެހެން ޒާތީ މައުލޫމާތެއް ހިއްސާކުރުމަކާ ނުލައި އެންސީއައިޓީގެ އެތެރޭގަ އެވެ،" ބަޔާނުގައި ބުންޏެވެ.
ރެގިއުލާ ފޮރެންސިކްސް އަކީ، އެންސީއައިޓީން ބުނި ގޮތުގައި، 30 އަހަރުގެ ތަޖުރިބާއާ އެކު، ސެކިއުރިޓީގެ ދާއިރާއިން ގިނަ ޑިޖިޓަލް ހިދުމަތްތަކެއް ފޯރުކޮށްދޭ ގާބިލް އަދި ބައިނަލްއަގުވާމީ ބޮޑެތި ކުންފުނިތަކާ ތަފާތު އެކި ގައުމުތަކަށް ވެސް ހިދުމަތްދެމުން އަންނަ އިތުބާރު ހުރި ކުންފުންޏެކެވެ.
އެންސީއައިޓީން ބުނީ މިވަގުތު ފޭސް ރިކޮގްނިޝަންގެ ހިދުމަތް ލިބޭނީ ވެބް ބްރައުޒާއެއް މެދުވެރިކޮށް އީފާސް އަށް ލޮގިންވާއިރު އެކަނި ކަމަށެވެ.
"""
css = """
.textbox1 textarea {
font-size: 18px !important;
font-family: 'MV_Faseyha', 'Faruma', 'A_Faruma' !important;
line-height: 1.8 !important;
}
"""
# Define the Gradio interface
iface = gr.Interface(
fn=translate_text, # Function to wrap
inputs=[
gr.Textbox(lines=2, placeholder="ލިޔުމެއް", label="Dhivehi Text", rtl=True, elem_classes="textbox1"), # Text input
gr.Radio(summary_type.keys(), label="Summary Type", value="medium")
],
css=css,
outputs=[gr.Textbox(label="English Text", lines=2), gr.Textbox(label="English Summarized Text", lines=2), gr.Textbox(label="Dhivehi Summary", lines=2, rtl=True, elem_classes="textbox1")], # Output type
title="Dhivehi Translation and Summarization",
description="Translate text from Dhivehi to English and summarize the text in English and Dhivehi.",
examples=[
[eg_text1, "medium"],
[eg_text2, "short"]
]
)
def install_requirements():
requirements_path = 'requirements.txt'
# Check if requirements.txt exists
if not os.path.exists(requirements_path):
print("Error: requirements.txt not found")
return False
try:
print("Installing requirements...")
# Using --no-cache-dir to avoid memory issues
subprocess.check_call([
sys.executable,
"-m",
"pip",
"install",
"-r",
requirements_path,
"--no-cache-dir"
])
print("Successfully installed all requirements")
return True
except subprocess.CalledProcessError as e:
print(f"Error installing requirements: {e}")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
if __name__ == "__main__":
success = install_requirements()
if success:
print("All requirements installed successfully")
# Launch the Gradio interface
from deep_translator import GoogleTranslator
from transformers import pipeline
pipe = pipeline("summarization", model="facebook/bart-large-cnn")
iface.launch()
else:
print("Failed to install some requirements")