File size: 2,253 Bytes
9c8c61e 865a19e 9c8c61e 865a19e 9c8c61e |
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 |
# -*- coding: utf-8 -*-
"""Untitled4.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1352Z_3Tsa5_YFTfI-jWhZpSJ_k4yHSm3
"""
# pip install gradio transformers torch
# !pip install gTTS
import gradio as gr
from transformers import pipeline, TextGenerationPipeline, AutoModelForCausalLM, AutoTokenizer
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
import torch
from gtts import gTTS
import tempfile
sentiment_pipeline = pipeline("sentiment-analysis")
summarizer_pipeline = pipeline("summarization")
# Sentiment Analysis
def analyze_sentiment(text):
result = sentiment_pipeline(text)[0]
return result["label"], round(result["score"], 3)
# Summarization
def summarize(text):
summary = summarizer_pipeline(text, max_length=60, min_length=15, do_sample=False)
return summary[0]["summary_text"]
def text_to_speech(text):
tts = gTTS(text)
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as fp:
tts.save(fp.name)
return fp.name
# Gradio UI
with gr.Blocks(title="TrailTrek AI Assistant",theme="soft") as demo:
gr.Markdown("## ๐ง TrailTrek Gears Co - Multi-Task AI Demo")
with gr.Tab("๐ Sentiment Analysis"):
with gr.Row():
text_input = gr.Textbox(label="Enter text")
sentiment_output = gr.Text(label="Sentiment")
confidence_output = gr.Number(label="Confidence")
analyze_btn = gr.Button("Analyze")
analyze_btn.click(analyze_sentiment, inputs=[text_input], outputs=[sentiment_output, confidence_output])
with gr.Tab("๐ Summarization"):
input_text = gr.Textbox(lines=8, label="Enter a long text")
output_summary = gr.Text(label="Summary")
summarize_btn = gr.Button("Summarize")
summarize_btn.click(summarize, inputs=[input_text], outputs=[output_summary])
with gr.Tab("๐ฃ๏ธ Text-to-Speech"):
tts_input = gr.Textbox(label="Enter text to speak")
tts_output = gr.Audio(label="Generated Speech", type="filepath")
tts_btn = gr.Button("Convert to Speech")
tts_btn.click(text_to_speech, inputs=[tts_input], outputs=[tts_output])
demo.launch()
|