Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,76 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
from ultralytics import YOLO
|
5 |
from openai import OpenAI
|
6 |
-
import os
|
7 |
|
|
|
|
|
|
|
|
|
8 |
client = OpenAI(
|
9 |
-
api_key=os.getenv("civil_project"),
|
10 |
base_url="https://api.groq.com/openai/v1"
|
11 |
)
|
12 |
-
# β
Load YOLOv8 model
|
13 |
-
model = YOLO("yolov8n.pt") # You can later swap with a custom-trained model
|
14 |
|
15 |
-
# β
|
|
|
|
|
|
|
16 |
def ask_groq(prompt):
|
17 |
-
|
18 |
-
model="llama3-8b-8192",
|
19 |
messages=[
|
20 |
-
{"role": "system", "content": "You are an expert in
|
21 |
{"role": "user", "content": prompt}
|
22 |
],
|
23 |
temperature=0.5
|
24 |
)
|
25 |
-
return
|
26 |
|
27 |
-
# β
Damage detection + chatbot response
|
28 |
def process_image(image):
|
29 |
try:
|
30 |
-
# Convert image from NumPy to PIL
|
31 |
if isinstance(image, np.ndarray):
|
32 |
image = Image.fromarray(image)
|
33 |
elif isinstance(image, str):
|
34 |
image = Image.open(image)
|
35 |
|
36 |
-
#
|
37 |
-
results = model(image)
|
38 |
-
|
39 |
-
|
40 |
-
detected = [labels[int(cls)] for cls in boxes.cls]
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
# Prompt sent to Groq's LLaMA 3 model
|
45 |
user_prompt = f"""
|
46 |
-
|
47 |
Please:
|
48 |
-
- Diagnose the
|
49 |
-
- Suggest
|
50 |
- List tools/materials required
|
51 |
- Estimate repair time
|
52 |
"""
|
53 |
|
54 |
-
|
55 |
-
return response
|
56 |
|
57 |
except Exception as e:
|
58 |
-
return f"β Error: {
|
59 |
|
60 |
-
# β
Gradio UI
|
61 |
with gr.Blocks() as demo:
|
62 |
-
gr.Markdown("##
|
63 |
-
gr.Markdown("Upload
|
64 |
-
|
65 |
-
with gr.Row():
|
66 |
-
img_input = gr.Image(type="numpy", label="Upload Damage Image")
|
67 |
-
output_text = gr.Textbox(label="Diagnosis & Suggestions")
|
68 |
|
69 |
-
|
70 |
-
|
|
|
71 |
|
72 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
from ultralytics import YOLO
|
6 |
from openai import OpenAI
|
|
|
7 |
|
8 |
+
# π§ Suppress Ultralytics config warning in Spaces
|
9 |
+
os.environ["YOLO_CONFIG_DIR"] = "/tmp"
|
10 |
+
|
11 |
+
# β
Initialize Groq client with Hugging Face secret
|
12 |
client = OpenAI(
|
13 |
+
api_key=os.getenv("civil_project"),
|
14 |
base_url="https://api.groq.com/openai/v1"
|
15 |
)
|
|
|
|
|
16 |
|
17 |
+
# β
Load crack-segmentation model from Hugging Face
|
18 |
+
# Use 'yolov8n-seg.pt' for fast edge inference
|
19 |
+
model = YOLO("yolov8n-seg.pt") # Downloaded from 'OpenSistemas/YOLOv8-crack-seg' :contentReference[oaicite:1]{index=1}
|
20 |
+
|
21 |
def ask_groq(prompt):
|
22 |
+
resp = client.chat.completions.create(
|
23 |
+
model="llama3-8b-8192",
|
24 |
messages=[
|
25 |
+
{"role": "system", "content": "You are an expert structural engineer specialized in crack diagnosis."},
|
26 |
{"role": "user", "content": prompt}
|
27 |
],
|
28 |
temperature=0.5
|
29 |
)
|
30 |
+
return resp.choices[0].message.content.strip()
|
31 |
|
|
|
32 |
def process_image(image):
|
33 |
try:
|
|
|
34 |
if isinstance(image, np.ndarray):
|
35 |
image = Image.fromarray(image)
|
36 |
elif isinstance(image, str):
|
37 |
image = Image.open(image)
|
38 |
|
39 |
+
# π Crack segmentation inference
|
40 |
+
results = model.predict(source=image, imgsz=640, verbose=False)[0]
|
41 |
+
masks = results.masks.data if results.masks is not None else []
|
42 |
+
count = len(masks)
|
|
|
43 |
|
44 |
+
if count > 0:
|
45 |
+
detected_info = f"{count} crack(s) detected"
|
46 |
+
prompt_prefix = detected_info
|
47 |
+
else:
|
48 |
+
prompt_prefix = (
|
49 |
+
"No cracks were detected by the vision model, "
|
50 |
+
"but the image may contain hidden surface damage. "
|
51 |
+
"Please analyze contextually."
|
52 |
+
)
|
53 |
|
|
|
54 |
user_prompt = f"""
|
55 |
+
{prompt_prefix}.
|
56 |
Please:
|
57 |
+
- Diagnose the issue
|
58 |
+
- Suggest repair methods
|
59 |
- List tools/materials required
|
60 |
- Estimate repair time
|
61 |
"""
|
62 |
|
63 |
+
return ask_groq(user_prompt)
|
|
|
64 |
|
65 |
except Exception as e:
|
66 |
+
return f"β Error: {e}"
|
67 |
|
|
|
68 |
with gr.Blocks() as demo:
|
69 |
+
gr.Markdown("## π§ Construction Crack Analyzer")
|
70 |
+
gr.Markdown("Upload an image of a wall or surface to detect and analyze cracks.")
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
img_input = gr.Image(type="numpy", label="Upload Damage Image")
|
73 |
+
output_text = gr.Textbox(label="Diagnosis & Recommendations", lines=8)
|
74 |
+
gr.Button("Analyze").click(fn=process_image, inputs=img_input, outputs=output_text)
|
75 |
|
76 |
demo.launch()
|