Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# === STEP 1: Get Groq API Key from HF Space Secret ===
|
| 7 |
+
GROQ_API_KEY = os.getenv("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") # Define this in your Space settings!
|
| 8 |
+
GROQ_MODEL = "llama3-70b-8192"
|
| 9 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# === STEP 2: Convert image to base64 ===
|
| 13 |
+
def image_to_base64(image_path):
|
| 14 |
+
with open(image_path, "rb") as image_file:
|
| 15 |
+
image_bytes = image_file.read()
|
| 16 |
+
return base64.b64encode(image_bytes).decode("utf-8")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# === STEP 3: Build the LLM Prompt ===
|
| 20 |
+
def build_prompt(image_b64: str) -> str:
|
| 21 |
+
return f"""
|
| 22 |
+
You are an expert in Non-Destructive Testing (NDT) and industrial defect analysis.
|
| 23 |
+
|
| 24 |
+
A user has uploaded an image of a defected component. Here's the base64 representation of that image (partial for size): {image_b64[:300]}...
|
| 25 |
+
|
| 26 |
+
Analyze the defect and provide structured output with the following format:
|
| 27 |
+
|
| 28 |
+
1. Defect Type:
|
| 29 |
+
2. Recommended NDT Technique(s):
|
| 30 |
+
3. Explanation of Defect Cause:
|
| 31 |
+
4. Proposed Solution/Fix:
|
| 32 |
+
5. Estimated Time to Repair:
|
| 33 |
+
6. Tools Required:
|
| 34 |
+
7. Preventive Measures:
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# === STEP 4: Call Groq API ===
|
| 39 |
+
def query_groq(prompt: str) -> str:
|
| 40 |
+
headers = {
|
| 41 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 42 |
+
"Content-Type": "application/json"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
payload = {
|
| 46 |
+
"model": GROQ_MODEL,
|
| 47 |
+
"messages": [
|
| 48 |
+
{"role": "system", "content": "You are a professional NDT inspector."},
|
| 49 |
+
{"role": "user", "content": prompt}
|
| 50 |
+
],
|
| 51 |
+
"temperature": 0.5,
|
| 52 |
+
"max_tokens": 800
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 56 |
+
|
| 57 |
+
if response.status_code == 200:
|
| 58 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 59 |
+
else:
|
| 60 |
+
return f"Error {response.status_code}: {response.text}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# === STEP 5: Main Function for Gradio ===
|
| 64 |
+
def analyze_defect(image_path):
|
| 65 |
+
if image_path is None:
|
| 66 |
+
return "Please upload an image."
|
| 67 |
+
|
| 68 |
+
image_b64 = image_to_base64(image_path)
|
| 69 |
+
prompt = build_prompt(image_b64)
|
| 70 |
+
response = query_groq(prompt)
|
| 71 |
+
return response
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# === STEP 6: Gradio UI ===
|
| 75 |
+
demo = gr.Interface(
|
| 76 |
+
fn=analyze_defect,
|
| 77 |
+
inputs=gr.Image(type="filepath", label="Upload Defective Component Image"),
|
| 78 |
+
outputs=gr.Textbox(label="Defect Analysis Report"),
|
| 79 |
+
title="🛠️ NDT Defect Analyzer",
|
| 80 |
+
description="Upload an image of a damaged industrial component. The AI will identify the defect, recommend NDT methods, suggest fixes, tools, and future prevention."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# === STEP 7: Launch for Hugging Face Spaces ===
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|