Spaces:
Sleeping
Sleeping
replaced with text gen task
Browse files
app.py
CHANGED
|
@@ -10,10 +10,8 @@ translator_en_id = pipeline("translation_en_to_id", model="Helsinki-NLP/opus-mt-
|
|
| 10 |
translator_id_en = pipeline("translation_id_to_en", model="Helsinki-NLP/opus-mt-id-en")
|
| 11 |
ner = pipeline("ner", grouped_entities=True)
|
| 12 |
|
| 13 |
-
# New:
|
| 14 |
-
|
| 15 |
-
dictionary_model = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 16 |
-
|
| 17 |
|
| 18 |
# Functions for each feature
|
| 19 |
def summarize_text(text):
|
|
@@ -57,12 +55,11 @@ def extract_entities(text):
|
|
| 57 |
formatted = "\n".join([f"{e['entity_group']}: {e['word']}" for e in entities])
|
| 58 |
return formatted
|
| 59 |
|
| 60 |
-
# New:
|
| 61 |
-
def
|
| 62 |
-
if not
|
| 63 |
-
return "Please enter a
|
| 64 |
-
|
| 65 |
-
result = dictionary_model(prompt, max_new_tokens=60)
|
| 66 |
return result[0]["generated_text"]
|
| 67 |
|
| 68 |
# Build Gradio interface
|
|
@@ -70,7 +67,7 @@ with gr.Blocks() as demo:
|
|
| 70 |
gr.Markdown("# Multi-Function AI Assistant")
|
| 71 |
gr.Markdown(
|
| 72 |
"This app provides **Text Summarization**, **Question Answering**, **Sentiment Analysis**, "
|
| 73 |
-
"**Translation**, **Named Entity Recognition**, and
|
| 74 |
"All models run efficiently on CPU, suitable for free tier deployment."
|
| 75 |
)
|
| 76 |
|
|
@@ -99,24 +96,24 @@ with gr.Blocks() as demo:
|
|
| 99 |
ner_button = gr.Button("Extract Entities")
|
| 100 |
ner_button.click(extract_entities, inputs=ner_input, outputs=ner_output)
|
| 101 |
|
| 102 |
-
with gr.Tab("Translation (EN →
|
| 103 |
trans_input = gr.Textbox(label="Enter English text", lines=4, placeholder="Type English text here...")
|
| 104 |
-
trans_output = gr.Textbox(label="
|
| 105 |
trans_button = gr.Button("Translate")
|
| 106 |
trans_button.click(translate_en_to_id, inputs=trans_input, outputs=trans_output)
|
| 107 |
|
| 108 |
-
with gr.Tab("Translation (
|
| 109 |
-
trans_input = gr.Textbox(label="Enter
|
| 110 |
trans_output = gr.Textbox(label="English Translation", lines=3)
|
| 111 |
trans_button = gr.Button("Translate")
|
| 112 |
trans_button.click(translate_id_to_en, inputs=trans_input, outputs=trans_output)
|
| 113 |
|
| 114 |
-
# New:
|
| 115 |
-
with gr.Tab("
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
gr.Markdown(
|
| 122 |
"### Notes:\n"
|
|
|
|
| 10 |
translator_id_en = pipeline("translation_id_to_en", model="Helsinki-NLP/opus-mt-id-en")
|
| 11 |
ner = pipeline("ner", grouped_entities=True)
|
| 12 |
|
| 13 |
+
# New: Text Generation pipeline
|
| 14 |
+
text_generator = pipeline("text-generation", model="gpt2")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Functions for each feature
|
| 17 |
def summarize_text(text):
|
|
|
|
| 55 |
formatted = "\n".join([f"{e['entity_group']}: {e['word']}" for e in entities])
|
| 56 |
return formatted
|
| 57 |
|
| 58 |
+
# New: Text Generation function
|
| 59 |
+
def generate_text(prompt):
|
| 60 |
+
if not prompt.strip():
|
| 61 |
+
return "Please enter a starting phrase."
|
| 62 |
+
result = text_generator(prompt, max_length=100, num_return_sequences=1)
|
|
|
|
| 63 |
return result[0]["generated_text"]
|
| 64 |
|
| 65 |
# Build Gradio interface
|
|
|
|
| 67 |
gr.Markdown("# Multi-Function AI Assistant")
|
| 68 |
gr.Markdown(
|
| 69 |
"This app provides **Text Summarization**, **Question Answering**, **Sentiment Analysis**, "
|
| 70 |
+
"**Translation**, **Named Entity Recognition**, and **Text Generation**. "
|
| 71 |
"All models run efficiently on CPU, suitable for free tier deployment."
|
| 72 |
)
|
| 73 |
|
|
|
|
| 96 |
ner_button = gr.Button("Extract Entities")
|
| 97 |
ner_button.click(extract_entities, inputs=ner_input, outputs=ner_output)
|
| 98 |
|
| 99 |
+
with gr.Tab("Translation (EN → ID)"):
|
| 100 |
trans_input = gr.Textbox(label="Enter English text", lines=4, placeholder="Type English text here...")
|
| 101 |
+
trans_output = gr.Textbox(label="Indonesian Translation", lines=3)
|
| 102 |
trans_button = gr.Button("Translate")
|
| 103 |
trans_button.click(translate_en_to_id, inputs=trans_input, outputs=trans_output)
|
| 104 |
|
| 105 |
+
with gr.Tab("Translation (ID → EN)"):
|
| 106 |
+
trans_input = gr.Textbox(label="Enter Indonesian text", lines=4, placeholder="Type Indonesian text here...")
|
| 107 |
trans_output = gr.Textbox(label="English Translation", lines=3)
|
| 108 |
trans_button = gr.Button("Translate")
|
| 109 |
trans_button.click(translate_id_to_en, inputs=trans_input, outputs=trans_output)
|
| 110 |
|
| 111 |
+
# New: Text Generation Tab
|
| 112 |
+
with gr.Tab("Text Generation"):
|
| 113 |
+
gen_input = gr.Textbox(label="Enter a starting phrase", lines=3, placeholder="e.g., Once upon a time")
|
| 114 |
+
gen_output = gr.Textbox(label="Generated Text", lines=6)
|
| 115 |
+
gen_button = gr.Button("Generate")
|
| 116 |
+
gen_button.click(generate_text, inputs=gen_input, outputs=gen_output)
|
| 117 |
|
| 118 |
gr.Markdown(
|
| 119 |
"### Notes:\n"
|