Atharwaaah's picture
Upload 2 files
bbe7950 verified
Raw History Blame
9.22 kB
# app.py
import torch
from transformers import InstructBlipProcessor, InstructBlipForConditionalGeneration
import gradio as gr
from PIL import Image
import re
from typing import List, Tuple
import os
class RiverPollutionAnalyzer:
def __init__(self):
# Check if CUDA is available
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# Load model with appropriate settings for Spaces
self.processor = InstructBlipProcessor.from_pretrained(
"Salesforce/instructblip-vicuna-7b"
)
# Simplified model loading for Spaces compatibility
self.model = InstructBlipForConditionalGeneration.from_pretrained(
"Salesforce/instructblip-vicuna-7b",
device_map="auto",
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
).to(self.device)
self.pollutants = [
"plastic waste",
"chemical foam",
"industrial discharge",
"sewage water",
"oil spill",
"organic debris",
"construction waste",
"medical waste",
"floating trash",
"algal bloom",
"toxic sludge",
"agricultural runoff",
]
self.severity_descriptions = {
1: "Minimal pollution - Slightly noticeable",
2: "Minor pollution - Small amounts visible",
3: "Moderate pollution - Clearly visible",
4: "Significant pollution - Affecting water quality",
5: "Heavy pollution - Obvious environmental impact",
6: "Severe pollution - Large accumulation",
7: "Very severe pollution - Major ecosystem impact",
8: "Extreme pollution - Dangerous levels",
9: "Critical pollution - Immediate action needed",
10: "Disaster level - Ecological catastrophe",
}
def analyze_image(self, image):
"""Analyze river pollution with robust parsing"""
if not isinstance(image, Image.Image):
image = Image.fromarray(image)
prompt = """Analyze this river pollution scene and provide:
1. List ALL visible pollutants ONLY from: [plastic waste, chemical foam, industrial discharge, sewage water, oil spill, organic debris, construction waste, medical waste, floating trash, algal bloom, toxic sludge, agricultural runoff]
2. Estimate pollution severity from 1-10
Respond EXACTLY in this format:
Pollutants: [comma separated list]
Severity: [number]"""
inputs = self.processor(images=image, text=prompt, return_tensors="pt").to(
self.device
)
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=200,
temperature=0.5,
top_p=0.85,
do_sample=True,
)
analysis = self.processor.batch_decode(outputs, skip_special_tokens=True)[0]
pollutants, severity = self._parse_response(analysis)
return self._format_analysis(pollutants, severity)
def _parse_response(self, analysis: str) -> Tuple[List[str], int]:
"""Robust parsing of model response"""
pollutants = []
severity = 3
# Extract pollutants
pollutant_match = re.search(
r"(?i)(pollutants?|contaminants?)[:\s]*\[?(.*?)(?:\]|Severity|severity|$)",
analysis,
)
if pollutant_match:
pollutants_str = pollutant_match.group(2).strip()
pollutants = [
p.strip().lower()
for p in re.split(r"[,;]|\band\b", pollutants_str)
if p.strip().lower() in self.pollutants
]
# Extract severity
severity_match = re.search(r"(?i)(severity|level)[:\s]*(\d{1,2})", analysis)
if severity_match:
try:
severity = min(max(int(severity_match.group(2)), 1), 10)
except:
severity = self._calculate_severity(pollutants)
else:
severity = self._calculate_severity(pollutants)
return pollutants, severity
def _calculate_severity(self, pollutants: List[str]) -> int:
"""Weighted severity calculation"""
if not pollutants:
return 1
weights = {
"medical waste": 3,
"toxic sludge": 3,
"oil spill": 2.5,
"chemical foam": 2,
"industrial discharge": 2,
"sewage water": 2,
"plastic waste": 1.5,
"construction waste": 1.5,
"algal bloom": 1.5,
"agricultural runoff": 1.5,
"floating trash": 1,
"organic debris": 1,
}
avg_weight = sum(weights.get(p, 1) for p in pollutants) / len(pollutants)
return min(10, max(1, round(avg_weight * 3)))
def _format_analysis(self, pollutants: List[str], severity: int) -> str:
"""Generate formatted report"""
severity_bar = f"""πŸ“Š Severity: {severity}/10
{"β–ˆ" * severity}{"β–‘" * (10 - severity)}
{self.severity_descriptions.get(severity, "")}"""
pollutants_list = (
"\nπŸ” No pollutants detected"
if not pollutants
else "\n".join(
f"{i}. {p.capitalize()}" for i, p in enumerate(pollutants[:5], 1)
)
)
return f"""🌊 River Pollution Analysis 🌊
{pollutants_list}
{severity_bar}"""
def analyze_chat(self, message: str) -> str:
"""Handle chat questions about pollution"""
# Simple implementation - you can expand this
if any(word in message.lower() for word in ["hello", "hi", "hey"]):
return "Hello! I'm a river pollution analyzer. Ask me about pollution types or upload an image for analysis."
elif "pollution" in message.lower():
return "Common river pollutants include: plastic waste, chemical foam, industrial discharge, sewage water, and oil spills."
else:
return "I can answer questions about river pollution. Try asking about pollution types or upload an image for analysis."
# Initialize analyzer
analyzer = RiverPollutionAnalyzer()
css = """
.header {
text-align: center;
padding: 20px;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
border-radius: 10px;
margin-bottom: 20px;
}
.side-by-side {
display: flex;
gap: 20px;
}
.left-panel, .right-panel {
flex: 1;
}
.analysis-box {
padding: 20px;
background: #f8f9fa;
border-radius: 10px;
margin-top: 20px;
border: 1px solid #dee2e6;
}
.chat-container {
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
height: 100%;
}
"""
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
with gr.Column(elem_classes="header"):
gr.Markdown("# 🌍 River Pollution Analyzer")
gr.Markdown("### AI-powered water pollution detection")
with gr.Row(elem_classes="side-by-side"):
# Left Panel
with gr.Column(elem_classes="left-panel"):
with gr.Group():
image_input = gr.Image(
type="pil", label="Upload River Image", height=300
)
analyze_btn = gr.Button("πŸ” Analyze Pollution", variant="primary")
with gr.Group(elem_classes="analysis-box"):
gr.Markdown("### πŸ“Š Analysis report")
analysis_output = gr.Markdown()
# Right Panel
with gr.Column(elem_classes="right-panel"):
with gr.Group(elem_classes="chat-container"):
chatbot = gr.Chatbot(label="Pollution Analysis Q&A", height=400)
with gr.Row():
chat_input = gr.Textbox(
placeholder="Ask about pollution sources...",
label="Your Question",
container=False,
scale=5,
)
chat_btn = gr.Button("πŸ’¬ Ask", variant="secondary", scale=1)
clear_btn = gr.Button("🧹 Clear Chat History", size="sm")
analyze_btn.click(
analyzer.analyze_image, inputs=image_input, outputs=analysis_output
)
chat_input.submit(
lambda msg, chat: ("", chat + [(msg, analyzer.analyze_chat(msg))]),
inputs=[chat_input, chatbot],
outputs=[chat_input, chatbot],
)
chat_btn.click(
lambda msg, chat: ("", chat + [(msg, analyzer.analyze_chat(msg))]),
inputs=[chat_input, chatbot],
outputs=[chat_input, chatbot],
)
clear_btn.click(lambda: None, outputs=[chatbot])
gr.Examples(
examples=[
[
"https://huggingface.co/spaces/username/your-space-name/resolve/main/examples/polluted_river_1.jpg"
],
[
"https://huggingface.co/spaces/username/your-space-name/resolve/main/examples/polluted_river_2.jpg"
],
],
inputs=image_input,
outputs=analysis_output,
fn=analyzer.analyze_image,
cache_examples=True,
label="Try example images:",
)
demo.launch()