Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- ML3/app.py +104 -0
- ML3/requirements.txt +2 -0
ML3/app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
ML3/requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|