|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from transformers import pipeline |
|
import json |
|
|
|
|
|
simplifier = pipeline("summarization", model="t5-small") |
|
|
|
def simplify_text(text): |
|
result = simplifier(text, max_length=100, min_length=50, do_sample=False) |
|
return result[0]['summary_text'] |
|
|
|
|
|
translator_en = pipeline("translation_es_to_en", model="Helsinki-NLP/opus-mt-es-en") |
|
translator_ar = pipeline("translation_es_to_ar", model="Helsinki-NLP/opus-mt-es-ar") |
|
translator_fr = pipeline("translation_es_to_fr", model="Helsinki-NLP/opus-mt-es-fr") |
|
|
|
def translate_text(text): |
|
translations = { |
|
"english": translator_en(text)[0]['translation_text'], |
|
"arabic": translator_ar(text)[0]['translation_text'], |
|
"french": translator_fr(text)[0]['translation_text'] |
|
} |
|
return translations |
|
|
|
|
|
classifier = pipeline("zero-shot-classification", model="distilbert-base-uncased-finetuned-sst-2-english") |
|
labels = ["Technology", "Science", "Health", "Business", "Education", "Other"] |
|
|
|
def identify_topic(text): |
|
classification = classifier(text, candidate_labels=labels) |
|
return classification['labels'][0] |
|
|
|
|
|
tone_analyzer = pipeline("sentiment-analysis", model="roberta-base") |
|
|
|
def detect_tone(text): |
|
tone_result = tone_analyzer(text)[0] |
|
return tone_result['label'] |
|
|
|
|
|
def process_text_for_web_service(text): |
|
simplified_text = simplify_text(text) |
|
translations = translate_text(simplified_text) |
|
main_topic = identify_topic(simplified_text) |
|
tone = detect_tone(simplified_text) |
|
|
|
|
|
result = { |
|
"original_text": text, |
|
"simplified_text": simplified_text, |
|
"translations": translations, |
|
"main_topic": main_topic, |
|
"tone": tone |
|
} |
|
|
|
|
|
return json.dumps(result, ensure_ascii=False, indent=4) |
|
|
|
|
|
input_text = "La inteligencia artificial (IA) est谩 revolucionando la industria de la tecnolog铆a al permitir nuevas aplicaciones en m煤ltiples campos, desde la salud hasta la educaci贸n." |
|
|
|
|
|
formatted_output = process_text_for_web_service(input_text) |
|
|
|
|
|
print(formatted_output) |
|
|