Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# Load sentiment analysis model
|
| 6 |
+
sentiment_pipe = pipeline("sentiment-analysis")
|
| 7 |
+
|
| 8 |
+
# Load summarization model
|
| 9 |
+
summarizer = pipeline("summarization")
|
| 10 |
+
|
| 11 |
+
# Load text-to-speech model
|
| 12 |
+
tts_pipe = pipeline("text-to-speech", model="suno/bark-small")
|
| 13 |
+
|
| 14 |
+
## real work now
|
| 15 |
+
|
| 16 |
+
# Sentiment Analysis Function
|
| 17 |
+
def get_sentiment(input_text):
|
| 18 |
+
analysis = sentiment_pipe(input_text)[0]
|
| 19 |
+
return analysis['label'], str(round(analysis['score'], 4))
|
| 20 |
+
|
| 21 |
+
# Summarization Function
|
| 22 |
+
def summarize_text(input_text):
|
| 23 |
+
summary = summarizer(input_text, max_length=150, min_length=30, do_sample=False)[0]
|
| 24 |
+
return summary['summary_text']
|
| 25 |
+
|
| 26 |
+
# Text-to-Speech Function
|
| 27 |
+
def text_to_speech(input_text):
|
| 28 |
+
speech = tts_pipe(input_text)
|
| 29 |
+
return speech["path"]
|
| 30 |
+
|
| 31 |
+
# Chatbot Logic
|
| 32 |
+
def chat(message, history):
|
| 33 |
+
history = history or []
|
| 34 |
+
if message.startswith("How many"):
|
| 35 |
+
response = str(random.randint(1, 10))
|
| 36 |
+
elif message.startswith("How"):
|
| 37 |
+
response = random.choice(["Great", "Good", "Okay", "Bad"])
|
| 38 |
+
elif message.startswith("Where"):
|
| 39 |
+
response = random.choice(["Here", "There", "Somewhere"])
|
| 40 |
+
else:
|
| 41 |
+
response = "I don't know"
|
| 42 |
+
history.append((message, response))
|
| 43 |
+
return history, history
|
| 44 |
+
|
| 45 |
+
# Create Gradio Interface
|
| 46 |
+
with gr.Blocks(title="TrailTrek AI Suite") as demo:
|
| 47 |
+
gr.Markdown("# TrailTrek Gears Co. AI Prototype")
|
| 48 |
+
|
| 49 |
+
with gr.Tabs():
|
| 50 |
+
# Sentiment Analysis Tab
|
| 51 |
+
with gr.Tab("Sentiment Analysis"):
|
| 52 |
+
gr.Markdown("## Analyze Text Sentiment")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
text_input = gr.Textbox(label="Input Text")
|
| 55 |
+
with gr.Column():
|
| 56 |
+
sentiment_label = gr.Textbox(label="Sentiment")
|
| 57 |
+
score_output = gr.Textbox(label="Confidence Score")
|
| 58 |
+
analyze_btn = gr.Button("Analyze")
|
| 59 |
+
analyze_btn.click(
|
| 60 |
+
fn=get_sentiment,
|
| 61 |
+
inputs=text_input,
|
| 62 |
+
outputs=[sentiment_label, score_output]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Chatbot Tab
|
| 66 |
+
with gr.Tab("Chatbot"):
|
| 67 |
+
gr.Markdown("## Interactive Chat")
|
| 68 |
+
chatbot = gr.Chatbot()
|
| 69 |
+
msg = gr.Textbox(label="Your Message")
|
| 70 |
+
clear = gr.Button("Clear")
|
| 71 |
+
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
| 72 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 73 |
+
|
| 74 |
+
# Summarization Tab
|
| 75 |
+
with gr.Tab("Summarization"):
|
| 76 |
+
gr.Markdown("## Text Summarization")
|
| 77 |
+
with gr.Row():
|
| 78 |
+
long_text = gr.Textbox(label="Input Text", lines=5)
|
| 79 |
+
summary = gr.Textbox(label="Summary", lines=5)
|
| 80 |
+
summarize_btn = gr.Button("Summarize")
|
| 81 |
+
summarize_btn.click(
|
| 82 |
+
fn=summarize_text,
|
| 83 |
+
inputs=long_text,
|
| 84 |
+
outputs=summary
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Text-to-Speech Tab
|
| 88 |
+
with gr.Tab("Text-to-Speech"):
|
| 89 |
+
gr.Markdown("## Web Accessibility Prototype")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
tts_input = gr.Textbox(label="Enter Text")
|
| 92 |
+
tts_output = gr.Audio(label="Generated Speech")
|
| 93 |
+
tts_btn = gr.Button("Convert to Speech")
|
| 94 |
+
tts_btn.click(
|
| 95 |
+
fn=text_to_speech,
|
| 96 |
+
inputs=tts_input,
|
| 97 |
+
outputs=tts_output
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# Launch the Gradio app
|
| 102 |
+
demo.launch()
|