Pixel101 commited on
Commit
e2b83d1
·
verified ·
1 Parent(s): 1a45551

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -103
app.py CHANGED
@@ -1,104 +1,101 @@
1
- # Install necessary libraries
2
- !pip install transformers gradio librosa
3
-
4
- import gradio as gr
5
- from transformers import pipeline
6
-
7
- # Load models
8
- # Sentiment Analysis
9
- classifier_sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
10
-
11
- def analyze_sentiment(text):
12
- result = classifier_sentiment(text)[0]
13
- label = result['label']
14
- score = result['score']
15
- return f"Label: {label}, Score: {score:.2f}"
16
-
17
- # Translation
18
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
19
-
20
- def translate_text(text):
21
- result = translator(text)[0]
22
- translated_text = result["translation_text"]
23
- return translated_text
24
-
25
- # Image Classification
26
- classifier_image = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")
27
-
28
- def classify_image(image):
29
- results = classifier_image(image)
30
- output = ""
31
- for result in results:
32
- output += f"{result['label']}: {result['score']:.2f}\n"
33
- return output
34
-
35
- # Speech to Text
36
- speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
37
-
38
- def transcribe_audio(audio):
39
- text = speech_to_text(audio)["text"]
40
- return text
41
-
42
- # Text Summarization
43
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
44
-
45
- def summarize_text(text):
46
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
47
- return summary
48
-
49
- # Define custom CSS styles
50
- css = """
51
- <style>
52
- body {
53
- background-color: #e9ecef;
54
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
55
- }
56
- .gradio-container {
57
- border-radius: 15px;
58
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
59
- padding: 20px;
60
- background-color: #74748a;
61
- max-width: 800px;
62
- margin: auto;
63
- }
64
- h1 {
65
- color: black;
66
- }
67
-
68
- </style>
69
- """
70
-
71
- with gr.Blocks(css=css) as demo:
72
- gr.Markdown("<h1 style='text-align: center;'>Multi-functional AI Demo</h1>")
73
-
74
- with gr.Tab("Sentiment Analysis😣"):
75
- text_input = gr.Textbox(placeholder="Enter text here...")
76
- text_output = gr.Textbox()
77
- sentiment_button = gr.Button("Analyze")
78
- sentiment_button.click(analyze_sentiment, inputs=text_input, outputs=text_output)
79
-
80
- with gr.Tab("Translation📚"):
81
- text_input_trans = gr.Textbox(placeholder="Enter English text here...")
82
- text_output_trans = gr.Textbox()
83
- trans_button = gr.Button("Translate")
84
- trans_button.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
85
-
86
- with gr.Tab("Image Classification🔮"):
87
- image_input = gr.Image(type="pil")
88
- image_output = gr.Textbox()
89
- image_button = gr.Button("Classify")
90
- image_button.click(classify_image, inputs=image_input, outputs=image_output)
91
-
92
- with gr.Tab("Speech to Text🔊"):
93
- audio_input = gr.Audio(sources=["microphone"], type="filepath")
94
- audio_output = gr.Textbox()
95
- audio_button = gr.Button("Transcribe")
96
- audio_button.click(transcribe_audio, inputs=audio_input, outputs=audio_output)
97
-
98
- with gr.Tab("Text Summarization📑"):
99
- text_input_summ = gr.Textbox(placeholder="Enter text here...")
100
- text_output_summ = gr.Textbox()
101
- summ_button = gr.Button("Summarize")
102
- summ_button.click(summarize_text, inputs=text_input_summ, outputs=text_output_summ)
103
-
104
  demo.launch()
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load models
5
+ # Sentiment Analysis
6
+ classifier_sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
7
+
8
+ def analyze_sentiment(text):
9
+ result = classifier_sentiment(text)[0]
10
+ label = result['label']
11
+ score = result['score']
12
+ return f"Label: {label}, Score: {score:.2f}"
13
+
14
+ # Translation
15
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
16
+
17
+ def translate_text(text):
18
+ result = translator(text)[0]
19
+ translated_text = result["translation_text"]
20
+ return translated_text
21
+
22
+ # Image Classification
23
+ classifier_image = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")
24
+
25
+ def classify_image(image):
26
+ results = classifier_image(image)
27
+ output = ""
28
+ for result in results:
29
+ output += f"{result['label']}: {result['score']:.2f}\n"
30
+ return output
31
+
32
+ # Speech to Text
33
+ speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
34
+
35
+ def transcribe_audio(audio):
36
+ text = speech_to_text(audio)["text"]
37
+ return text
38
+
39
+ # Text Summarization
40
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
41
+
42
+ def summarize_text(text):
43
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
44
+ return summary
45
+
46
+ # Define custom CSS styles
47
+ css = """
48
+ <style>
49
+ body {
50
+ background-color: #e9ecef;
51
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
52
+ }
53
+ .gradio-container {
54
+ border-radius: 15px;
55
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
56
+ padding: 20px;
57
+ background-color: #74748a;
58
+ max-width: 800px;
59
+ margin: auto;
60
+ }
61
+ h1 {
62
+ color: black;
63
+ }
64
+
65
+ </style>
66
+ """
67
+
68
+ with gr.Blocks(css=css) as demo:
69
+ gr.Markdown("<h1 style='text-align: center;'>Multi-functional AI Demo</h1>")
70
+
71
+ with gr.Tab("Sentiment Analysis😣"):
72
+ text_input = gr.Textbox(placeholder="Enter text here...")
73
+ text_output = gr.Textbox()
74
+ sentiment_button = gr.Button("Analyze")
75
+ sentiment_button.click(analyze_sentiment, inputs=text_input, outputs=text_output)
76
+
77
+ with gr.Tab("Translation📚"):
78
+ text_input_trans = gr.Textbox(placeholder="Enter English text here...")
79
+ text_output_trans = gr.Textbox()
80
+ trans_button = gr.Button("Translate")
81
+ trans_button.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
82
+
83
+ with gr.Tab("Image Classification🔮"):
84
+ image_input = gr.Image(type="pil")
85
+ image_output = gr.Textbox()
86
+ image_button = gr.Button("Classify")
87
+ image_button.click(classify_image, inputs=image_input, outputs=image_output)
88
+
89
+ with gr.Tab("Speech to Text🔊"):
90
+ audio_input = gr.Audio(sources=["microphone"], type="filepath")
91
+ audio_output = gr.Textbox()
92
+ audio_button = gr.Button("Transcribe")
93
+ audio_button.click(transcribe_audio, inputs=audio_input, outputs=audio_output)
94
+
95
+ with gr.Tab("Text Summarization📑"):
96
+ text_input_summ = gr.Textbox(placeholder="Enter text here...")
97
+ text_output_summ = gr.Textbox()
98
+ summ_button = gr.Button("Summarize")
99
+ summ_button.click(summarize_text, inputs=text_input_summ, outputs=text_output_summ)
100
+
 
 
 
101
  demo.launch()