Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
"""
|
| 2 |
Netra AI - Construction Material Classifier
|
| 3 |
-
MVP Demo using
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
-
import
|
| 8 |
import os
|
| 9 |
from PIL import Image
|
| 10 |
import base64
|
|
@@ -36,22 +36,45 @@ def classify_material(image):
|
|
| 36 |
return "Please upload an image", "", ""
|
| 37 |
|
| 38 |
# Get API key from environment
|
| 39 |
-
api_key = os.getenv("
|
| 40 |
if not api_key:
|
| 41 |
return "❌ System Error", "", "AI service not configured. Please contact support."
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
# Configure
|
| 45 |
-
|
| 46 |
-
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 47 |
|
| 48 |
# Convert to PIL Image if needed
|
| 49 |
if not isinstance(image, Image.Image):
|
| 50 |
image = Image.fromarray(image)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Generate classification
|
| 53 |
-
response =
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Parse response
|
| 57 |
lines = result_text.split('\n')
|
|
|
|
| 1 |
"""
|
| 2 |
Netra AI - Construction Material Classifier
|
| 3 |
+
MVP Demo using Claude Vision API
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
+
import anthropic
|
| 8 |
import os
|
| 9 |
from PIL import Image
|
| 10 |
import base64
|
|
|
|
| 36 |
return "Please upload an image", "", ""
|
| 37 |
|
| 38 |
# Get API key from environment
|
| 39 |
+
api_key = os.getenv("ANTHROPIC_API_KEY", "")
|
| 40 |
if not api_key:
|
| 41 |
return "❌ System Error", "", "AI service not configured. Please contact support."
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
# Configure Claude
|
| 45 |
+
client = anthropic.Anthropic(api_key=api_key)
|
|
|
|
| 46 |
|
| 47 |
# Convert to PIL Image if needed
|
| 48 |
if not isinstance(image, Image.Image):
|
| 49 |
image = Image.fromarray(image)
|
| 50 |
|
| 51 |
+
# Convert image to base64
|
| 52 |
+
buffered = BytesIO()
|
| 53 |
+
image.save(buffered, format="JPEG")
|
| 54 |
+
image_data = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 55 |
+
|
| 56 |
# Generate classification
|
| 57 |
+
response = client.messages.create(
|
| 58 |
+
model="claude-opus-4-6",
|
| 59 |
+
max_tokens=150,
|
| 60 |
+
messages=[
|
| 61 |
+
{
|
| 62 |
+
"role": "user",
|
| 63 |
+
"content": [
|
| 64 |
+
{
|
| 65 |
+
"type": "image",
|
| 66 |
+
"source": {
|
| 67 |
+
"type": "base64",
|
| 68 |
+
"media_type": "image/jpeg",
|
| 69 |
+
"data": image_data,
|
| 70 |
+
},
|
| 71 |
+
},
|
| 72 |
+
{"type": "text", "text": CLASSIFICATION_PROMPT}
|
| 73 |
+
],
|
| 74 |
+
}
|
| 75 |
+
],
|
| 76 |
+
)
|
| 77 |
+
result_text = response.content[0].text.strip()
|
| 78 |
|
| 79 |
# Parse response
|
| 80 |
lines = result_text.split('\n')
|