Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
+
|
| 5 |
+
# Translation models
|
| 6 |
+
translation_models = {
|
| 7 |
+
'Vietnamese': "Helsinki-NLP/opus-mt-en-vi",
|
| 8 |
+
'Japanese': "Helsinki-NLP/opus-mt-en-jap",
|
| 9 |
+
'Thai': "Helsinki-NLP/opus-mt-en-tha",
|
| 10 |
+
'Spanish': "Helsinki-NLP/opus-mt-en-es"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Initialize translation pipeline
|
| 14 |
+
def get_translator(language):
|
| 15 |
+
model_name = translation_models.get(language)
|
| 16 |
+
if model_name:
|
| 17 |
+
return pipeline("translation", model=model_name)
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
# Helper function to generate bullet points
|
| 21 |
+
def generate_bullet_points(text):
|
| 22 |
+
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 23 |
+
sentences = text.split('. ')
|
| 24 |
+
embeddings = model.encode(sentences, convert_to_tensor=True)
|
| 25 |
+
clusters = util.community_detection(embeddings, threshold=0.75)
|
| 26 |
+
|
| 27 |
+
bullet_points = []
|
| 28 |
+
for cluster in clusters:
|
| 29 |
+
cluster_sentences = [sentences[idx] for idx in cluster]
|
| 30 |
+
main_sentence = cluster_sentences[0] if cluster_sentences else ""
|
| 31 |
+
bullet_points.append(main_sentence.strip())
|
| 32 |
+
|
| 33 |
+
return "\n".join(f"- {point}" for point in bullet_points)
|
| 34 |
+
|
| 35 |
+
# Helper function to translate text
|
| 36 |
+
def translate_text(text, language):
|
| 37 |
+
translator = get_translator(language)
|
| 38 |
+
if translator:
|
| 39 |
+
translated_text = translator(text)[0]['translation_text']
|
| 40 |
+
return translated_text
|
| 41 |
+
return text
|
| 42 |
+
|
| 43 |
+
def process_text(input_text, language):
|
| 44 |
+
bullet_points = generate_bullet_points(input_text)
|
| 45 |
+
translated_text = translate_text(bullet_points, language)
|
| 46 |
+
return bullet_points, translated_text
|
| 47 |
+
|
| 48 |
+
# Create Gradio interface
|
| 49 |
+
iface = gr.Interface(
|
| 50 |
+
fn=process_text,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Textbox(label="Input Text", placeholder="Paste your text here..."),
|
| 53 |
+
gr.Dropdown(choices=["Vietnamese", "Japanese", "Thai", "Spanish"], label="Translate to", value="Vietnamese")
|
| 54 |
+
],
|
| 55 |
+
outputs=[
|
| 56 |
+
gr.Textbox(label="Bullet Points"),
|
| 57 |
+
gr.Textbox(label="Translated Bullet Points")
|
| 58 |
+
],
|
| 59 |
+
title="Text to Bullet Points and Translation",
|
| 60 |
+
description="Paste any text, and the program will transform it into bullet points. Optionally, translate the bullet points into Vietnamese, Japanese, Thai, or Spanish."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
iface.launch()
|