adil9858 commited on
Commit
3d1d7d0
Β·
verified Β·
1 Parent(s): 42ac28a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -48
app.py CHANGED
@@ -4,13 +4,18 @@ import base64
4
  from PIL import Image
5
  import io
6
  import os
 
7
 
8
- # Initialize OpenAI client with environment variable for API key
9
  client = OpenAI(
10
  base_url="https://openrouter.ai/api/v1",
11
  api_key='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'
12
  )
13
 
 
 
 
 
14
  def analyze_image(image, prompt):
15
  if image is None:
16
  return "Please capture or upload an image first."
@@ -47,66 +52,122 @@ def analyze_image(image, prompt):
47
  except Exception as e:
48
  return f"Error: {str(e)}"
49
 
50
- # Custom CSS for mobile optimization
51
  css = """
52
- #camera-input {width: 100% !important;}
53
- #camera-preview {max-width: 100%; margin: 0 auto;}
54
- @media (max-width: 768px) {
55
- #col-left {padding: 10px !important;}
56
- #col-right {padding: 10px !important;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  }
58
  """
59
 
60
- with gr.Blocks(css=css, title="DaltonVision") as demo:
61
  gr.Markdown("""
62
- # πŸ“Έ DaltonVision - Camera Analysis
63
- ### Capture, Upload & Analyze Images with AI
64
  """)
65
 
66
- with gr.Row(equal_height=True):
67
- with gr.Column(elem_id="col-left"):
68
- # Camera component with larger preview
69
- camera = gr.Image(
70
- sources=["webcam", "upload"],
71
- type="pil",
72
- label="Take a picture or upload",
73
- elem_id="camera-input",
74
- interactive=True,
75
- height=400
76
- )
77
-
78
- prompt = gr.Textbox(
79
- label="What would you like to know?",
80
- placeholder="Describe this image...",
81
- lines=3
82
- )
83
-
84
- submit_btn = gr.Button("Analyze", variant="primary")
 
 
 
 
 
 
 
 
 
85
 
86
- gr.Examples(
87
- examples=[
88
- ["What's written in this document?"],
89
- ["Describe this scene in detail"],
90
- ["Extract all text from this image"]
91
- ],
92
- inputs=[prompt],
93
- label="Try these prompts:"
94
- )
95
 
96
- with gr.Column(elem_id="col-right"):
97
- output = gr.Textbox(
98
- label="Analysis Results",
99
- interactive=False,
100
- lines=15,
101
- show_copy_button=True
102
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  submit_btn.click(
105
- fn=analyze_image,
106
  inputs=[camera, prompt],
107
  outputs=output
108
  )
109
 
110
- # For Hugging Face Spaces deployment
111
  if __name__ == "__main__":
112
- demo.launch(show_api=False)
 
4
  from PIL import Image
5
  import io
6
  import os
7
+ import time
8
 
9
+ # Initialize OpenAI client
10
  client = OpenAI(
11
  base_url="https://openrouter.ai/api/v1",
12
  api_key='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'
13
  )
14
 
15
+ def capture_image():
16
+ # This will trigger the camera capture in the frontend
17
+ return None
18
+
19
  def analyze_image(image, prompt):
20
  if image is None:
21
  return "Please capture or upload an image first."
 
52
  except Exception as e:
53
  return f"Error: {str(e)}"
54
 
 
55
  css = """
56
+ #camera-container {
57
+ position: relative;
58
+ width: 100%;
59
+ margin: 0 auto;
60
+ }
61
+ #camera-input {
62
+ width: 100% !important;
63
+ min-height: 300px;
64
+ }
65
+ #capture-btn {
66
+ position: absolute;
67
+ bottom: 20px;
68
+ left: 50%;
69
+ transform: translateX(-50%);
70
+ z-index: 100;
71
+ background: white;
72
+ border-radius: 50%;
73
+ width: 60px;
74
+ height: 60px;
75
+ border: 3px solid #4a6cf7;
76
+ cursor: pointer;
77
+ }
78
+ #capture-btn:active {
79
+ transform: translateX(-50%) scale(0.95);
80
+ }
81
+ .mobile-controls {
82
+ display: flex;
83
+ gap: 10px;
84
+ margin-top: 10px;
85
+ justify-content: center;
86
+ }
87
+ @media (min-width: 768px) {
88
+ #camera-container {
89
+ max-width: 500px;
90
+ }
91
  }
92
  """
93
 
94
+ with gr.Blocks(css=css, title="DaltonVision Camera") as demo:
95
  gr.Markdown("""
96
+ # πŸ“Έ DaltonVision - Camera Mode
97
+ ### Take pictures directly in the app for analysis
98
  """)
99
 
100
+ with gr.Column():
101
+ with gr.Row():
102
+ with gr.Column(elem_id="camera-container"):
103
+ # Camera component
104
+ camera = gr.Image(
105
+ sources=["webcam"],
106
+ type="pil",
107
+ label="Camera Preview",
108
+ elem_id="camera-input",
109
+ interactive=False,
110
+ mirror_webcam=False
111
+ )
112
+
113
+ # Hidden button that triggers capture
114
+ capture_trigger = gr.Button("Capture", visible=False)
115
+
116
+ # Custom capture button
117
+ with gr.Row(elem_classes="mobile-controls"):
118
+ gr.HTML("""
119
+ <div id="capture-btn" onclick="document.querySelector('#capture-btn-hidden').click()"></div>
120
+ """)
121
+ capture_btn = gr.Button("Capture Photo", elem_id="capture-btn-hidden", visible=False)
122
+
123
+ flip_btn = gr.Button("πŸ”„ Flip Camera")
124
+ reset_btn = gr.Button("❌ Reset")
125
+
126
+ # Upload fallback
127
+ upload = gr.UploadButton("πŸ“ Upload Instead", file_types=["image"])
128
 
129
+ prompt = gr.Textbox(
130
+ label="Ask about the image",
131
+ placeholder="What would you like to know about this image?",
132
+ lines=3
133
+ )
134
+
135
+ submit_btn = gr.Button("Analyze Image", variant="primary")
 
 
136
 
137
+ output = gr.Textbox(
138
+ label="Analysis Results",
139
+ interactive=False,
140
+ lines=10,
141
+ show_copy_button=True
142
+ )
143
+
144
+ # Event handlers
145
+ capture_btn.click(
146
+ capture_image,
147
+ outputs=camera
148
+ )
149
+
150
+ flip_btn.click(
151
+ None,
152
+ _js="() => { document.querySelector('video').style.transform = document.querySelector('video').style.transform === 'scaleX(-1)' ? 'scaleX(1)' : 'scaleX(-1)'; }"
153
+ )
154
+
155
+ reset_btn.click(
156
+ lambda: (None, ""),
157
+ outputs=[camera, output]
158
+ )
159
+
160
+ upload.upload(
161
+ lambda file: Image.open(file.name),
162
+ inputs=upload,
163
+ outputs=camera
164
+ )
165
 
166
  submit_btn.click(
167
+ analyze_image,
168
  inputs=[camera, prompt],
169
  outputs=output
170
  )
171
 
 
172
  if __name__ == "__main__":
173
+ demo.launch()