Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
"""
|
| 59 |
|
| 60 |
-
with gr.Blocks(css=css, title="DaltonVision") as demo:
|
| 61 |
gr.Markdown("""
|
| 62 |
-
# πΈ DaltonVision - Camera
|
| 63 |
-
###
|
| 64 |
""")
|
| 65 |
|
| 66 |
-
with gr.
|
| 67 |
-
with gr.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
label="Try these prompts:"
|
| 94 |
-
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
submit_btn.click(
|
| 105 |
-
|
| 106 |
inputs=[camera, prompt],
|
| 107 |
outputs=output
|
| 108 |
)
|
| 109 |
|
| 110 |
-
# For Hugging Face Spaces deployment
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
demo.launch(
|
|
|
|
| 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()
|