Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323334351 unmodified line37383940414243444546474849505126 unmodified lines78798081828384858687888990919293942 unmodified linesfrom 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 costRespond in a helpful tone."""headers = {"Authorization": f"Bearer {GROQ_API_KEY}","Content-Type": "application/json"1 unmodified linedata = {"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 UI26 unmodified lines}""") 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)2 unmodified lines345678910111213141516171819202122232425262728293031323334351 unmodified line373839404142434445464748495051525354555657585926 unmodified lines86878889909192939495969798991001011022 unmodified linesfrom PIL import Imagefrom io import BytesIOimport osimport torchfrom transformers import BlipProcessor, BlipForConditionalGeneration# β Load Groq API key from environment (add this in Hugging Face Secrets)GROQ_API_KEY = os.getenv("GROQ_API_KEY")GROQ_MODEL = "llama3-70b-8192"# β Load BLIP model for image captioningprocessor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")# πΈ Generate caption from imagedef get_caption(image):inputs = processor(images=image, return_tensors="pt")out = blip_model.generate(**inputs)caption = processor.decode(out[0], skip_special_tokens=True)return caption# π¬ Communicate with Groq APIdef generate_response(image, user_query=""):if GROQ_API_KEY is None:return "β API key not set. Please set GROQ_API_KEY using Hugging Face Secrets."caption = get_caption(image)headers = {"Authorization": f"Bearer {GROQ_API_KEY}","Content-Type": "application/json"1 unmodified linedata = {"model": GROQ_MODEL,"messages": [{"role": "system", "content": "You are a helpful assistant that analyzes construction damage from image captions and gives expert repair advice."},{"role": "user", "content": f"Image caption: {caption}"},{"role": "user", "content": f"My question is: {user_query or 'No follow-up question.'}"},{"role": "user", "content": "Please provide:\n1. Type of damage\n2. Likely cause\n3. Suggested repair solutions\n4. Required tools\n5. Estimated time and cost"}]}try:response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data, timeout=30)if response.status_code == 200:content = response.json()['choices'][0]['message']['content']return content if content.strip() else "β οΈ No response received. Please try again."else:return f"β Error {response.status_code}: {response.text}"except Exception as e:return f"β Exception: {str(e)}"# ============================# π Gradio Modern UI26 unmodified lines}""") as demo:gr.Markdown("<div id='title'>\ud83c\udfd7\ufe0f 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="\ud83d\udcf8 Upload Image of Damage")user_query = gr.Textbox(label="\ud83d\udcac Ask a follow-up question (optional)", placeholder="e.g., How expensive is this repair?", lines=2)submit_btn = gr.Button("\ud83d\udd0d Analyze", variant="primary")with gr.Column(scale=1, elem_classes="card"):result_output = gr.Textbox(label="\ud83e\uddd0 AI Report", lines=20)submit_btn.click(fn=generate_response, inputs=[image_input, user_query], outputs=result_output)
app.py+36-28