Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# β
Load API key from environment
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
GROQ_MODEL = "llama3-70b-8192"
|
| 10 |
+
|
| 11 |
+
# π§ Simulated caption (replace later)
|
| 12 |
+
def get_caption(image):
|
| 13 |
+
return "This image shows a large crack in the wall extending diagonally."
|
| 14 |
+
|
| 15 |
+
# π¬ Send prompt to Groq
|
| 16 |
+
def generate_response(image, user_query=""):
|
| 17 |
+
if GROQ_API_KEY is None:
|
| 18 |
+
return "β API key not set. Please set GROQ_API_KEY using os.environ."
|
| 19 |
+
|
| 20 |
+
caption = get_caption(image)
|
| 21 |
+
|
| 22 |
+
prompt = f"""
|
| 23 |
+
You are a construction damage analysis assistant. Based on the description: "{caption}", and user question: "{user_query}", provide the following:
|
| 24 |
+
1. Type of damage
|
| 25 |
+
2. Likely cause
|
| 26 |
+
3. Suggested repair solutions
|
| 27 |
+
4. Required tools
|
| 28 |
+
5. Estimated time and cost
|
| 29 |
+
|
| 30 |
+
Respond in a helpful tone.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
headers = {
|
| 34 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 35 |
+
"Content-Type": "application/json"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
data = {
|
| 39 |
+
"model": GROQ_MODEL,
|
| 40 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
|
| 44 |
+
|
| 45 |
+
if response.status_code == 200:
|
| 46 |
+
return response.json()['choices'][0]['message']['content']
|
| 47 |
+
else:
|
| 48 |
+
return f"β Error {response.status_code}: {response.text}"
|
| 49 |
+
|
| 50 |
+
# ============================
|
| 51 |
+
# π Gradio Modern UI
|
| 52 |
+
# ============================
|
| 53 |
+
|
| 54 |
+
with gr.Blocks(css="""
|
| 55 |
+
.gradio-container {
|
| 56 |
+
background-color: #f8f9fa;
|
| 57 |
+
font-family: 'Segoe UI', sans-serif;
|
| 58 |
+
}
|
| 59 |
+
#title {
|
| 60 |
+
text-align: center;
|
| 61 |
+
font-size: 32px;
|
| 62 |
+
font-weight: bold;
|
| 63 |
+
padding: 20px 0;
|
| 64 |
+
color: #333;
|
| 65 |
+
}
|
| 66 |
+
#subtitle {
|
| 67 |
+
text-align: center;
|
| 68 |
+
font-size: 16px;
|
| 69 |
+
color: #666;
|
| 70 |
+
margin-bottom: 20px;
|
| 71 |
+
}
|
| 72 |
+
.card {
|
| 73 |
+
border: 1px solid #ddd;
|
| 74 |
+
border-radius: 16px;
|
| 75 |
+
background-color: white;
|
| 76 |
+
padding: 20px;
|
| 77 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.03);
|
| 78 |
+
}
|
| 79 |
+
""") as demo:
|
| 80 |
+
|
| 81 |
+
gr.Markdown("<div id='title'>ποΈ BuildFix AI β Construction Damage Inspector</div>")
|
| 82 |
+
gr.Markdown("<div id='subtitle'>Upload a construction damage image and get expert analysis with repair suggestions instantly.</div>")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=1, elem_classes="card"):
|
| 86 |
+
image_input = gr.Image(type="pil", label="πΈ Upload Image of Damage")
|
| 87 |
+
user_query = gr.Textbox(label="π¬ Ask a follow-up question (optional)", placeholder="e.g., How expensive is this repair?", lines=2)
|
| 88 |
+
submit_btn = gr.Button("π Analyze", variant="primary")
|
| 89 |
+
|
| 90 |
+
with gr.Column(scale=1, elem_classes="card"):
|
| 91 |
+
result_output = gr.Textbox(label="π§ AI Report", lines=20)
|
| 92 |
+
|
| 93 |
+
submit_btn.click(fn=generate_response, inputs=[image_input, user_query], outputs=result_output)
|
| 94 |
+
|
| 95 |
+
demo.launch()
|