Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,97 @@
|
|
1 |
-
import
|
2 |
import os
|
3 |
-
import time
|
4 |
import json
|
5 |
-
import re
|
6 |
from openai import OpenAI
|
7 |
|
8 |
-
#
|
9 |
-
st.set_page_config(page_title="Forrestdale Drawing Viewer", layout="wide")
|
10 |
-
st.title("π Forrestdale Technical Drawing Assistant")
|
11 |
-
|
12 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
13 |
-
ASSISTANT_ID = "
|
14 |
-
|
15 |
-
if not OPENAI_API_KEY:
|
16 |
-
st.error("β Missing OPENAI_API_KEY.")
|
17 |
-
st.stop()
|
18 |
-
|
19 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
if "
|
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 |
-
break
|
58 |
-
time.sleep(1)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
messages = client.beta.threads.messages.list(thread_id=st.session_state.tech_thread_id)
|
64 |
-
for message in reversed(messages.data):
|
65 |
-
if message.role == "assistant":
|
66 |
-
reply_content = message.content[0].text.value.strip()
|
67 |
-
st.session_state.tech_messages.append({"role": "assistant", "content": reply_content})
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
json_str = match.group(1) if match else reply_content
|
72 |
-
results = json.loads(json_str)
|
73 |
|
74 |
-
|
75 |
-
cols = st.columns(4)
|
76 |
-
for idx, item in enumerate(results):
|
77 |
-
with cols[idx % 4]:
|
78 |
-
with st.container(border=True):
|
79 |
-
st.markdown(f"### π {item.get('drawing_number')} ({item.get('discipline')})", help=item.get("summary"))
|
80 |
-
st.caption(item.get("summary"))
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
if item.get("image"):
|
86 |
-
st.image(item.get("image"), caption=item.get("drawing_number"))
|
87 |
-
elif item.get("images"):
|
88 |
-
for i, img in enumerate(item["images"]):
|
89 |
-
if img.startswith("http"):
|
90 |
-
st.image(img, caption=f"{item['drawing_number']} β Page {i+1}")
|
91 |
-
except Exception as parse_error:
|
92 |
-
st.warning("π‘ Could not parse assistant response as JSON.")
|
93 |
-
break
|
94 |
-
except Exception as e:
|
95 |
-
st.error(f"β Error occurred: {e}")
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
|
|
3 |
import json
|
|
|
4 |
from openai import OpenAI
|
5 |
|
6 |
+
# ========== Config ==========
|
|
|
|
|
|
|
7 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
8 |
+
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
|
|
|
|
|
|
|
|
|
|
9 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
10 |
|
11 |
+
# ========== Functions ==========
|
12 |
+
def query_assistant(message):
|
13 |
+
try:
|
14 |
+
thread = client.beta.threads.create()
|
15 |
+
client.beta.threads.messages.create(
|
16 |
+
thread_id=thread.id,
|
17 |
+
role="user",
|
18 |
+
content=message
|
19 |
+
)
|
20 |
+
run = client.beta.threads.runs.create(
|
21 |
+
thread_id=thread.id,
|
22 |
+
assistant_id=ASSISTANT_ID
|
23 |
+
)
|
24 |
+
|
25 |
+
# Poll run status
|
26 |
+
while True:
|
27 |
+
run_status = client.beta.threads.runs.retrieve(
|
28 |
+
thread_id=thread.id,
|
29 |
+
run_id=run.id
|
30 |
+
)
|
31 |
+
if run_status.status in ("completed", "failed", "cancelled"):
|
32 |
+
break
|
33 |
|
34 |
+
if run_status.status != "completed":
|
35 |
+
return f"β Assistant failed: {run_status.status}", []
|
36 |
|
37 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
38 |
+
for m in reversed(messages.data):
|
39 |
+
if m.role == "assistant":
|
40 |
+
try:
|
41 |
+
parsed = json.loads(m.content[0].text.value)
|
42 |
+
return None, parsed.get("results", [])
|
43 |
+
except Exception as e:
|
44 |
+
return f"β οΈ Failed to parse assistant response: {e}", []
|
45 |
+
return "β οΈ No assistant response found", []
|
46 |
+
except Exception as e:
|
47 |
+
return f"β Error: {e}", []
|
48 |
|
49 |
+
def render_cards(results):
|
50 |
+
cards = []
|
51 |
+
for r in results:
|
52 |
+
title = r.get("drawing_title", "Untitled")
|
53 |
+
summary = r.get("summary", "")
|
54 |
+
pages = r.get("pages", [])
|
55 |
+
image_elems = []
|
56 |
+
for page in pages:
|
57 |
+
url = page.get("public_image_url")
|
58 |
+
label = f"{title} β Page {page.get('page_number')}"
|
59 |
+
if url:
|
60 |
+
image_elems.append(gr.Image(value=url, label=label, show_label=True))
|
61 |
|
62 |
+
with gr.Column(scale=1):
|
63 |
+
cards.append(
|
64 |
+
gr.Group(
|
65 |
+
[
|
66 |
+
gr.Markdown(f"**{title}**\n\n**Summary:** {summary}"),
|
67 |
+
gr.Accordion("π View Drawing Pages", open=False, children=image_elems)
|
68 |
+
]
|
69 |
+
)
|
70 |
)
|
71 |
+
return cards
|
72 |
|
73 |
+
def on_query_submit(prompt):
|
74 |
+
status, results = query_assistant(prompt)
|
75 |
+
if status:
|
76 |
+
return status, []
|
77 |
+
return "β
Found matching drawings", render_cards(results)
|
78 |
|
79 |
+
# ========== Interface ==========
|
80 |
+
with gr.Blocks(theme=gr.themes.Base(), title="Forrestdale Technical Drawing Assistant") as app:
|
81 |
+
gr.Markdown("""
|
82 |
+
# ποΈ Forrestdale Technical Drawing Assistant
|
83 |
+
Ask about plans, drawings or components (e.g. _Show me all electrical plans_)
|
84 |
+
""")
|
|
|
|
|
85 |
|
86 |
+
with gr.Row():
|
87 |
+
query_input = gr.Textbox(placeholder="e.g. Show me all civil drainage plans", scale=5)
|
88 |
+
query_button = gr.Button("π Search", scale=1)
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
status_output = gr.Markdown("", visible=True)
|
91 |
+
result_display = gr.Column()
|
|
|
|
|
92 |
|
93 |
+
query_button.click(fn=on_query_submit, inputs=query_input, outputs=[status_output, result_display])
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
# ========== Launch ==========
|
96 |
+
if __name__ == "__main__":
|
97 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|