Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client with API key (set this as a secret in HF Spaces)
|
| 10 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 11 |
+
|
| 12 |
+
# Helper: Convert PIL Image to base64
|
| 13 |
+
def image_to_base64(image):
|
| 14 |
+
buffered = io.BytesIO()
|
| 15 |
+
image.save(buffered, format="JPEG")
|
| 16 |
+
return base64.b64encode(buffered.getvalue()).decode()
|
| 17 |
+
|
| 18 |
+
# Prompt for model
|
| 19 |
+
SYSTEM_PROMPT = """
|
| 20 |
+
You are a helpful civil engineering assistant. The user uploads an image showing some construction damage such as cracks, water leakage, or pipe failure. Based on the image, give:
|
| 21 |
+
|
| 22 |
+
1. Likely issue
|
| 23 |
+
2. Possible solution
|
| 24 |
+
3. Tools or materials needed
|
| 25 |
+
4. Estimated time to fix
|
| 26 |
+
|
| 27 |
+
Use simple, helpful, practical language.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
# Chatbot logic
|
| 31 |
+
def analyze_image(image, history):
|
| 32 |
+
if image is None:
|
| 33 |
+
return history + [("User", "No image uploaded."), ("Bot", "Please upload a damage photo.")]
|
| 34 |
+
|
| 35 |
+
base64_img = image_to_base64(image)
|
| 36 |
+
image_url = f"data:image/jpeg;base64,{base64_img}"
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = client.chat.completions.create(
|
| 40 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 41 |
+
messages=[
|
| 42 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 43 |
+
{"role": "user", "content": [
|
| 44 |
+
{"type": "text", "text": "Please analyze this image and give advice on the damage."},
|
| 45 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 46 |
+
]}
|
| 47 |
+
],
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
max_tokens=512
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
reply = response.choices[0].message.content
|
| 53 |
+
history.append(("User", "Uploaded image"))
|
| 54 |
+
history.append(("Bot", reply))
|
| 55 |
+
return history
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return history + [("Bot", f"❌ Error: {str(e)}")]
|
| 58 |
+
|
| 59 |
+
# Gradio Interface
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
gr.Markdown("## 🛠️ Construction Damage Assistant\nUpload a photo of damage to get repair advice.")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column(scale=1):
|
| 65 |
+
image_input = gr.Image(type="pil", label="Upload Damage Image")
|
| 66 |
+
|
| 67 |
+
with gr.Column(scale=2):
|
| 68 |
+
chatbot = gr.Chatbot(label="Repair Suggestions", height=450)
|
| 69 |
+
|
| 70 |
+
state = gr.State([])
|
| 71 |
+
submit_btn = gr.Button("Analyze")
|
| 72 |
+
|
| 73 |
+
submit_btn.click(fn=analyze_image, inputs=[image_input, state], outputs=chatbot)
|
| 74 |
+
|
| 75 |
+
demo.launch()
|