Spaces:
Sleeping
Sleeping
import os | |
import json | |
import time | |
import requests | |
import streamlit as st | |
from openai import OpenAI | |
# -------------------- CONFIG -------------------- | |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") | |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID") | |
st.set_page_config(page_title="Forrestdale Technical Drawing Assistant", layout="wide") | |
st.title("🧱 Forrestdale Technical Drawing Assistant") | |
# -------------------- ERROR CHECK -------------------- | |
if not OPENAI_API_KEY or not ASSISTANT_ID: | |
st.error("❌ Missing API credentials. Please set OPENAI_API_KEY and ASSISTANT_ID as environment variables.") | |
st.stop() | |
client = OpenAI(api_key=OPENAI_API_KEY) | |
# -------------------- CHAT SESSION -------------------- | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
if "thread_id" not in st.session_state: | |
st.session_state.thread_id = None | |
query = st.text_input("Ask about plans, drawings or components", placeholder="e.g. Show me all electrical plans") | |
if query: | |
with st.spinner("Querying assistant..."): | |
try: | |
# Create thread if needed | |
if not st.session_state.thread_id: | |
thread = client.beta.threads.create() | |
st.session_state.thread_id = thread.id | |
# Submit message | |
client.beta.threads.messages.create( | |
thread_id=st.session_state.thread_id, | |
role="user", | |
content=query | |
) | |
# Run assistant | |
run = client.beta.threads.runs.create( | |
thread_id=st.session_state.thread_id, | |
assistant_id=ASSISTANT_ID | |
) | |
# Poll for result | |
while True: | |
run_status = client.beta.threads.runs.retrieve( | |
thread_id=st.session_state.thread_id, | |
run_id=run.id | |
) | |
if run_status.status == "completed": | |
break | |
elif run_status.status in ("failed", "cancelled"): | |
raise Exception(f"Assistant failed: {run_status.status}") | |
time.sleep(1) | |
# Fetch assistant message | |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id) | |
for message in reversed(messages.data): | |
if message.role == "assistant": | |
content = message.content[0].text.value.strip() | |
if content.startswith("```json") and content.endswith("```"): | |
json_block = content.strip("```json\n").strip("```") | |
parsed = json.loads(json_block) | |
st.session_state.messages.append(parsed) | |
else: | |
st.error("⚠️ Could not parse assistant response as JSON.") | |
break | |
except Exception as e: | |
st.error(f"❌ Error: {e}") | |
# -------------------- DISPLAY RESULTS -------------------- | |
if st.session_state.messages: | |
drawings = st.session_state.messages[-1] | |
if isinstance(drawings, list): | |
cols = st.columns(4) | |
for idx, item in enumerate(drawings): | |
with cols[idx % 4]: | |
st.subheader(f"📘 {item['drawing_number']}") | |
st.caption(f"Discipline: {item['discipline']}") | |
st.write(item.get("summary", "No summary available.")) | |
if "images" in item: | |
for img in item["images"][:1]: | |
st.image(img, use_column_width=True) | |
elif "image" in item: | |
st.image(item["image"], use_column_width=True) | |
if "question" in item: | |
st.markdown(f"**Related Question:** {item['question']}") | |