naxemCDA commited on
Commit
c4f352b
·
1 Parent(s): 69882d5

replaced with text gen task

Browse files
Files changed (1) hide show
  1. app.py +18 -21
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: Load dictionary/thesaurus model
14
- #dictionary_model = pipeline("text2text-generation", model="google/flan-t5-large")
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: Dictionary + Thesaurus function
61
- def define_and_list_synonyms(word):
62
- if not word.strip():
63
- return "Please enter a word."
64
- prompt = f"Define the word '{word}' and give 5 related words or synonyms."
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 a **Dictionary-Thesaurus**. "
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 → BM)"):
103
  trans_input = gr.Textbox(label="Enter English text", lines=4, placeholder="Type English text here...")
104
- trans_output = gr.Textbox(label="Malay Translation", lines=3)
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 (BM → EN)"):
109
- trans_input = gr.Textbox(label="Enter Malay text", lines=4, placeholder="Type Malay text here...")
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: Dictionary + Thesaurus Tab
115
- with gr.Tab("Dictionary + Thesaurus"):
116
- dict_input = gr.Textbox(label="Enter a word", placeholder="e.g., resilient")
117
- dict_output = gr.Textbox(label="Definition and Related Words", lines=4)
118
- dict_button = gr.Button("Get Meaning and Synonyms")
119
- dict_button.click(define_and_list_synonyms, inputs=dict_input, outputs=dict_output)
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"