IqraFatima commited on
Commit
f7b150e
Β·
verified Β·
1 Parent(s): d4a9a09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -33
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"), # Hugging Face reads from environment variables
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
- # βœ… Function to ask Groq LLaMA 3 model for repair advice
 
 
 
16
  def ask_groq(prompt):
17
- response = client.chat.completions.create(
18
- model="llama3-8b-8192", # Groq-supported model
19
  messages=[
20
- {"role": "system", "content": "You are an expert in construction damage assessment."},
21
  {"role": "user", "content": prompt}
22
  ],
23
  temperature=0.5
24
  )
25
- return response.choices[0].message.content.strip()
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
- # Run YOLOv8 for object detection
37
- results = model(image)
38
- labels = results[0].names
39
- boxes = results[0].boxes
40
- detected = [labels[int(cls)] for cls in boxes.cls]
41
 
42
- detected_info = ", ".join(set(detected)) or "no damage detected"
 
 
 
 
 
 
 
 
43
 
44
- # Prompt sent to Groq's LLaMA 3 model
45
  user_prompt = f"""
46
- The uploaded image shows: {detected_info}.
47
  Please:
48
- - Diagnose the construction issue
49
- - Suggest possible repair solutions
50
  - List tools/materials required
51
  - Estimate repair time
52
  """
53
 
54
- response = ask_groq(user_prompt)
55
- return response
56
 
57
  except Exception as e:
58
- return f"❌ Error: {str(e)}"
59
 
60
- # βœ… Gradio UI
61
  with gr.Blocks() as demo:
62
- gr.Markdown("## πŸ—οΈ Construction Damage Analyzer")
63
- gr.Markdown("Upload a photo of damaged wall, pipe, or structure. Get diagnosis and repair suggestions from AI.")
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
- submit_btn = gr.Button("Analyze")
70
- submit_btn.click(fn=process_image, inputs=img_input, outputs=output_text)
 
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()