shuhayas's picture
Upload folder using huggingface_hub
c99e39f verified
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
# Custom CSS
custom_css = """
.container {
max-width: 900px;
margin: auto;
padding: 20px;
}
.gr-header {
text-align: center;
margin-bottom: 40px;
}
.gr-header h1 {
color: #2a2a2a;
font-size: 2.5em;
margin-bottom: 10px;
}
.gr-header p {
color: #666;
font-size: 1.2em;
}
.gr-tab-text {
font-size: 1.1em;
line-height: 1.6;
}
.gr-button {
background-color: #2e7af5 !important;
color: white !important;
border: none !important;
border-radius: 8px !important;
padding: 10px 20px !important;
transition: all 0.3s ease !important;
}
.gr-button:hover {
background-color: #1c5ed9 !important;
transform: translateY(-2px);
}
.gr-input {
border-radius: 8px !important;
border: 2px solid #e0e0e0 !important;
padding: 12px !important;
}
.gr-input:focus {
border-color: #2e7af5 !important;
box-shadow: 0 0 0 2px rgba(46,122,245,0.2) !important;
}
.gr-box {
border-radius: 12px !important;
box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
padding: 20px !important;
background: white !important;
}
"""
# Application 1: Image Classification
def image_classification(image):
if image is None:
return "Please upload an image.", None
# Get basic image information
info = f"""
Image Analysis Results:
- Size: {image.shape}
- Color Space: RGB
- Resolution: {image.shape[0]}x{image.shape[1]} pixels
"""
# Generate thumbnail
thumbnail = Image.fromarray(image).copy()
thumbnail.thumbnail((200, 200))
return info, thumbnail
# Application 2: Text Summary
def text_summary(text):
if not text:
return "Please enter text to summarize."
# Analyze basic text information
char_count = len(text)
word_count = len(text.split())
line_count = len(text.splitlines())
summary = f"""
Text Analysis Results:
- Character Count: {char_count}
- Word Count: {word_count}
- Line Count: {line_count}
Summary:
{text[:200] + '...' if len(text) > 200 else text}
"""
return summary
# Application 3: Data Visualization
def visualize_data(x_input, y_input):
try:
x = [float(i.strip()) for i in x_input.split(',')]
y = [float(i.strip()) for i in y_input.split(',')]
if len(x) != len(y):
return None, "Error: Number of X and Y coordinates must match."
fig = plt.figure(figsize=(10, 6))
plt.style.use('seaborn')
plt.plot(x, y, 'o-', linewidth=2, markersize=8, color='#2e7af5')
plt.title('Data Visualization', fontsize=16, pad=20)
plt.xlabel('X Axis', fontsize=12)
plt.ylabel('Y Axis', fontsize=12)
plt.grid(True, linestyle='--', alpha=0.7)
# Improve graph appearance
plt.tick_params(axis='both', which='major', labelsize=10)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
return fig, "Graph generated successfully."
except ValueError:
return None, "Error: Invalid input. Please enter numbers separated by commas."
# Build Gradio interface
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as portfolio:
# Header
with gr.Column(elem_classes="container"):
gr.Markdown(
"""
# Portfolio Site
Welcome to my portfolio site where you can experience various applications I've developed.
Try out different applications in each tab.
""",
elem_classes="gr-header"
)
# Tab container
with gr.Tabs():
# Image Classification Tab
with gr.TabItem("📷 Image Classification", elem_classes="gr-tab"):
with gr.Column(elem_classes="gr-box"):
gr.Markdown(
"""
### Image Classification App
Upload an image to analyze its features. Higher quality images will yield
more accurate analysis results.
""",
elem_classes="gr-tab-text"
)
with gr.Row():
with gr.Column():
image_input = gr.Image(
label="Upload Image",
type="numpy",
elem_classes="gr-input"
)
image_button = gr.Button(
"Analyze",
elem_classes="gr-button"
)
with gr.Column():
image_output = gr.Textbox(
label="Analysis Results",
elem_classes="gr-input"
)
image_thumbnail = gr.Image(
label="Thumbnail",
elem_classes="gr-input"
)
image_button.click(
image_classification,
inputs=image_input,
outputs=[image_output, image_thumbnail]
)
# Text Summary Tab
with gr.TabItem("📝 Text Summary", elem_classes="gr-tab"):
with gr.Column(elem_classes="gr-box"):
gr.Markdown(
"""
### Text Summary App
Enter text to generate a summary and basic statistics.
Works with both English and other languages.
""",
elem_classes="gr-tab-text"
)
text_input = gr.Textbox(
label="Enter Text",
placeholder="Enter the text you want to summarize...",
lines=5,
elem_classes="gr-input"
)
text_button = gr.Button(
"Summarize",
elem_classes="gr-button"
)
text_output = gr.Textbox(
label="Summary Results",
elem_classes="gr-input"
)
text_button.click(
text_summary,
inputs=text_input,
outputs=text_output
)
# Data Visualization Tab
with gr.TabItem("📊 Data Visualization", elem_classes="gr-tab"):
with gr.Column(elem_classes="gr-box"):
gr.Markdown(
"""
### Data Visualization Tool
Enter X and Y coordinates to generate a beautiful graph.
Separate numbers with commas.
Example:
- X coordinates: 1, 2, 3, 4, 5
- Y coordinates: 10, 20, 15, 25, 30
""",
elem_classes="gr-tab-text"
)
with gr.Row():
x_input = gr.Textbox(
label="X Coordinates",
placeholder="Example: 1, 2, 3, 4, 5",
value="1, 2, 3, 4, 5",
elem_classes="gr-input"
)
y_input = gr.Textbox(
label="Y Coordinates",
placeholder="Example: 10, 20, 30, 40, 50",
value="10, 20, 30, 40, 50",
elem_classes="gr-input"
)
plot_button = gr.Button(
"Generate Graph",
elem_classes="gr-button"
)
with gr.Row():
plot_output = gr.Plot(label="Graph")
plot_message = gr.Textbox(
label="Message",
elem_classes="gr-input"
)
plot_button.click(
visualize_data,
inputs=[x_input, y_input],
outputs=[plot_output, plot_message]
)
# Project Information Tab
with gr.TabItem("ℹ️ Project Information", elem_classes="gr-tab"):
with gr.Column(elem_classes="gr-box"):
gr.Markdown(
"""
### About the Project
This portfolio site integrates multiple applications built using Python's powerful
Gradio library.
#### Features
1. **Image Classification App** 🖼️
- Upload and analyze images
- Display basic image information
- Thumbnail generation
2. **Text Summary App** 📚
- Basic text statistics
- Text summarization
- Multi-language support
3. **Data Visualization Tool** 📈
- Interactive graph generation
- Beautiful data visualization
- Customizable plots
#### Tech Stack
- Python 3.8+
- Gradio 4.0+
- Matplotlib 3.7+
- NumPy 1.24+
- Pillow 10.0+
#### Source Code
The source code for this project is available on [GitHub](https://github.com/yourusername/portfolio)
#### Contact
For questions or feedback, please reach out:
- Email: your.email@example.com
- Twitter: @yourusername
- LinkedIn: [Your Name](https://linkedin.com/in/yourprofile)
""",
elem_classes="gr-tab-text"
)
# Launch application
if __name__ == "__main__":
portfolio.launch(share=True)