Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,77 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"image-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
print("✓ Model loaded!")
|
| 20 |
-
|
| 21 |
-
def
|
| 22 |
-
"""Analyze image with
|
| 23 |
-
if image is None:
|
| 24 |
-
return "Please upload an image first"
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
- **
|
| 49 |
-
- **
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# Connect button to function
|
| 80 |
-
analyze_btn.click(
|
| 81 |
-
fn=analyze_image,
|
| 82 |
-
inputs=[image_input, prompt_input],
|
| 83 |
-
outputs=output
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
# Example images
|
| 87 |
-
gr.Examples(
|
| 88 |
-
examples=[],
|
| 89 |
-
inputs=[image_input],
|
| 90 |
-
label="Examples (optional)"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
if __name__ == "__main__":
|
| 94 |
-
demo.launch(
|
| 95 |
-
server_name="0.0.0.0",
|
| 96 |
-
server_port=7860,
|
| 97 |
-
share=True
|
| 98 |
-
)
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Vision AI - Detailed Image Analysis
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
print("Loading vision model...")
|
| 11 |
+
|
| 12 |
+
# Advanced captioning model for detailed descriptions
|
| 13 |
+
vision_pipeline = pipeline(
|
| 14 |
+
"image-to-text",
|
| 15 |
+
model="Salesforce/blip-image-captioning-large",
|
| 16 |
+
device=-1 # CPU
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
print("✓ Model loaded!")
|
| 20 |
+
|
| 21 |
+
def analyze_image_detailed(image, prompt=""):
|
| 22 |
+
"""Analyze image with detailed captioning"""
|
| 23 |
+
if image is None:
|
| 24 |
+
return "Please upload an image first"
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
if isinstance(image, str):
|
| 28 |
+
image = Image.open(image)
|
| 29 |
+
|
| 30 |
+
image.thumbnail((512, 512))
|
| 31 |
+
|
| 32 |
+
# Generate detailed caption
|
| 33 |
+
result = vision_pipeline(image)
|
| 34 |
+
caption = result[0]["generated_text"] if result else "No output"
|
| 35 |
+
|
| 36 |
+
return caption
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error: {str(e)}"
|
| 39 |
+
|
| 40 |
+
# Create Gradio interface
|
| 41 |
+
with gr.Blocks(title="Vision AI - Detailed Analysis", theme=gr.themes.Soft()) as demo:
|
| 42 |
+
gr.Markdown("""
|
| 43 |
+
# 🖼️ Vision AI - Advanced Image Analysis
|
| 44 |
+
|
| 45 |
+
Get detailed analysis of your images using advanced AI.
|
| 46 |
+
|
| 47 |
+
- **Model**: BLIP Large (Salesforce)
|
| 48 |
+
- **Processing**: 100% local (no cloud)
|
| 49 |
+
- **Analysis**: Comprehensive image descriptions
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
gr.Markdown("### Upload Image")
|
| 55 |
+
image_input = gr.Image(label="Select Image", type="pil")
|
| 56 |
+
analyze_btn = gr.Button("🔍 Analyze Image", size="lg", variant="primary")
|
| 57 |
+
|
| 58 |
+
with gr.Column():
|
| 59 |
+
gr.Markdown("### Detailed Analysis")
|
| 60 |
+
output = gr.Textbox(
|
| 61 |
+
label="Image Description",
|
| 62 |
+
lines=8,
|
| 63 |
+
interactive=False
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
analyze_btn.click(
|
| 67 |
+
fn=analyze_image_detailed,
|
| 68 |
+
inputs=image_input,
|
| 69 |
+
outputs=output
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch(
|
| 74 |
+
server_name="0.0.0.0",
|
| 75 |
+
server_port=7860,
|
| 76 |
+
share=True
|
| 77 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|