Spaces:
Runtime error
Runtime error
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import gradio as grimport requestsfrom PIL import Imagefrom io import BytesIOimport os
# β
Load API key from environmentGROQ_API_KEY = os.getenv("GROQ_API_KEY")GROQ_MODEL = "llama3-70b-8192"
# π§ Simulated caption (replace later)def get_caption(image): return "This image shows a large crack in the wall extending diagonally."
# π¬ Send prompt to Groqdef generate_response(image, user_query=""): if GROQ_API_KEY is None: return "β API key not set. Please set GROQ_API_KEY using os.environ."
caption = get_caption(image)
prompt = f"""You are a construction damage analysis assistant. Based on the description: "{caption}", and user question: "{user_query}", provide the following:1. Type of damage2. Likely cause3. Suggested repair solutions4. Required tools5. Estimated time and cost
Respond in a helpful tone."""
headers = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" }
data = { "model": GROQ_MODEL, "messages": [{"role": "user", "content": prompt}] }
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data) if response.status_code == 200: return response.json()['choices'][0]['message']['content'] else: return f"β Error {response.status_code}: {response.text}"
# ============================# π Gradio Modern UI# ============================
with gr.Blocks(css=""".gradio-container { background-color: #f8f9fa; font-family: 'Segoe UI', sans-serif;}#title { text-align: center; font-size: 32px; font-weight: bold; padding: 20px 0; color: #333;}#subtitle { text-align: center; font-size: 16px; color: #666; margin-bottom: 20px;}.card { border: 1px solid #ddd; border-radius: 16px; background-color: white; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.03);}""") as demo:
gr.Markdown("<div id='title'>ποΈ BuildFix AI β Construction Damage Inspector</div>") gr.Markdown("<div id='subtitle'>Upload a construction damage image and get expert analysis with repair suggestions instantly.</div>")
with gr.Row(): with gr.Column(scale=1, elem_classes="card"): image_input = gr.Image(type="pil", label="πΈ Upload Image of Damage") user_query = gr.Textbox(label="π¬ Ask a follow-up question (optional)", placeholder="e.g., How expensive is this repair?", lines=2) submit_btn = gr.Button("π Analyze", variant="primary")
with gr.Column(scale=1, elem_classes="card"): result_output = gr.Textbox(label="π§ AI Report", lines=20)
submit_btn.click(fn=generate_response, inputs=[image_input, user_query], outputs=result_output)
demo.launch()app.pyAdded+95