Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,102 @@
|
|
| 1 |
-
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"""
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.json(out)
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# # pipe = pipeline('sentiment-analysis')
|
| 5 |
+
# text = st.text_area('enter text: ')
|
| 6 |
+
|
| 7 |
+
# generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# technical_text = """
|
| 11 |
+
# The CRISPR-Cas9 system enables precise genome editing by creating double-strand breaks at specific DNA locations, facilitating targeted genetic modifications.
|
| 12 |
+
# """
|
| 13 |
+
|
| 14 |
+
# # Prompt para transformaci贸n
|
| 15 |
+
# prompt = f"Rewrite the following technical text in simple terms for a general audience:\n\n{text}\n\nSimplified version:"
|
| 16 |
+
|
| 17 |
+
# # Generar texto transformado
|
| 18 |
+
# result = generator(
|
| 19 |
+
# prompt,
|
| 20 |
+
# max_length=256,
|
| 21 |
+
# num_return_sequences=1,
|
| 22 |
+
# do_sample=True,
|
| 23 |
+
# temperature=0.1,
|
| 24 |
+
# top_p=0.9,
|
| 25 |
+
# repetition_penalty=1.1,
|
| 26 |
+
# )
|
| 27 |
+
|
| 28 |
+
# print(result[0]['generated_text'])
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# if text:
|
| 32 |
+
# out = pipe(text)
|
| 33 |
+
# st.json(out)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
from transformers import pipeline
|
| 39 |
+
import json
|
| 40 |
+
|
| 41 |
+
# Step 1: Rewriting the technical text in accessible language using T5 model
|
| 42 |
+
simplifier = pipeline("summarization", model="t5-small")
|
| 43 |
+
|
| 44 |
+
def simplify_text(text):
|
| 45 |
+
result = simplifier(text, max_length=100, min_length=50, do_sample=False)
|
| 46 |
+
return result[0]['summary_text']
|
| 47 |
+
|
| 48 |
+
# Step 2: Translation to English, Arabic, and French using MarianMT models
|
| 49 |
+
translator_en = pipeline("translation_es_to_en", model="Helsinki-NLP/opus-mt-es-en")
|
| 50 |
+
translator_ar = pipeline("translation_es_to_ar", model="Helsinki-NLP/opus-mt-es-ar")
|
| 51 |
+
translator_fr = pipeline("translation_es_to_fr", model="Helsinki-NLP/opus-mt-es-fr")
|
| 52 |
|
| 53 |
+
def translate_text(text):
|
| 54 |
+
translations = {
|
| 55 |
+
"english": translator_en(text)[0]['translation_text'],
|
| 56 |
+
"arabic": translator_ar(text)[0]['translation_text'],
|
| 57 |
+
"french": translator_fr(text)[0]['translation_text']
|
| 58 |
+
}
|
| 59 |
+
return translations
|
| 60 |
|
| 61 |
+
# Step 3: Identify the main topic using DistilBERT
|
| 62 |
+
classifier = pipeline("zero-shot-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 63 |
+
labels = ["Technology", "Science", "Health", "Business", "Education", "Other"]
|
| 64 |
|
| 65 |
+
def identify_topic(text):
|
| 66 |
+
classification = classifier(text, candidate_labels=labels)
|
| 67 |
+
return classification['labels'][0] # Main topic
|
| 68 |
|
| 69 |
+
# Step 4: Detect the tone of the text using RoBERTa
|
| 70 |
+
tone_analyzer = pipeline("sentiment-analysis", model="roberta-base")
|
|
|
|
| 71 |
|
| 72 |
+
def detect_tone(text):
|
| 73 |
+
tone_result = tone_analyzer(text)[0]
|
| 74 |
+
return tone_result['label'] # This gives a general idea of the tone (positive, neutral, etc.)
|
| 75 |
|
| 76 |
+
# Step 5: Formatting results for web service
|
| 77 |
+
def process_text_for_web_service(text):
|
| 78 |
+
simplified_text = simplify_text(text)
|
| 79 |
+
translations = translate_text(simplified_text)
|
| 80 |
+
main_topic = identify_topic(simplified_text)
|
| 81 |
+
tone = detect_tone(simplified_text)
|
| 82 |
+
|
| 83 |
+
# Create a structured output
|
| 84 |
+
result = {
|
| 85 |
+
"original_text": text,
|
| 86 |
+
"simplified_text": simplified_text,
|
| 87 |
+
"translations": translations,
|
| 88 |
+
"main_topic": main_topic,
|
| 89 |
+
"tone": tone
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
# Convert to JSON for web service
|
| 93 |
+
return json.dumps(result, ensure_ascii=False, indent=4)
|
| 94 |
|
| 95 |
+
# Example input text (in Spanish)
|
| 96 |
+
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."
|
| 97 |
|
| 98 |
+
# Run the process
|
| 99 |
+
formatted_output = process_text_for_web_service(input_text)
|
| 100 |
|
| 101 |
+
# Output the JSON formatted result
|
| 102 |
+
print(formatted_output)
|
|
|