Hadi32 commited on
Commit
6eaa671
·
verified ·
1 Parent(s): cf3b3fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -98
app.py CHANGED
@@ -1,98 +1,77 @@
1
- #!/usr/bin/env python3
2
- """
3
- Hugging Face Spaces App - Vision Model
4
- Deploy this to HF Spaces for free cloud inference
5
- """
6
-
7
- import gradio as gr
8
- from transformers import pipeline
9
- from PIL import Image
10
- import io
11
-
12
- # Load model once when app starts
13
- print("Loading vision model...")
14
- vision_model = pipeline(
15
- "image-to-text",
16
- model="Salesforce/blip-image-captioning-base",
17
- device="cpu"
18
- )
19
- print("✓ Model loaded!")
20
-
21
- def analyze_image(image, prompt="Describe the image"):
22
- """Analyze image with vision model"""
23
- if image is None:
24
- return "Please upload an image first"
25
-
26
- try:
27
- # Convert to PIL Image if needed
28
- if isinstance(image, str):
29
- image = Image.open(image)
30
-
31
- # Resize if too large
32
- image.thumbnail((512, 512))
33
-
34
- # Generate caption
35
- result = vision_model(image)
36
- caption = result[0]["generated_text"] if result else "No output"
37
-
38
- return caption
39
- except Exception as e:
40
- return f"Error: {str(e)}"
41
-
42
- # Create Gradio interface
43
- with gr.Blocks(title="Vision AI - Image Analysis", theme=gr.themes.Soft()) as demo:
44
- gr.Markdown("""
45
- # 🖼️ Vision AI - Local Image Analysis
46
-
47
- Analyze images using a lightweight vision model running locally.
48
- - **Model**: BLIP Image Captioning (Base)
49
- - **Processing**: 100% on device (no cloud required)
50
- - **Speed**: Fast inference on CPU
51
- """)
52
-
53
- with gr.Row():
54
- with gr.Column():
55
- gr.Markdown("### Upload Image")
56
- image_input = gr.Image(
57
- label="Select Image",
58
- type="pil",
59
- interactive=True
60
- )
61
-
62
- with gr.Column():
63
- gr.Markdown("### Settings")
64
- prompt_input = gr.Textbox(
65
- label="Prompt",
66
- value="Describe the image",
67
- lines=2
68
- )
69
-
70
- analyze_btn = gr.Button("🔍 Analyze Image", size="lg", variant="primary")
71
-
72
- gr.Markdown("### Result")
73
- output = gr.Textbox(
74
- label="Analysis Result",
75
- lines=4,
76
- interactive=False
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
+ )