File size: 3,504 Bytes
03b2869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Install necessary libraries
!pip install transformers gradio librosa

import gradio as gr
from transformers import pipeline

# Load models
# Sentiment Analysis
classifier_sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

def analyze_sentiment(text):
    result = classifier_sentiment(text)[0]
    label = result['label']
    score = result['score']
    return f"Label: {label}, Score: {score:.2f}"

# Translation
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")

def translate_text(text):
    result = translator(text)[0]
    translated_text = result["translation_text"]
    return translated_text

# Image Classification
classifier_image = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")

def classify_image(image):
    results = classifier_image(image)
    output = ""
    for result in results:
        output += f"{result['label']}: {result['score']:.2f}\n"
    return output

# Speech to Text
speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")

def transcribe_audio(audio):
    text = speech_to_text(audio)["text"]
    return text

# Text Summarization
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def summarize_text(text):
    summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
    return summary

# Define custom CSS styles
css = """

<style>

    body {

        background-color: #e9ecef;

        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

    }

    .gradio-container {

        border-radius: 15px;

        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);

        padding: 20px;

        background-color: #74748a;

        max-width: 800px;

        margin: auto;

    }

    h1 {

        color: black;

    }

    

</style>

"""

with gr.Blocks(css=css) as demo:
    gr.Markdown("<h1 style='text-align: center;'>Multi-functional AI Demo</h1>")

    with gr.Tab("Sentiment Analysis😣"):
        text_input = gr.Textbox(placeholder="Enter text here...")
        text_output = gr.Textbox()
        sentiment_button = gr.Button("Analyze")
        sentiment_button.click(analyze_sentiment, inputs=text_input, outputs=text_output)

    with gr.Tab("Translation📚"):
        text_input_trans = gr.Textbox(placeholder="Enter English text here...")
        text_output_trans = gr.Textbox()
        trans_button = gr.Button("Translate")
        trans_button.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)

    with gr.Tab("Image Classification🔮"):
        image_input = gr.Image(type="pil")
        image_output = gr.Textbox()
        image_button = gr.Button("Classify")
        image_button.click(classify_image, inputs=image_input, outputs=image_output)

    with gr.Tab("Speech to Text🔊"):
        audio_input = gr.Audio(sources=["microphone"], type="filepath")
        audio_output = gr.Textbox()
        audio_button = gr.Button("Transcribe")
        audio_button.click(transcribe_audio, inputs=audio_input, outputs=audio_output)
    
    with gr.Tab("Text Summarization📑"):
        text_input_summ = gr.Textbox(placeholder="Enter text here...")
        text_output_summ = gr.Textbox()
        summ_button = gr.Button("Summarize")
        summ_button.click(summarize_text, inputs=text_input_summ, outputs=text_output_summ)

demo.launch()