Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Device and Model Setup
|
| 10 |
+
# -----------------------------
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
print(f"Using device: {device}")
|
| 13 |
+
|
| 14 |
+
model_name = "Salesforce/blip2-flan-t5-xl-coco" # public model
|
| 15 |
+
processor = Blip2Processor.from_pretrained(model_name)
|
| 16 |
+
model = Blip2ForConditionalGeneration.from_pretrained(model_name)
|
| 17 |
+
model.to(device)
|
| 18 |
+
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# Inference Function
|
| 21 |
+
# -----------------------------
|
| 22 |
+
def analyze_eo_image(image, question):
|
| 23 |
+
if image is None or question.strip() == "":
|
| 24 |
+
return "Please upload an EO image and ask a question."
|
| 25 |
+
|
| 26 |
+
if image.mode != "RGB":
|
| 27 |
+
image = image.convert("RGB")
|
| 28 |
+
|
| 29 |
+
inputs = processor(image, question, return_tensors="pt").to(
|
| 30 |
+
device, torch.float16 if device=="cuda" else torch.float32
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
out = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_new_tokens=80,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
temperature=0.7
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return processor.decode(out[0], skip_special_tokens=True)
|
| 41 |
+
|
| 42 |
+
# -----------------------------
|
| 43 |
+
# Optional: URL input
|
| 44 |
+
# -----------------------------
|
| 45 |
+
def analyze_eo_url(url, question):
|
| 46 |
+
try:
|
| 47 |
+
response = requests.get(url)
|
| 48 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 49 |
+
return analyze_eo_image(image, question)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error loading image: {e}"
|
| 52 |
+
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# Beautiful Gradio Layout
|
| 55 |
+
# -----------------------------
|
| 56 |
+
with gr.Blocks(title="π EO Image Analysis") as demo:
|
| 57 |
+
|
| 58 |
+
gr.Markdown(
|
| 59 |
+
"""
|
| 60 |
+
# π Earth Observation Image Analysis
|
| 61 |
+
Ask questions about EO images using a multimodal AI model.
|
| 62 |
+
Powered by BLIP-2 + FLAN-T5.
|
| 63 |
+
**Examples:** "Identify land cover types", "Where is the river?", "Has urban area expanded?"
|
| 64 |
+
"""
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
with gr.Tabs():
|
| 68 |
+
with gr.Tab("Upload Image"):
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column(scale=1):
|
| 71 |
+
img_input = gr.Image(type="pil", label="Upload EO Image")
|
| 72 |
+
question_input = gr.Textbox(label="Ask a question about the image", placeholder="E.g. Where is the river?")
|
| 73 |
+
submit_btn = gr.Button("Analyze π")
|
| 74 |
+
with gr.Column(scale=1):
|
| 75 |
+
output_text = gr.Textbox(label="AI Answer", interactive=False)
|
| 76 |
+
|
| 77 |
+
submit_btn.click(analyze_eo_image, inputs=[img_input, question_input], outputs=output_text)
|
| 78 |
+
|
| 79 |
+
with gr.Tab("Use Image URL"):
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column(scale=1):
|
| 82 |
+
url_input = gr.Textbox(label="Enter Image URL")
|
| 83 |
+
url_question = gr.Textbox(label="Ask a question about the image")
|
| 84 |
+
url_btn = gr.Button("Analyze π")
|
| 85 |
+
with gr.Column(scale=1):
|
| 86 |
+
url_output = gr.Textbox(label="AI Answer", interactive=False)
|
| 87 |
+
|
| 88 |
+
url_btn.click(analyze_eo_url, inputs=[url_input, url_question], outputs=url_output)
|
| 89 |
+
|
| 90 |
+
gr.Markdown(
|
| 91 |
+
"π‘ Tip: Use clear, simple questions for best results. Supports natural language queries about EO images."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
demo.launch(share=True)
|