import torch import gradio as gr from transformers import BlipProcessor, BlipForConditionalGeneration import time import logging # Corrected the logging function name logging.getLogger("asyncio").setLevel(logging.CRITICAL) # --- CONFIGURATION --- model_id = "Salesforce/blip-image-captioning-base" device = "cuda" if torch.cuda.is_available() else "cpu" # --- MODEL LOADING --- print(f"🚀 TurboVision System starting on: {device}") try: processor = BlipProcessor.from_pretrained(model_id) model = BlipForConditionalGeneration.from_pretrained(model_id).to(device) print("✅ SUCCESS: BLIP Model is ready!") except Exception as e: print(f"❌ LOADING ERROR: {e}") model = None # --- ANALYSIS LOGIC --- def generate_knowledge(image): if model is None: return "Model not loaded.", "N/A", "N/A", "0s" if image is None: return "Please upload an image.", "N/A", "N/A", "0s" start_time = time.time() try: inputs = processor(image, return_tensors="pt").to(device) out = model.generate(**inputs) summary = processor.decode(out[0], skip_special_tokens=True) inputs_det = processor(image, "a detailed description of", return_tensors="pt").to(device) out_det = model.generate(**inputs_det) details = processor.decode(out_det[0], skip_special_tokens=True) speed = f"{round(time.time() - start_time, 2)}s" return summary.capitalize(), details.capitalize(), "Visual Analysis Complete.", speed except Exception as e: return f"Error: {str(e)}", "N/A", "N/A", "Error" # --- UI DESIGN --- with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.HTML("""
Deep Learning Image Intelligence by Muhammad Bilal