Spaces:
Sleeping
Sleeping
File size: 3,769 Bytes
4a71975 f1f3914 9910527 a68bccd 9910527 4a71975 9910527 80e3a1e 9910527 4a71975 9910527 4a71975 9910527 4a71975 9910527 4a71975 9910527 f1f3914 9910527 f1f3914 9910527 f1f3914 9910527 f1f3914 9910527 f1f3914 9910527 f1f3914 18e25a2 9910527 18e25a2 9910527 18e25a2 9910527 18e25a2 9910527 f1f3914 9910527 |
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 87 88 89 90 91 92 93 94 95 96 |
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']}")
|