Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr #importing the necessary libraries
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
sentiment_pipeline = pipeline("sentiment-analysis") #importing the model that we will use to work at, as explained in the output terminal
|
| 5 |
+
chatbot_pipeline = pipeline("text-generation")
|
| 6 |
+
summarization_pipeline = pipeline("summarization", model="t5-small") # T5 for summarization
|
| 7 |
+
|
| 8 |
+
# Sentiment Analysis function
|
| 9 |
+
def analyze_sentiment(text):
|
| 10 |
+
result = sentiment_pipeline(text)[0]
|
| 11 |
+
return result["label"], round(result["score"], 2)
|
| 12 |
+
|
| 13 |
+
def generate_response(user_input):
|
| 14 |
+
response = chatbot_pipeline(user_input, max_length=100, num_return_sequences=1)
|
| 15 |
+
return response[0]['generated_text']
|
| 16 |
+
|
| 17 |
+
# Summarization function
|
| 18 |
+
def summarize_text(long_text):
|
| 19 |
+
summary = summarization_pipeline(long_text, max_length=50, min_length=25, do_sample=False)
|
| 20 |
+
return summary[0]["summary_text"]
|
| 21 |
+
|
| 22 |
+
# Speech-to-Text function
|
| 23 |
+
def text_to_speech(text):
|
| 24 |
+
from gtts import gTTS
|
| 25 |
+
import os
|
| 26 |
+
tts = gTTS(text)
|
| 27 |
+
tts.save("output.mp3")
|
| 28 |
+
return "output.mp3"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("# Multi-Task AI App with Alternative Models")
|
| 33 |
+
with gr.Tabs():
|
| 34 |
+
# tab for Sentiment Analysis
|
| 35 |
+
with gr.Tab("Sentiment Analysis"):
|
| 36 |
+
gr.Markdown("### Sentiment Analysis")
|
| 37 |
+
sentiment_input = gr.Textbox(label="Enter Text for Sentiment Analysis")
|
| 38 |
+
sentiment_output_label = gr.Textbox(label="Sentiment")
|
| 39 |
+
sentiment_output_score = gr.Textbox(label="Confidence Score")
|
| 40 |
+
gr.Button("Analyze").click(analyze_sentiment, inputs=sentiment_input, outputs=[sentiment_output_label, sentiment_output_score])
|
| 41 |
+
|
| 42 |
+
# tab for chatbot
|
| 43 |
+
with gr.Tab("Chatbot"):
|
| 44 |
+
gr.Markdown("### Chatbot")
|
| 45 |
+
chatbot_input = gr.Textbox(label="Enter Your Message")
|
| 46 |
+
chatbot_output = gr.Textbox(label="Chatbot Response", lines=5)
|
| 47 |
+
gr.Button("Send").click(chatbot_response, inputs=chatbot_input, outputs=chatbot_output)
|
| 48 |
+
|
| 49 |
+
# tab for summarization
|
| 50 |
+
with gr.Tab("Summarization"):
|
| 51 |
+
gr.Markdown("### Summarization")
|
| 52 |
+
summarization_input = gr.Textbox(label="Enter Long Text for Summarization", lines=5)
|
| 53 |
+
summarization_output = gr.Textbox(label="Summary", lines=5)
|
| 54 |
+
gr.Button("Summarize").click(summarize_text, inputs=summarization_input, outputs=summarization_output)
|
| 55 |
+
|
| 56 |
+
# tab for STT
|
| 57 |
+
with gr.Tab("Speech-to-Text"):
|
| 58 |
+
gr.Markdown("### Text-to-Speech")
|
| 59 |
+
tts_input = gr.Textbox(label="Enter Text for Text-to-Speech")
|
| 60 |
+
tts_output = gr.Audio(label="Generated Speech")
|
| 61 |
+
gr.Button("Convert to Speech").click(text_to_speech, inputs=tts_input, outputs=tts_output)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
demo.launch()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|