Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# Load API key from Streamlit secrets
|
| 10 |
+
groq_api_key = st.secrets["GROQ_API_KEY"]
|
| 11 |
+
|
| 12 |
+
# Initialize Groq client
|
| 13 |
+
client = Groq(api_key=groq_api_key)
|
| 14 |
+
|
| 15 |
+
# Load YOLOv8 model (pretrained on COCO dataset)
|
| 16 |
+
model = YOLO("yolov8n.pt")
|
| 17 |
+
|
| 18 |
+
# Function to get AI-powered recommendations
|
| 19 |
+
def get_ai_recommendation(detected_objects):
|
| 20 |
+
if not detected_objects:
|
| 21 |
+
return "No objects detected. Please try again with a clearer image."
|
| 22 |
+
|
| 23 |
+
prompt = f"Suggest optimal placement and design improvements for: {', '.join(detected_objects)} in a room."
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
chat_completion = client.chat.completions.create(
|
| 27 |
+
messages=[{"role": "user", "content": prompt}],
|
| 28 |
+
model="llama-3.3-70b-versatile"
|
| 29 |
+
)
|
| 30 |
+
return chat_completion.choices[0].message.content
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error fetching AI recommendation: {e}"
|
| 33 |
+
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
st.title("🏡 AI-Powered Home Setup Chatbot")
|
| 36 |
+
st.write("Upload an image of your room or use your camera for live capture.")
|
| 37 |
+
|
| 38 |
+
# Upload image option
|
| 39 |
+
uploaded_file = st.file_uploader("Upload an image of your room:", type=["jpg", "jpeg", "png"])
|
| 40 |
+
|
| 41 |
+
# Live camera capture
|
| 42 |
+
use_camera = st.checkbox("Use Live Camera")
|
| 43 |
+
|
| 44 |
+
if use_camera:
|
| 45 |
+
st.write("Click 'Capture Image' to take a photo.")
|
| 46 |
+
camera = cv2.VideoCapture(0) # Open the webcam
|
| 47 |
+
|
| 48 |
+
if st.button("Capture Image"):
|
| 49 |
+
ret, frame = camera.read()
|
| 50 |
+
if ret:
|
| 51 |
+
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 52 |
+
captured_image = Image.fromarray(image)
|
| 53 |
+
st.image(captured_image, caption="Captured Image", use_column_width=True)
|
| 54 |
+
|
| 55 |
+
# Run YOLOv8 for object detection
|
| 56 |
+
results = model(image)
|
| 57 |
+
detected_objects = [model.names[int(box.cls)] for box in results[0].boxes]
|
| 58 |
+
|
| 59 |
+
# Display detected objects
|
| 60 |
+
st.subheader("Detected Objects:")
|
| 61 |
+
st.write(", ".join(set(detected_objects)))
|
| 62 |
+
|
| 63 |
+
# Get AI-powered recommendations
|
| 64 |
+
st.subheader("AI-Powered Placement Suggestions:")
|
| 65 |
+
ai_recommendations = get_ai_recommendation(detected_objects)
|
| 66 |
+
st.write(ai_recommendations)
|
| 67 |
+
else:
|
| 68 |
+
st.error("Failed to capture image. Please check your camera and try again.")
|
| 69 |
+
|
| 70 |
+
camera.release()
|
| 71 |
+
cv2.destroyAllWindows()
|
| 72 |
+
|
| 73 |
+
elif uploaded_file:
|
| 74 |
+
image = Image.open(uploaded_file)
|
| 75 |
+
image = np.array(image)
|
| 76 |
+
|
| 77 |
+
# Run YOLOv8 for object detection
|
| 78 |
+
results = model(image)
|
| 79 |
+
detected_objects = [model.names[int(box.cls)] for box in results[0].boxes]
|
| 80 |
+
|
| 81 |
+
# Display uploaded image and detected objects
|
| 82 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 83 |
+
st.subheader("Detected Objects:")
|
| 84 |
+
st.write(", ".join(set(detected_objects)))
|
| 85 |
+
|
| 86 |
+
# Get AI-powered recommendations
|
| 87 |
+
st.subheader("AI-Powered Placement Suggestions:")
|
| 88 |
+
ai_recommendations = get_ai_recommendation(detected_objects)
|
| 89 |
+
st.write(ai_recommendations)
|
| 90 |
+
|
| 91 |
+
# Text-based recommendations
|
| 92 |
+
user_input = st.text_input("Or enter an object name:")
|
| 93 |
+
if user_input:
|
| 94 |
+
ai_recommendation = get_ai_recommendation([user_input.lower()])
|
| 95 |
+
st.subheader("AI-Powered Suggestion:")
|
| 96 |
+
st.write(ai_recommendation)
|