Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,101 @@
|
|
| 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 |
-
output
|
| 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 |
-
with gr.
|
| 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 |
-
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()
|