Spaces:
Sleeping
Sleeping
File size: 3,230 Bytes
d3205b2 13919c8 930629f d3205b2 b386f62 d3205b2 6aa98b4 d3205b2 56b7b50 d3205b2 6aa98b4 d3205b2 2e0eb4d d3205b2 2e0eb4d d3205b2 56b7b50 d3205b2 56b7b50 d3205b2 56b7b50 d3205b2 930629f d3205b2 7794a19 d3205b2 dc07f4a d3205b2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import streamlit as st
import os
import json
import time
import requests
from openai import OpenAI
# ------------------ App Configuration ------------------
st.set_page_config(page_title="Forrestdale Drawing Assistant", layout="wide")
st.title("📐 Forrestdale Technical Drawing Assistant")
# ------------------ Load API Key and Assistant ID ------------------
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
if not OPENAI_API_KEY or not ASSISTANT_ID:
st.error("❌ Missing secrets. Please set OPENAI_API_KEY and ASSISTANT_ID in secrets.")
st.stop()
client = OpenAI(api_key=OPENAI_API_KEY)
# ------------------ Session State Init ------------------
if "thread_id" not in st.session_state:
st.session_state.thread_id = None
if "results" not in st.session_state:
st.session_state.results = []
# ------------------ Query Bar ------------------
prompt = st.text_input("Ask about plans, drawings or components", placeholder="e.g. Show me all electrical plans")
if prompt:
if st.session_state.thread_id is None:
thread = client.beta.threads.create()
st.session_state.thread_id = thread.id
client.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role="user",
content=prompt
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id=ASSISTANT_ID
)
with st.spinner("Thinking..."):
while True:
run_status = client.beta.threads.runs.retrieve(
thread_id=st.session_state.thread_id,
run_id=run.id
)
if run_status.status in ("completed", "failed", "cancelled"):
break
time.sleep(1)
if run_status.status == "completed":
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
for message in reversed(messages.data):
if message.role == "assistant":
try:
json_data = json.loads(message.content[0].text.value)
st.session_state.results = json_data["results"]
except Exception as e:
st.error("⚠️ Could not parse assistant response as JSON.")
break
else:
st.error(f"⚠️ Assistant failed: {run_status.status}")
# ------------------ Display Results as Cards ------------------
if st.session_state.results:
cols = st.columns(4)
for i, result in enumerate(st.session_state.results):
with cols[i % 4]:
st.markdown("""
<div style='border:1px solid #444; padding:10px; border-radius:10px; background:#111;'>
<h5 style='color:#fdd;'>📁 {drawing_number}</h5>
<b>Summary:</b> {summary}<br>
<img src="{image_url}" style="width:100%; margin-top:10px; border-radius:6px;">
</div>
""".format(
drawing_number=result.get("drawing_number", "Untitled"),
summary=result.get("summary", "No summary available."),
image_url=result.get("image_url", "")
), unsafe_allow_html=True)
|