Spaces:
Sleeping
Sleeping
File size: 7,021 Bytes
c6f9c09 |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
import gradio as gr
from transformers import pipeline
# Initialize the models
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
image_captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
# Function for text summarization
def summarize_text(text):
if not text:
return "Please enter some text to summarize."
try:
result = summarizer(text, max_length=130, min_length=30, do_sample=False)
return result[0]['summary_text']
except Exception as e:
return f"Error: {str(e)}"
# Function for image captioning
def caption_image(image):
if image is None:
return "Please upload an image."
try:
result = image_captioner(image)
return result[0]['generated_text']
except Exception as e:
return f"Error: {str(e)}"
# Function for sentiment analysis
def analyze_sentiment(text):
if not text:
return "Please enter some text to analyze."
try:
result = sentiment_analyzer(text)
label = result[0]['label']
score = result[0]['score']
return f"Sentiment: {label}\nConfidence: {score:.2%}"
except Exception as e:
return f"Error: {str(e)}"
# Create the Gradio interface with tabs
with gr.Blocks(title="AI Wizard Toolkit") as demo:
gr.Markdown("""
# ๐งโโ๏ธ AI Wizard Toolkit
Welcome to the AI Wizard Toolkit! This application provides three powerful AI tools:
## ๐ฎ Features:
### ๐ Tab 1: Text Summarizer
- **Model**: facebook/bart-large-cnn
- **Function**: Condense long articles or documents into concise summaries
- **How to use**: Paste your text in the input box and click "Summarize"
### ๐ผ๏ธ Tab 2: Image Captioning
- **Model**: nlpconnect/vit-gpt2-image-captioning
- **Function**: Generate descriptive captions for your images
- **How to use**: Upload an image and click "Generate Caption"
### ๐ Tab 3: Sentiment Analysis
- **Model**: distilbert-base-uncased-finetuned-sst-2-english
- **Function**: Analyze the emotional tone of text (positive or negative)
- **How to use**: Enter your text and click "Analyze Sentiment"
---
Select a tab below to get started!
""")
with gr.Tabs():
# Tab 1: Text Summarization
with gr.Tab("๐ Text Summarizer"):
gr.Markdown("### AI-Powered Text Summarization")
gr.Markdown("Enter a long text and get a concise summary using facebook/bart-large-cnn model.")
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Input Text",
placeholder="Paste your article or long text here...",
lines=10
)
summarize_btn = gr.Button("Summarize", variant="primary")
with gr.Column():
summary_output = gr.Textbox(
label="Summary",
lines=10
)
summarize_btn.click(
fn=summarize_text,
inputs=text_input,
outputs=summary_output
)
gr.Examples(
examples=[
["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)."]
],
inputs=text_input
)
# Tab 2: Image Captioning
with gr.Tab("๐ผ๏ธ Image Captioning"):
gr.Markdown("### AI-Powered Image Captioning")
gr.Markdown("Upload an image and get an AI-generated caption using nlpconnect/vit-gpt2-image-captioning model.")
with gr.Row():
with gr.Column():
image_input = gr.Image(
label="Upload Image",
type="pil"
)
caption_btn = gr.Button("Generate Caption", variant="primary")
with gr.Column():
caption_output = gr.Textbox(
label="Generated Caption",
lines=3
)
caption_btn.click(
fn=caption_image,
inputs=image_input,
outputs=caption_output
)
# Tab 3: Sentiment Analysis
with gr.Tab("๐ Sentiment Analysis"):
gr.Markdown("### AI-Powered Sentiment Analysis")
gr.Markdown("Analyze the sentiment of text using distilbert-base-uncased-finetuned-sst-2-english model.")
with gr.Row():
with gr.Column():
sentiment_input = gr.Textbox(
label="Input Text",
placeholder="Enter text to analyze sentiment...",
lines=5
)
sentiment_btn = gr.Button("Analyze Sentiment", variant="primary")
with gr.Column():
sentiment_output = gr.Textbox(
label="Sentiment Result",
lines=5
)
sentiment_btn.click(
fn=analyze_sentiment,
inputs=sentiment_input,
outputs=sentiment_output
)
gr.Examples(
examples=[
["I love this product! It's absolutely amazing and exceeded all my expectations."],
["This is terrible. I'm very disappointed and would not recommend it to anyone."],
["The weather is nice today."]
],
inputs=sentiment_input
)
gr.Markdown("""
---
### ๐ About the Models:
- **BART-large-CNN**: A transformer model fine-tuned for summarization tasks
- **ViT-GPT2**: Combines Vision Transformer with GPT-2 for image understanding
- **DistilBERT-SST-2**: A distilled version of BERT fine-tuned on sentiment classification
All models are powered by Hugging Face Transformers! ๐ค
""")
if __name__ == "__main__":
demo.launch() |