File size: 1,244 Bytes
81762da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load the sentiment analysis, keyword extraction, and text summarization models from Hugging Face
sentiment_model = pipeline("sentiment-analysis")
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
keyword_extraction_model = pipeline(
    "text2text-generation", model="transformer3/keywordextractor"
)


# Define the function to be called when text input is provided
def analyze_text(text):
    # Sentiment analysis
    sentiment_result = sentiment_model(text)[0]
    sentiment = sentiment_result["label"]
    sentiment_score = sentiment_result["score"]
    summary = summarizer(text, max_length=130, min_length=30, do_sample=False)

    # Keyword extraction
    keyword_result = keyword_extraction_model(
        f"summarize: {text}", max_length=50, num_return_sequences=1
    )
    keywords = keyword_result[0]

    # # Text summarization
    summary = summarizer(text, max_length=130, min_length=30, do_sample=False)

    return f"Sentiment: {sentiment}, Score: {sentiment_score}\nKeywords: {keywords}\nSummary: {summary}"


# Create the Gradio interface
iface = gr.Interface(fn=analyze_text, inputs="text", outputs="text")

# Launch the interface
iface.launch()