TaifAlharbi's picture
Update app.py
865a19e verified
# -*- 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()