File size: 2,365 Bytes
31b4996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Sentiment Analysis Model
sentiment_analyzer = pipeline("sentiment-analysis")

# Chatbot Model
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")

# Summarization Model
summarizer = pipeline("summarization")

# Sentiment Analysis Function
def analyze_sentiment(text):
    result = sentiment_analyzer(text)[0]
    sentiment = result['label']
    confidence = round(result['score'], 4)
    return sentiment, confidence

# Chatbot Function
def generate_response(user_input, history=[]):
    history.append(user_input)
    input_text = " ".join(history)
    response = chatbot(input_text, max_length=1000, pad_token_id=50256)
    bot_reply = response[0]['generated_text'][len(input_text):]
    history.append(bot_reply)
    return history, history

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

    with gr.Blocks() as demo:
    gr.Markdown("# Multi-Function Language Application")

    with gr.Tabs():
        with gr.TabItem("Sentiment Analysis"):
            gr.Markdown("## Enter text for sentiment analysis:")
            sentiment_input = gr.Textbox(placeholder="Type your text here...")
            sentiment_button = gr.Button("Analyze")
            sentiment_output = gr.Textbox(label="Sentiment")
            confidence_output = gr.Textbox(label="Confidence Score")
            sentiment_button.click(analyze_sentiment, inputs=sentiment_input, outputs=[sentiment_output, confidence_output])

        with gr.TabItem("Chatbot"):
            gr.Markdown("## Chat with the bot:")
            chatbot_input = gr.Textbox(placeholder="Type your message here...")
            chatbot_button = gr.Button("Send")
            chatbot_output = gr.Chatbot()
            chatbot_state = gr.State([])
            chatbot_button.click(generate_response, inputs=[chatbot_input, chatbot_state], outputs=[chatbot_output, chatbot_state])

        with gr.TabItem("Summarization"):
            gr.Markdown("## Enter text to summarize:")
            summary_input = gr.Textbox(placeholder="Paste your text here...")
            summary_button = gr.Button("Summarize")
            summary_output = gr.Textbox(label="Summary")
            summary_button.click(summarize_text, inputs=summary_input, outputs=summary_output)

demo.launch()