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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Initialize the models
|
| 5 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 6 |
+
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
| 7 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 8 |
+
|
| 9 |
+
# Function for text summarization
|
| 10 |
+
def summarize_text(text):
|
| 11 |
+
if not text:
|
| 12 |
+
return "Please enter some text to summarize."
|
| 13 |
+
try:
|
| 14 |
+
result = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 15 |
+
return result[0]['summary_text']
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Error: {str(e)}"
|
| 18 |
+
|
| 19 |
+
# Function for image captioning
|
| 20 |
+
def caption_image(image):
|
| 21 |
+
if image is None:
|
| 22 |
+
return "Please upload an image."
|
| 23 |
+
try:
|
| 24 |
+
result = image_captioner(image)
|
| 25 |
+
return result[0]['generated_text']
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
# Function for sentiment analysis
|
| 30 |
+
def analyze_sentiment(text):
|
| 31 |
+
if not text:
|
| 32 |
+
return "Please enter some text to analyze."
|
| 33 |
+
try:
|
| 34 |
+
result = sentiment_analyzer(text)
|
| 35 |
+
label = result[0]['label']
|
| 36 |
+
score = result[0]['score']
|
| 37 |
+
return f"Sentiment: {label}\nConfidence: {score:.2%}"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error: {str(e)}"
|
| 40 |
+
|
| 41 |
+
# Create the Gradio interface with tabs
|
| 42 |
+
with gr.Blocks(title="AI Wizard Toolkit") as demo:
|
| 43 |
+
gr.Markdown("""
|
| 44 |
+
# ๐งโโ๏ธ AI Wizard Toolkit
|
| 45 |
+
|
| 46 |
+
Welcome to the AI Wizard Toolkit! This application provides three powerful AI tools:
|
| 47 |
+
|
| 48 |
+
## ๐ฎ Features:
|
| 49 |
+
|
| 50 |
+
### ๐ Tab 1: Text Summarizer
|
| 51 |
+
- **Model**: facebook/bart-large-cnn
|
| 52 |
+
- **Function**: Condense long articles or documents into concise summaries
|
| 53 |
+
- **How to use**: Paste your text in the input box and click "Summarize"
|
| 54 |
+
|
| 55 |
+
### ๐ผ๏ธ Tab 2: Image Captioning
|
| 56 |
+
- **Model**: nlpconnect/vit-gpt2-image-captioning
|
| 57 |
+
- **Function**: Generate descriptive captions for your images
|
| 58 |
+
- **How to use**: Upload an image and click "Generate Caption"
|
| 59 |
+
|
| 60 |
+
### ๐ Tab 3: Sentiment Analysis
|
| 61 |
+
- **Model**: distilbert-base-uncased-finetuned-sst-2-english
|
| 62 |
+
- **Function**: Analyze the emotional tone of text (positive or negative)
|
| 63 |
+
- **How to use**: Enter your text and click "Analyze Sentiment"
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
Select a tab below to get started!
|
| 68 |
+
""")
|
| 69 |
+
|
| 70 |
+
with gr.Tabs():
|
| 71 |
+
# Tab 1: Text Summarization
|
| 72 |
+
with gr.Tab("๐ Text Summarizer"):
|
| 73 |
+
gr.Markdown("### AI-Powered Text Summarization")
|
| 74 |
+
gr.Markdown("Enter a long text and get a concise summary using facebook/bart-large-cnn model.")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
with gr.Column():
|
| 78 |
+
text_input = gr.Textbox(
|
| 79 |
+
label="Input Text",
|
| 80 |
+
placeholder="Paste your article or long text here...",
|
| 81 |
+
lines=10
|
| 82 |
+
)
|
| 83 |
+
summarize_btn = gr.Button("Summarize", variant="primary")
|
| 84 |
+
|
| 85 |
+
with gr.Column():
|
| 86 |
+
summary_output = gr.Textbox(
|
| 87 |
+
label="Summary",
|
| 88 |
+
lines=10
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
summarize_btn.click(
|
| 92 |
+
fn=summarize_text,
|
| 93 |
+
inputs=text_input,
|
| 94 |
+
outputs=summary_output
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
gr.Examples(
|
| 98 |
+
examples=[
|
| 99 |
+
["The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft)."]
|
| 100 |
+
],
|
| 101 |
+
inputs=text_input
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Tab 2: Image Captioning
|
| 105 |
+
with gr.Tab("๐ผ๏ธ Image Captioning"):
|
| 106 |
+
gr.Markdown("### AI-Powered Image Captioning")
|
| 107 |
+
gr.Markdown("Upload an image and get an AI-generated caption using nlpconnect/vit-gpt2-image-captioning model.")
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
with gr.Column():
|
| 111 |
+
image_input = gr.Image(
|
| 112 |
+
label="Upload Image",
|
| 113 |
+
type="pil"
|
| 114 |
+
)
|
| 115 |
+
caption_btn = gr.Button("Generate Caption", variant="primary")
|
| 116 |
+
|
| 117 |
+
with gr.Column():
|
| 118 |
+
caption_output = gr.Textbox(
|
| 119 |
+
label="Generated Caption",
|
| 120 |
+
lines=3
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
caption_btn.click(
|
| 124 |
+
fn=caption_image,
|
| 125 |
+
inputs=image_input,
|
| 126 |
+
outputs=caption_output
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Tab 3: Sentiment Analysis
|
| 130 |
+
with gr.Tab("๐ Sentiment Analysis"):
|
| 131 |
+
gr.Markdown("### AI-Powered Sentiment Analysis")
|
| 132 |
+
gr.Markdown("Analyze the sentiment of text using distilbert-base-uncased-finetuned-sst-2-english model.")
|
| 133 |
+
|
| 134 |
+
with gr.Row():
|
| 135 |
+
with gr.Column():
|
| 136 |
+
sentiment_input = gr.Textbox(
|
| 137 |
+
label="Input Text",
|
| 138 |
+
placeholder="Enter text to analyze sentiment...",
|
| 139 |
+
lines=5
|
| 140 |
+
)
|
| 141 |
+
sentiment_btn = gr.Button("Analyze Sentiment", variant="primary")
|
| 142 |
+
|
| 143 |
+
with gr.Column():
|
| 144 |
+
sentiment_output = gr.Textbox(
|
| 145 |
+
label="Sentiment Result",
|
| 146 |
+
lines=5
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
sentiment_btn.click(
|
| 150 |
+
fn=analyze_sentiment,
|
| 151 |
+
inputs=sentiment_input,
|
| 152 |
+
outputs=sentiment_output
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
gr.Examples(
|
| 156 |
+
examples=[
|
| 157 |
+
["I love this product! It's absolutely amazing and exceeded all my expectations."],
|
| 158 |
+
["This is terrible. I'm very disappointed and would not recommend it to anyone."],
|
| 159 |
+
["The weather is nice today."]
|
| 160 |
+
],
|
| 161 |
+
inputs=sentiment_input
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
gr.Markdown("""
|
| 165 |
+
---
|
| 166 |
+
|
| 167 |
+
### ๐ About the Models:
|
| 168 |
+
|
| 169 |
+
- **BART-large-CNN**: A transformer model fine-tuned for summarization tasks
|
| 170 |
+
- **ViT-GPT2**: Combines Vision Transformer with GPT-2 for image understanding
|
| 171 |
+
- **DistilBERT-SST-2**: A distilled version of BERT fine-tuned on sentiment classification
|
| 172 |
+
|
| 173 |
+
All models are powered by Hugging Face Transformers! ๐ค
|
| 174 |
+
""")
|
| 175 |
+
|
| 176 |
+
if __name__ == "__main__":
|
| 177 |
+
demo.launch()
|