File size: 3,374 Bytes
ec72e84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
from transformers import pipeline
import random

# Load sentiment analysis model
sentiment_pipe = pipeline("sentiment-analysis")

# Load summarization model
summarizer = pipeline("summarization")

# Load text-to-speech model
tts_pipe = pipeline("text-to-speech", model="suno/bark-small")

## real work now

# Sentiment Analysis Function
def get_sentiment(input_text):
    analysis = sentiment_pipe(input_text)[0]
    return analysis['label'], str(round(analysis['score'], 4))

# Summarization Function
def summarize_text(input_text):
    summary = summarizer(input_text, max_length=150, min_length=30, do_sample=False)[0]
    return summary['summary_text']

# Text-to-Speech Function
def text_to_speech(input_text):
    speech = tts_pipe(input_text)
    return speech["path"]

# Chatbot Logic
def chat(message, history):
    history = history or []
    if message.startswith("How many"):
        response = str(random.randint(1, 10))
    elif message.startswith("How"):
        response = random.choice(["Great", "Good", "Okay", "Bad"])
    elif message.startswith("Where"):
        response = random.choice(["Here", "There", "Somewhere"])
    else:
        response = "I don't know"
    history.append((message, response))
    return history, history

# Create Gradio Interface
with gr.Blocks(title="TrailTrek AI Suite") as demo:
    gr.Markdown("# TrailTrek Gears Co. AI Prototype")

    with gr.Tabs():
        # Sentiment Analysis Tab
        with gr.Tab("Sentiment Analysis"):
            gr.Markdown("## Analyze Text Sentiment")
            with gr.Row():
                text_input = gr.Textbox(label="Input Text")
                with gr.Column():
                    sentiment_label = gr.Textbox(label="Sentiment")
                    score_output = gr.Textbox(label="Confidence Score")
            analyze_btn = gr.Button("Analyze")
            analyze_btn.click(
                fn=get_sentiment,
                inputs=text_input,
                outputs=[sentiment_label, score_output]
            )

        # Chatbot Tab
        with gr.Tab("Chatbot"):
            gr.Markdown("## Interactive Chat")
            chatbot = gr.Chatbot()
            msg = gr.Textbox(label="Your Message")
            clear = gr.Button("Clear")
            msg.submit(chat, [msg, chatbot], [chatbot, msg])
            clear.click(lambda: None, None, chatbot, queue=False)

        # Summarization Tab
        with gr.Tab("Summarization"):
            gr.Markdown("## Text Summarization")
            with gr.Row():
                long_text = gr.Textbox(label="Input Text", lines=5)
                summary = gr.Textbox(label="Summary", lines=5)
            summarize_btn = gr.Button("Summarize")
            summarize_btn.click(
                fn=summarize_text,
                inputs=long_text,
                outputs=summary
            )

        # Text-to-Speech Tab
        with gr.Tab("Text-to-Speech"):
            gr.Markdown("## Web Accessibility Prototype")
            with gr.Row():
                tts_input = gr.Textbox(label="Enter Text")
                tts_output = gr.Audio(label="Generated Speech")
            tts_btn = gr.Button("Convert to Speech")
            tts_btn.click(
                fn=text_to_speech,
                inputs=tts_input,
                outputs=tts_output
            )


# Launch the Gradio app
demo.launch()