Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled4.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1352Z_3Tsa5_YFTfI-jWhZpSJ_k4yHSm3
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
pip install gradio transformers torch
|
| 11 |
+
|
| 12 |
+
!pip install gTTS
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
+
from transformers import pipeline, TextGenerationPipeline, AutoModelForCausalLM, AutoTokenizer
|
| 16 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 17 |
+
import torch
|
| 18 |
+
from gtts import gTTS
|
| 19 |
+
import tempfile
|
| 20 |
+
|
| 21 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 22 |
+
summarizer_pipeline = pipeline("summarization")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Sentiment Analysis
|
| 26 |
+
def analyze_sentiment(text):
|
| 27 |
+
result = sentiment_pipeline(text)[0]
|
| 28 |
+
return result["label"], round(result["score"], 3)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Summarization
|
| 32 |
+
def summarize(text):
|
| 33 |
+
summary = summarizer_pipeline(text, max_length=60, min_length=15, do_sample=False)
|
| 34 |
+
return summary[0]["summary_text"]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def text_to_speech(text):
|
| 38 |
+
tts = gTTS(text)
|
| 39 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as fp:
|
| 40 |
+
tts.save(fp.name)
|
| 41 |
+
return fp.name
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Gradio UI
|
| 45 |
+
with gr.Blocks(title="TrailTrek AI Assistant",theme="soft") as demo:
|
| 46 |
+
gr.Markdown("## 🧠 TrailTrek Gears Co - Multi-Task AI Demo")
|
| 47 |
+
|
| 48 |
+
with gr.Tab("📊 Sentiment Analysis"):
|
| 49 |
+
with gr.Row():
|
| 50 |
+
text_input = gr.Textbox(label="Enter text")
|
| 51 |
+
sentiment_output = gr.Text(label="Sentiment")
|
| 52 |
+
confidence_output = gr.Number(label="Confidence")
|
| 53 |
+
analyze_btn = gr.Button("Analyze")
|
| 54 |
+
analyze_btn.click(analyze_sentiment, inputs=[text_input], outputs=[sentiment_output, confidence_output])
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
with gr.Tab("📄 Summarization"):
|
| 58 |
+
input_text = gr.Textbox(lines=8, label="Enter a long text")
|
| 59 |
+
output_summary = gr.Text(label="Summary")
|
| 60 |
+
summarize_btn = gr.Button("Summarize")
|
| 61 |
+
summarize_btn.click(summarize, inputs=[input_text], outputs=[output_summary])
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
with gr.Tab("🗣️ Text-to-Speech"):
|
| 65 |
+
tts_input = gr.Textbox(label="Enter text to speak")
|
| 66 |
+
tts_output = gr.Audio(label="Generated Speech", type="filepath")
|
| 67 |
+
tts_btn = gr.Button("Convert to Speech")
|
| 68 |
+
tts_btn.click(text_to_speech, inputs=[tts_input], outputs=[tts_output])
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
demo.launch()
|
| 73 |
+
|