CV / app.py
limitedonly41's picture
Update app.py
36a4738 verified
import gradio as gr
import pandas as pd
import numpy as np
import io
import json
import time
import random
from datetime import datetime
import plotly.graph_objects as go
import plotly.express as px
import spaces
# Mock functions to simulate the FastAPI web scraping project
class WebScrapingSimulator:
def __init__(self):
self.demo_results = {
"https://example.com": "OTHER",
"https://news.bbc.com": "NEWS/BLOG",
"https://github.com": "OTHER",
"https://stackoverflow.com": "OTHER",
"https://amazon.com": "E-COMMERCE",
"https://techcrunch.com": "NEWS/BLOG",
"https://shopify.com": "E-COMMERCE",
"https://python.org": "OTHER"
}
def simulate_scraping(self, urls_text, progress=gr.Progress()):
if not urls_text.strip():
return "Please enter at least one URL", None, ""
urls = [url.strip() for url in urls_text.split('\n') if url.strip()]
if not urls:
return "Please enter valid URLs", None, ""
results = {}
progress_bar = progress.tqdm(urls, desc="Processing URLs")
for url in progress_bar:
time.sleep(1)
if url in self.demo_results:
classification = self.demo_results[url]
else:
classification = random.choice(["OTHER", "NEWS/BLOG", "E-COMMERCE"])
results[url] = {
"url": url,
"classification": classification,
"confidence": round(random.uniform(0.75, 0.99), 2),
"processed_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
# Format results for display
results_text = "Classification Results:\n\n"
for url, data in results.items():
results_text += f"URL: {url}\n"
results_text += f"Classification: {data['classification']}\n"
results_text += f"Confidence: {data['confidence']}\n"
results_text += f"Processed: {data['processed_at']}\n"
results_text += "-" * 50 + "\n"
# # βœ… Create in-memory JSON file for download
# json_bytes = json.dumps(results, indent=2).encode('utf-8')
# file_obj = io.BytesIO(json_bytes)
# file_obj.name = "scraping_results.json" # optional but helpful
# return results_text, ("scraping_results.json", json_bytes), f"Processed {len(results)} URLs successfully!"
return results_text, f"Processed {len(results)} URLs successfully!"
# Computer Vision simulator
@spaces.GPU
def simulate_cv_processing(image, model_type):
"""Simulate computer vision processing"""
if image is None:
return "Please upload an image", ""
time.sleep(2) # Simulate processing
if model_type == "Face Detection":
result = "Detected 2 faces in the image\nConfidence scores: 0.94, 0.87\nBounding boxes: [(120,150,200,240), (300,180,380,270)]"
elif model_type == "Object Detection":
result = "Detected objects:\n- Person (confidence: 0.91)\n- Car (confidence: 0.84)\n- Tree (confidence: 0.76)"
else: # Image Classification
result = "Classification results:\n- Dog: 85.3%\n- Golden Retriever: 12.1%\n- Pet: 2.6%"
status = f"βœ… Image processed successfully using {model_type}"
return result, status
# Text analysis simulator
@spaces.GPU
def simulate_text_analysis(text, analysis_type):
"""Simulate text analysis"""
if not text.strip():
return "Please enter some text to analyze"
time.sleep(1) # Simulate processing
if analysis_type == "Sentiment Analysis":
sentiment = random.choice(["Positive", "Negative", "Neutral"])
confidence = round(random.uniform(0.7, 0.95), 2)
return f"Sentiment: {sentiment}\nConfidence: {confidence}\nText length: {len(text)} characters"
elif analysis_type == "Summarization":
# Simple extractive summarization simulation
sentences = text.split('.')
summary = '. '.join(sentences[:2]) + '.' if len(sentences) > 1 else text[:100] + "..."
return f"Summary:\n{summary}\n\nOriginal length: {len(text)} chars\nSummary length: {len(summary)} chars"
else: # Named Entity Recognition
entities = ["OpenAI (ORG)", "San Francisco (LOC)", "2023 (DATE)", "AI (TECH)"]
return f"Named Entities Found:\n" + "\n".join([f"- {entity}" for entity in entities])
# Data visualization functions
def create_sample_chart(chart_type, data_source="sample"):
"""Create sample charts"""
if data_source == "sample":
# Use built-in sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [4000, 3000, 2000, 2780, 1890, 2390]
visitors = [2400, 1398, 9800, 3908, 4800, 3800]
if chart_type == "Line Chart":
fig = go.Figure()
fig.add_trace(go.Scatter(x=months, y=sales, name='Sales', mode='lines+markers'))
fig.add_trace(go.Scatter(x=months, y=visitors, name='Visitors', mode='lines+markers'))
fig.update_layout(title="Sales & Visitors Trend", xaxis_title="Month", yaxis_title="Count")
return fig
elif chart_type == "Bar Chart":
fig = go.Figure(data=[go.Bar(x=months, y=sales, name='Sales')])
fig.update_layout(title="Monthly Sales", xaxis_title="Month", yaxis_title="Sales")
return fig
else: # Pie Chart
fig = go.Figure(data=[go.Pie(labels=months, values=sales)])
fig.update_layout(title="Sales Distribution by Month")
return fig
return go.Figure()
def process_uploaded_csv(file):
"""Process uploaded CSV file"""
if file is None:
return "Please upload a CSV file", go.Figure()
try:
df = pd.read_csv(file.name)
preview = df.head().to_string()
# Create a simple visualization
if len(df.columns) >= 2:
fig = px.scatter(df, x=df.columns[0], y=df.columns[1],
title=f"{df.columns[1]} vs {df.columns[0]}")
else:
fig = go.Figure()
return f"CSV loaded successfully!\n\nShape: {df.shape}\nColumns: {list(df.columns)}\n\nPreview:\n{preview}", fig
except Exception as e:
return f"Error processing CSV: {str(e)}", go.Figure()
# Initialize the web scraping simulator
scraper = WebScrapingSimulator()
# Custom CSS
custom_css = """
/* Custom styling for the portfolio */
.gradio-container {
max-width: 1200px !important;
margin: auto !important;
}
.tab-nav {
background: linear-gradient(90deg, #1e3a8a 0%, #3b82f6 100%);
padding: 1rem;
border-radius: 10px;
margin-bottom: 2rem;
}
.project-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 2rem;
border-radius: 15px;
text-align: center;
margin-bottom: 2rem;
}
.tech-badge {
background: #e0f2fe;
color: #01579b;
padding: 0.25rem 0.75rem;
border-radius: 20px;
font-size: 0.875rem;
font-weight: 500;
margin: 0.25rem;
display: inline-block;
}
.project-card {
background: white;
border: 1px solid #e5e7eb;
border-radius: 10px;
padding: 1.5rem;
margin: 1rem 0;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.status-success {
color: #059669;
font-weight: 500;
}
.status-error {
color: #dc2626;
font-weight: 500;
}
"""
# Create the main application
def create_portfolio_app():
with gr.Blocks(css=custom_css, title="AI/ML Developer Portfolio", theme=gr.themes.Soft()) as app:
gr.HTML("""
<div class="project-header">
<h1>πŸ€– AI/ML Developer Portfolio</h1>
<p>Interactive showcase of machine learning and data science projects</p>
<p><strong>Specializing in:</strong> Web Scraping β€’ Computer Vision β€’ NLP β€’ MLOps</p>
</div>
""")
with gr.Tabs() as tabs:
# Home & About Tab
with gr.Tab("🏠 Home & About"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("""
## About Me
I'm a passionate **Python Developer & Data Scientist** based in **Simferopol, Crimea**,
specializing in building production-ready AI/ML systems. This interactive portfolio
showcases real projects that you can explore and run directly in your browser.
### πŸ”§ Core Technologies
""")
gr.HTML("""
<div style="margin: 1rem 0;">
<span class="tech-badge">Python</span>
<span class="tech-badge">FastAPI</span>
<span class="tech-badge">Gradio</span>
<span class="tech-badge">Docker</span>
<span class="tech-badge">Redis</span>
<span class="tech-badge">PostgreSQL</span>
<span class="tech-badge">PyTorch</span>
<span class="tech-badge">Hugging Face</span>
<span class="tech-badge">OpenCV</span>
<span class="tech-badge">BeautifulSoup</span>
</div>
""")
gr.Markdown("""
### πŸ“š Featured Projects
**πŸ•·οΈ Web Scraping & Classification System**
- Async web scraping with content classification using fine-tuned Mistral 7B
- Redis-based task management and progress tracking
- Docker containerized deployment
**πŸ‘οΈ Computer Vision Applications**
- Real-time face recognition with InsightFace
- RTSP camera stream processing
- CUDA-optimized performance
**🧠 LLM Fine-tuning & Deployment**
- Custom model training with Unsloth framework
- API deployment on Modal.com and Hugging Face Spaces
- Batch processing and inference optimization
""")
with gr.Column(scale=1):
gr.Markdown("""
### πŸ“ž Contact Information
**Location:** Simferopol, Crimea
**Focus:** AI/ML Engineering & Deployment
**Experience:** Production systems, MLOps, Full-stack development
### 🎯 Expertise Areas
- **Machine Learning**: Model training, fine-tuning, optimization
- **Computer Vision**: Face recognition, object detection, image processing
- **NLP**: Text classification, sentiment analysis, LLM deployment
- **Web Scraping**: Large-scale data extraction and processing
- **MLOps**: Model deployment, monitoring, CI/CD pipelines
- **Backend Development**: FastAPI, async programming, database design
""")
gr.HTML("""
<div class="project-card">
<h4>πŸš€ Ready to Explore?</h4>
<p>Navigate through the tabs above to interact with live demos of my projects.
Each tab represents a different area of expertise with hands-on functionality.</p>
</div>
""")
# Computer Vision Tab
with gr.Tab("πŸ€– LLM"):
gr.Markdown("""
## LLM Demo
Demonstrating various computer vision capabilities including face detection,
object detection, and image classification. Built with OpenCV, InsightFace,
and custom trained models.
### πŸ” Available Models:
- **Face Detection**: Locate and identify faces in images
- **Object Detection**: Identify and classify objects
- **Image Classification**: Categorize images into predefined classes
""")
# Embedding section
gr.HTML("""
<iframe src="https://limitedonly41-cv-website-classify.hf.space"
frameborder="0"
width="100%"
height="850px">
</iframe>
""")
# Web Scraping Tab
with gr.Tab("πŸ•·οΈ Web Scraping & Classification"):
gr.Markdown("""
## Web Scraping & Content Classification System
This project demonstrates an advanced web scraping system that uses machine learning
to classify websites into categories. Built with FastAPI, Redis, and a fine-tuned
Mistral 7B model for accurate content classification.
### ✨ Key Features:
- **Async scraping** with configurable concurrency
- **ML-based classification** (OTHER, NEWS/BLOG, E-COMMERCE)
- **Real-time progress tracking** with Redis
- **Robust error handling** and retry mechanisms
- **Export results** in JSON format
""")
with gr.Row():
with gr.Column():
urls_input = gr.Textbox(
label="URLs to Scrape (one per line)",
placeholder="https://example.com\nhttps://news.bbc.com\nhttps://amazon.com",
lines=6,
value="https://example.com\nhttps://news.bbc.com\nhttps://github.com\nhttps://stackoverflow.com\nhttps://amazon.com"
)
scrape_btn = gr.Button("πŸš€ Start Scraping & Classification", variant="primary")
with gr.Row():
sample_btn = gr.Button("πŸ“‹ Load Sample URLs", variant="secondary")
clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
with gr.Column():
status_output = gr.Textbox(label="Status", interactive=False)
results_output = gr.Textbox(
label="Classification Results",
lines=10,
interactive=False
)
with gr.Row():
json_output = gr.File(label="Download Results (JSON)")
# Event handlers
scrape_btn.click(
scraper.simulate_scraping,
inputs=[urls_input],
outputs=[results_output, status_output]
)
sample_btn.click(
lambda: "\n".join(scraper.demo_results.keys()),
outputs=[urls_input]
)
clear_btn.click(
lambda: ("", "", ""),
outputs=[urls_input, status_output]
)
# Computer Vision Tab
with gr.Tab("πŸ‘οΈ Computer Vision"):
gr.Markdown("""
## Computer Vision Processing Demo
Demonstrating various computer vision capabilities including face detection,
object detection, and image classification. Built with OpenCV, InsightFace,
and custom trained models.
### πŸ” Available Models:
- **Face Detection**: Locate and identify faces in images
- **Object Detection**: Identify and classify objects
- **Image Classification**: Categorize images into predefined classes
""")
with gr.Row():
with gr.Column():
image_input = gr.Image(label="Upload Image", type="pil")
model_dropdown = gr.Dropdown(
choices=["Face Detection", "Object Detection", "Image Classification"],
value="Face Detection",
label="Select Model"
)
process_btn = gr.Button("πŸ” Process Image", variant="primary")
with gr.Column():
cv_status = gr.Textbox(label="Status", interactive=False)
cv_results = gr.Textbox(
label="Analysis Results",
lines=8,
interactive=False
)
process_btn.click(
simulate_cv_processing,
inputs=[image_input, model_dropdown],
outputs=[cv_results, cv_status]
)
# Text Analysis Tab
with gr.Tab("πŸ“ Text Analysis"):
gr.Markdown("""
## Natural Language Processing Demo
Advanced text analysis capabilities including sentiment analysis, text summarization,
and named entity recognition. Powered by transformer models and custom NLP pipelines.
### 🧠 Analysis Types:
- **Sentiment Analysis**: Determine emotional tone of text
- **Summarization**: Generate concise summaries of long text
- **Named Entity Recognition**: Extract entities like names, places, organizations
""")
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Text to Analyze",
lines=6,
placeholder="Enter your text here...",
value="The latest developments in artificial intelligence and machine learning are revolutionizing industries across the globe. From healthcare to finance, companies are implementing AI solutions to improve efficiency and decision-making."
)
analysis_dropdown = gr.Dropdown(
choices=["Sentiment Analysis", "Summarization", "Named Entity Recognition"],
value="Sentiment Analysis",
label="Analysis Type"
)
analyze_btn = gr.Button("πŸ”¬ Analyze Text", variant="primary")
with gr.Column():
text_results = gr.Textbox(
label="Analysis Results",
lines=8,
interactive=False
)
analyze_btn.click(
simulate_text_analysis,
inputs=[text_input, analysis_dropdown],
outputs=[text_results]
)
# Data Visualization Tab
with gr.Tab("πŸ“Š Data Visualization"):
gr.Markdown("""
## Interactive Data Visualization
Create dynamic visualizations from your data using Plotly and Pandas.
Upload your own CSV files or explore with sample data.
### πŸ“ˆ Visualization Types:
- **Line Charts**: Perfect for trends over time
- **Bar Charts**: Great for categorical comparisons
- **Pie Charts**: Ideal for showing proportions
- **Scatter Plots**: Excellent for correlation analysis
""")
with gr.Tabs():
with gr.Tab("Sample Data"):
with gr.Row():
chart_type = gr.Dropdown(
choices=["Line Chart", "Bar Chart", "Pie Chart"],
value="Line Chart",
label="Chart Type"
)
generate_chart_btn = gr.Button("πŸ“Š Generate Chart", variant="primary")
sample_plot = gr.Plot(label="Visualization")
generate_chart_btn.click(
lambda chart_type: create_sample_chart(chart_type),
inputs=[chart_type],
outputs=[sample_plot]
)
with gr.Tab("Upload Your Data"):
with gr.Row():
csv_file = gr.File(label="Upload CSV File", file_types=[".csv"])
process_csv_btn = gr.Button("πŸ“‹ Process CSV", variant="primary")
csv_info = gr.Textbox(label="Dataset Information", lines=6, interactive=False)
csv_plot = gr.Plot(label="Data Visualization")
process_csv_btn.click(
process_uploaded_csv,
inputs=[csv_file],
outputs=[csv_info, csv_plot]
)
# Load initial sample chart
app.load(lambda: create_sample_chart("Line Chart"), outputs=[])
return app
# Create and launch the application
if __name__ == "__main__":
portfolio_app = create_portfolio_app()
portfolio_app.launch(share=True, server_name="0.0.0.0", server_port=7860)