Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,9 @@ import streamlit as st
|
|
2 |
import os
|
3 |
import time
|
4 |
import re
|
|
|
|
|
|
|
5 |
from openai import OpenAI
|
6 |
|
7 |
# ------------------ App Configuration ------------------
|
@@ -14,7 +17,7 @@ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
|
14 |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
15 |
|
16 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
17 |
-
st.error("❌ Missing secrets. Please set both OPENAI_API_KEY and ASSISTANT_ID in Hugging Face Space secrets.")
|
18 |
st.stop()
|
19 |
|
20 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
@@ -46,8 +49,14 @@ col1, col2 = st.columns([1, 2])
|
|
46 |
# ------------------ Left Panel: Image ------------------
|
47 |
with col1:
|
48 |
if show_image and st.session_state.image_url:
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# ------------------ Right Panel: Chat ------------------
|
53 |
with col2:
|
@@ -78,7 +87,7 @@ with col2:
|
|
78 |
thread = client.beta.threads.create()
|
79 |
st.session_state.thread_id = thread.id
|
80 |
|
81 |
-
# Send
|
82 |
client.beta.threads.messages.create(
|
83 |
thread_id=st.session_state.thread_id,
|
84 |
role="user",
|
@@ -112,7 +121,7 @@ with col2:
|
|
112 |
|
113 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
114 |
|
115 |
-
# ✅
|
116 |
image_match = re.search(
|
117 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/[a-zA-Z0-9_\-/]+\.png',
|
118 |
assistant_message
|
|
|
2 |
import os
|
3 |
import time
|
4 |
import re
|
5 |
+
import requests
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
from openai import OpenAI
|
9 |
|
10 |
# ------------------ App Configuration ------------------
|
|
|
17 |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
18 |
|
19 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
20 |
+
st.error("❌ Missing secrets. Please set both OPENAI_API_KEY and ASSISTANT_ID in your Hugging Face Space secrets.")
|
21 |
st.stop()
|
22 |
|
23 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
49 |
# ------------------ Left Panel: Image ------------------
|
50 |
with col1:
|
51 |
if show_image and st.session_state.image_url:
|
52 |
+
try:
|
53 |
+
response = requests.get(st.session_state.image_url)
|
54 |
+
response.raise_for_status()
|
55 |
+
img = Image.open(BytesIO(response.content))
|
56 |
+
st.image(img, caption="📑 Extracted Page", use_container_width=True)
|
57 |
+
st.session_state.image_updated = False
|
58 |
+
except Exception as e:
|
59 |
+
st.warning(f"⚠️ Failed to load image from URL:\n{st.session_state.image_url}\n\nError: {e}")
|
60 |
|
61 |
# ------------------ Right Panel: Chat ------------------
|
62 |
with col2:
|
|
|
87 |
thread = client.beta.threads.create()
|
88 |
st.session_state.thread_id = thread.id
|
89 |
|
90 |
+
# Send user prompt
|
91 |
client.beta.threads.messages.create(
|
92 |
thread_id=st.session_state.thread_id,
|
93 |
role="user",
|
|
|
121 |
|
122 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
123 |
|
124 |
+
# ✅ Extract GitHub raw image URL
|
125 |
image_match = re.search(
|
126 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/[a-zA-Z0-9_\-/]+\.png',
|
127 |
assistant_message
|