import gradio as gr from transformers import pipeline sentiment_model = pipeline("sentiment-analysis") chatbot_model = pipeline("text-generation", model="microsoft/DialoGPT-medium") summarization_model = pipeline("summarization") text_to_speech_model = pipeline("text-to-speech") def get_sentiment(input_text): analysis = sentiment_model(input_text) sent = analysis[0]['label'] score = analysis[0]['score'] return sent, score def chatbot_response(input_text): response = chatbot_model(input_text, max_length=100, do_sample=True)[0]['generated_text'] return response def summarize_text(input_text): summary = summarization_model(input_text, max_length=100, min_length=30, do_sample=False) return summary[0]['summary_text'] def text_to_speech(input_text): audio = text_to_speech_model(input_text) return audio with gr.Blocks() as demo: gr.Markdown("## Multi-Function AI Language Application") with gr.Tab("Sentiment Analysis"): text_input = gr.Textbox(label="Enter text for sentiment analysis:") sentiment_output = gr.Textbox(label="Sentiment") score_output = gr.Number(label="Confidence Score") sentiment_button = gr.Button("Analyze") sentiment_button.click(get_sentiment, inputs=text_input, outputs=[sentiment_output, score_output]) with gr.Tab("Chatbot"): chat_input = gr.Textbox(label="Enter your message:") chat_output = gr.Textbox(label="Chatbot Response") chat_button = gr.Button("Send") chat_button.click(chatbot_response, inputs=chat_input, outputs=chat_output) with gr.Tab("Summarization"): summary_input = gr.Textbox(label="Enter text to summarize:", lines=5) summary_output = gr.Textbox(label="Summary") summary_button = gr.Button("Summarize") summary_button.click(summarize_text, inputs=summary_input, outputs=summary_output) with gr.Tab("Text-to-Speech"): tts_input = gr.Textbox(label="Enter text to convert to speech:") tts_output = gr.Audio(label="Generated Speech") tts_button = gr.Button("Convert") tts_button.click(text_to_speech, inputs=tts_input, outputs=tts_output) # Launch the app demo.launch()