Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py — Civil Damage Analyzer (Gradio + Groq)
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import base64, requests, os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Get Groq API key from Hugging Face Secret
|
| 9 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 10 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
+
MODEL_NAME = "llama3-70b-8192"
|
| 12 |
+
|
| 13 |
+
def call_groq(prompt: str) -> str:
|
| 14 |
+
headers = {
|
| 15 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 16 |
+
"Content-Type": "application/json"
|
| 17 |
+
}
|
| 18 |
+
data = {
|
| 19 |
+
"model": MODEL_NAME,
|
| 20 |
+
"messages": [
|
| 21 |
+
{"role": "system", "content": "You are a helpful civil engineering assistant."},
|
| 22 |
+
{"role": "user", "content": prompt}
|
| 23 |
+
],
|
| 24 |
+
"temperature": 0.5
|
| 25 |
+
}
|
| 26 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=data, timeout=60)
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 29 |
+
else:
|
| 30 |
+
return f"❌ Groq API error {response.status_code}\n{response.text}"
|
| 31 |
+
|
| 32 |
+
def analyze_damage(image: Image.Image) -> str:
|
| 33 |
+
buf = BytesIO()
|
| 34 |
+
image.save(buf, format="JPEG")
|
| 35 |
+
img_b64 = base64.b64encode(buf.getvalue()).decode()
|
| 36 |
+
|
| 37 |
+
prompt = f"""
|
| 38 |
+
You are a professional civil engineer. Given a base64‑encoded photo of construction damage,
|
| 39 |
+
provide a structured report with:
|
| 40 |
+
1. Damage Type
|
| 41 |
+
2. Estimated Repair Time
|
| 42 |
+
3. Estimated Cost Range
|
| 43 |
+
4. Required Materials
|
| 44 |
+
5. Safety Precautions
|
| 45 |
+
6. Prevention Tips
|
| 46 |
+
|
| 47 |
+
Base64 (truncated): {img_b64[:500]}...
|
| 48 |
+
"""
|
| 49 |
+
return call_groq(prompt)
|
| 50 |
+
|
| 51 |
+
demo = gr.Interface(
|
| 52 |
+
fn=analyze_damage,
|
| 53 |
+
inputs=gr.Image(type="pil", label="📷 Upload Damage Image"),
|
| 54 |
+
outputs=gr.Textbox(label="📄 Damage Assessment", lines=15),
|
| 55 |
+
title="🏗️ Civil Damage Analyzer",
|
| 56 |
+
description="Upload a photo of damage (e.g., cracked wall, broken pipe) and get expert repair guidance.",
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|