Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,22 +3,17 @@ from openai import OpenAI
|
|
| 3 |
import base64
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
-
import
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
-
#
|
| 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
|
| 22 |
|
| 23 |
# Convert image to base64
|
| 24 |
buffered = io.BytesIO()
|
|
@@ -31,7 +26,13 @@ def analyze_image(image, prompt):
|
|
| 31 |
messages=[
|
| 32 |
{
|
| 33 |
"role": "system",
|
| 34 |
-
"content": """You are an expert AI assistant specialized in image understanding.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
},
|
| 36 |
{
|
| 37 |
"role": "user",
|
|
@@ -48,126 +49,91 @@ def analyze_image(image, prompt):
|
|
| 48 |
],
|
| 49 |
max_tokens=2048
|
| 50 |
)
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
-
return f"
|
| 54 |
|
|
|
|
| 55 |
css = """
|
| 56 |
-
#camera
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 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 |
-
.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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
|
| 95 |
gr.Markdown("""
|
| 96 |
-
#
|
| 97 |
-
###
|
| 98 |
""")
|
| 99 |
|
| 100 |
-
with gr.
|
| 101 |
-
with gr.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 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 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 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 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
inputs=upload,
|
| 163 |
-
outputs=camera
|
| 164 |
-
)
|
| 165 |
|
|
|
|
| 166 |
submit_btn.click(
|
| 167 |
-
analyze_image,
|
| 168 |
-
inputs=[
|
| 169 |
-
outputs=
|
| 170 |
)
|
| 171 |
|
|
|
|
| 172 |
if __name__ == "__main__":
|
| 173 |
demo.launch()
|
|
|
|
| 3 |
import base64
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
+
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
+
# OpenAI client setup
|
| 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 upload or capture an image first."
|
| 17 |
|
| 18 |
# Convert image to base64
|
| 19 |
buffered = io.BytesIO()
|
|
|
|
| 26 |
messages=[
|
| 27 |
{
|
| 28 |
"role": "system",
|
| 29 |
+
"content": """You are Dalton, an expert AI assistant specialized in image understanding.
|
| 30 |
+
Your tasks include:
|
| 31 |
+
- Extracting and structuring text from images
|
| 32 |
+
- Answering questions about image content
|
| 33 |
+
- Providing detailed descriptions
|
| 34 |
+
- Analyzing receipts, documents, and other visual content
|
| 35 |
+
Be thorough, accurate, and helpful in your responses."""
|
| 36 |
},
|
| 37 |
{
|
| 38 |
"role": "user",
|
|
|
|
| 49 |
],
|
| 50 |
max_tokens=2048
|
| 51 |
)
|
| 52 |
+
|
| 53 |
+
result = response.choices[0].message.content
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"An error occurred: {str(e)}"
|
| 58 |
|
| 59 |
+
# Custom CSS for better mobile experience
|
| 60 |
css = """
|
| 61 |
+
#mobile-camera { width: 100% !important; }
|
| 62 |
+
#prompt-textbox { min-height: 100px !important; }
|
| 63 |
+
.result-box {
|
| 64 |
+
max-height: 500px;
|
| 65 |
+
overflow-y: auto;
|
| 66 |
+
padding: 15px;
|
| 67 |
+
border: 1px solid #e0e0e0;
|
| 68 |
+
border-radius: 8px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
+
.footer {
|
| 71 |
+
margin-top: 20px;
|
| 72 |
+
font-size: 12px;
|
| 73 |
+
color: #666;
|
| 74 |
+
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
"""
|
| 77 |
|
| 78 |
+
with gr.Blocks(css=css, title="DaltonVision - Koshur AI") as demo:
|
| 79 |
gr.Markdown("""
|
| 80 |
+
# π§Ύ DaltonVision - InternVL3-14B
|
| 81 |
+
### Advanced Image Understanding β’ Powered by OpenRouter β’ Developed by [Koshur AI](https://koshurai.com)
|
| 82 |
""")
|
| 83 |
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column():
|
| 86 |
+
# Image input section
|
| 87 |
+
image_input = gr.Image(
|
| 88 |
+
sources=["upload", "webcam"],
|
| 89 |
+
type="pil",
|
| 90 |
+
label="Upload or Capture Image",
|
| 91 |
+
elem_id="mobile-camera"
|
| 92 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Prompt input
|
| 95 |
+
prompt_input = gr.Textbox(
|
| 96 |
+
label="π Enter your question or instruction",
|
| 97 |
+
value="Extract all content structurally",
|
| 98 |
+
lines=3,
|
| 99 |
+
elem_id="prompt-textbox"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
submit_btn = gr.Button("π Analyze Image", variant="primary")
|
| 103 |
+
|
| 104 |
+
gr.Examples(
|
| 105 |
+
examples=[
|
| 106 |
+
["What is the total amount on this receipt?"],
|
| 107 |
+
["List all items and their prices"],
|
| 108 |
+
["Who is the vendor and what is the date?"],
|
| 109 |
+
["Describe this image in detail"]
|
| 110 |
+
],
|
| 111 |
+
inputs=[prompt_input],
|
| 112 |
+
label="π‘ Try these example prompts:"
|
| 113 |
+
)
|
| 114 |
|
| 115 |
+
with gr.Column():
|
| 116 |
+
# Result output
|
| 117 |
+
result_output = gr.Markdown(
|
| 118 |
+
label="β
Analysis Result",
|
| 119 |
+
elem_classes="result-box"
|
| 120 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
# Footer
|
| 123 |
+
gr.Markdown("""
|
| 124 |
+
<div class="footer">
|
| 125 |
+
Β© 2025 Koshur AI. All rights reserved.<br>
|
| 126 |
+
Note: Images are processed in real-time and not stored.
|
| 127 |
+
</div>
|
| 128 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Button action
|
| 131 |
submit_btn.click(
|
| 132 |
+
fn=analyze_image,
|
| 133 |
+
inputs=[image_input, prompt_input],
|
| 134 |
+
outputs=result_output
|
| 135 |
)
|
| 136 |
|
| 137 |
+
# Launch the app
|
| 138 |
if __name__ == "__main__":
|
| 139 |
demo.launch()
|