oB3TAo commited on
Commit
33cd3a4
·
1 Parent(s): 3bd6a3c

initial commit

Browse files
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from translation import translate_text
3
+ from summarization import summarize_text
4
+ from sentiment_analysis import analyze_sentiment
5
+ from grammar_correction import correct_text
6
+ from character_count import count_characters
7
+ from examples import (translation_examples, summarization_examples,
8
+ sentiment_examples, grammar_examples,
9
+ character_count_examples)
10
+
11
+ # Create Gradio app with tabs for different functionalities
12
+ with gr.Blocks() as demo:
13
+ gr.Markdown("# Multilingual Translator and Text Analysis Tool")
14
+
15
+ with gr.Tab("Translation"):
16
+ input_text_trans = gr.Textbox(lines=3, placeholder="Enter text", label="Input Text (English)")
17
+ target_language_dropdown = gr.Dropdown(choices=["French", "German", "Romanian"],
18
+ label="Select target language", value="French")
19
+ output_text_trans = gr.Textbox(label="Translated Text", interactive=True)
20
+ translate_btn = gr.Button("Translate")
21
+ translate_btn.click(fn=translate_text, inputs=[input_text_trans, target_language_dropdown],
22
+ outputs=output_text_trans)
23
+ gr.Examples(translation_examples, inputs=[input_text_trans, target_language_dropdown])
24
+
25
+ with gr.Tab("Summarization"):
26
+ input_text_summarize = gr.Textbox(lines=5, placeholder="Enter text to summarize", label="Input Text")
27
+ summarized_text = gr.Textbox(label="Summary", interactive=False)
28
+ summarize_btn = gr.Button("Summarize")
29
+ summarize_btn.click(fn=summarize_text, inputs=input_text_summarize, outputs=summarized_text)
30
+ gr.Examples(summarization_examples, inputs=input_text_summarize)
31
+
32
+ with gr.Tab("Sentiment Analysis"):
33
+ input_text_sentiment = gr.Textbox(lines=3, placeholder="Enter text to analyze", label="Input Text")
34
+ sentiment_output = gr.Textbox(label="Sentiment", interactive=False)
35
+ sentiment_btn = gr.Button("Analyze Sentiment")
36
+ sentiment_btn.click(fn=analyze_sentiment, inputs=input_text_sentiment, outputs=sentiment_output)
37
+ gr.Examples(sentiment_examples, inputs=input_text_sentiment)
38
+
39
+ with gr.Tab("Grammar Correction"):
40
+ input_text_correct = gr.Textbox(lines=3, placeholder="Enter text for grammar correction", label="Input Text")
41
+ corrected_text = gr.Textbox(label="Corrected Text", interactive=False)
42
+ correct_btn = gr.Button("Correct Grammar")
43
+ correct_btn.click(fn=correct_text, inputs=input_text_correct, outputs=corrected_text)
44
+ gr.Examples(grammar_examples, inputs=input_text_correct)
45
+
46
+ with gr.Tab("Character Count"):
47
+ input_text_count = gr.Textbox(lines=3, placeholder="Enter text to count characters", label="Input Text")
48
+ character_count = gr.Textbox(label="Character Count", interactive=False)
49
+ count_btn = gr.Button("Count Characters")
50
+ count_btn.click(fn=count_characters, inputs=input_text_count, outputs=character_count)
51
+ gr.Examples(character_count_examples, inputs=input_text_count)
52
+
53
+ demo.launch()
character_count.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ def count_characters(text):
2
+ return f"Character Count: {len(text)}"
examples.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Examples for translation
2
+ translation_examples = [
3
+ ["I love programming", "French"],
4
+ ["This is a great day", "German"],
5
+ ["Machine learning is fascinating", "Romanian"]
6
+ ]
7
+
8
+ # Examples for summarization
9
+ summarization_examples = [
10
+ ["Artificial intelligence is the simulation of human intelligence in machines that are programmed to think and act like humans. The term may also be applied to any machine that exhibits traits associated with a human mind such as learning and problem-solving."],
11
+ ["Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed."]
12
+ ]
13
+
14
+ # Examples for sentiment analysis
15
+ sentiment_examples = [
16
+ ["I am extremely happy with the results."],
17
+ ["This is the worst experience I’ve had."]
18
+ ]
19
+
20
+ # Examples for grammar correction
21
+ grammar_examples = [
22
+ ["He go to school everyday."],
23
+ ["She dont know the answer."]
24
+ ]
25
+
26
+ # Examples for character count
27
+ character_count_examples = [
28
+ ["How many characters does this sentence have?"]
29
+ ]
grammar_correction.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ # Load the grammar correction model
4
+ grammar_correction_model = pipeline(task="text2text-generation", model="hassaanik/grammar-correction-model")
5
+
6
+ def correct_text(text):
7
+ result = grammar_correction_model(text, max_length=200, num_beams=5, no_repeat_ngram_size=2)
8
+ return result[0]['generated_text']
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio~=4.44.0
2
+ transformers~=4.44.2
3
+ torch~=2.4.1
sentiment_analysis.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ # Load sentiment analysis model
4
+ sentiment_analyzer = pipeline("sentiment-analysis")
5
+
6
+ def analyze_sentiment(text):
7
+ result = sentiment_analyzer(text)[0]
8
+ return f"Sentiment: {result['label']} (Confidence: {result['score']:.2f})"
summarization.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ # Load summarization model
4
+ summarizer = pipeline("summarization", model="t5-small")
5
+
6
+ def summarize_text(text):
7
+ summary = summarizer(text, max_length=60, min_length=25, do_sample=False)[0]['summary_text']
8
+ word_count = len(summary.split())
9
+ return summary, f"Word Count: {word_count}"
translation.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, T5ForConditionalGeneration
2
+
3
+ # Load the model and tokenizer
4
+ tokenizer = AutoTokenizer.from_pretrained("t5-small")
5
+ model = T5ForConditionalGeneration.from_pretrained("t5-small")
6
+
7
+ # Define inference function for translation
8
+ def translate_text(text, target_language):
9
+ language_code = {
10
+ "French": "translate English to French",
11
+ "German": "translate English to German",
12
+ "Romanian": "translate English to Romanian",
13
+ }
14
+ translation_task = language_code.get(target_language, "translate English to French")
15
+ input_ids = tokenizer(f"{translation_task}: {text}", return_tensors="pt").input_ids
16
+ outputs = model.generate(input_ids, max_length=60, num_beams=4, early_stopping=True)
17
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)