Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ from openai import OpenAI
|
|
7 |
from websockets import connect, Data, ClientConnection
|
8 |
from dotenv import load_dotenv
|
9 |
|
10 |
-
#
|
11 |
load_dotenv()
|
12 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
13 |
ASSISTANT_ID = os.getenv("ASSISTANT_ID")
|
@@ -17,7 +17,7 @@ HEADERS = {"Authorization": f"Bearer {OPENAI_API_KEY}", "OpenAI-Beta": "realtime
|
|
17 |
WS_URI = "wss://api.openai.com/v1/realtime?intent=transcription"
|
18 |
connections = {}
|
19 |
|
20 |
-
#
|
21 |
class WebSocketClient:
|
22 |
def __init__(self, uri, headers, client_id):
|
23 |
self.uri, self.headers, self.client_id = uri, headers, client_id
|
@@ -74,7 +74,7 @@ def clear_transcript(cid):
|
|
74 |
if cid in connections: connections[cid].transcript = ""
|
75 |
return ""
|
76 |
|
77 |
-
#
|
78 |
def handle_chat(user_input, history, thread_id, image_url):
|
79 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
80 |
return "β Missing secrets!", history, thread_id, image_url
|
@@ -106,7 +106,7 @@ def handle_chat(user_input, history, thread_id, image_url):
|
|
106 |
except Exception as e:
|
107 |
return f"β {e}", history, thread_id, image_url
|
108 |
|
109 |
-
#
|
110 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
111 |
gr.Markdown("# π Document AI Assistant")
|
112 |
|
@@ -116,23 +116,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
116 |
image_state = gr.State()
|
117 |
client_id = gr.State()
|
118 |
|
119 |
-
with gr.Row():
|
120 |
-
|
121 |
-
|
122 |
-
image_display = gr.Image(label="πΌοΈ Document", type="filepath")
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
voice_input = gr.Audio(label="π΄ Record", streaming=True)
|
127 |
clear_btn = gr.Button("π§Ή Clear Transcript")
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
chat = gr.Chatbot(label="π¬ Chat", height=
|
132 |
-
user_prompt = gr.Textbox(
|
133 |
-
send_btn = gr.Button("Send")
|
134 |
|
135 |
-
#
|
136 |
send_btn.click(handle_chat,
|
137 |
inputs=[user_prompt, chat_state, thread_state, image_state],
|
138 |
outputs=[user_prompt, chat, thread_state, image_state])
|
|
|
7 |
from websockets import connect, Data, ClientConnection
|
8 |
from dotenv import load_dotenv
|
9 |
|
10 |
+
# ============ Load Secrets ============
|
11 |
load_dotenv()
|
12 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
13 |
ASSISTANT_ID = os.getenv("ASSISTANT_ID")
|
|
|
17 |
WS_URI = "wss://api.openai.com/v1/realtime?intent=transcription"
|
18 |
connections = {}
|
19 |
|
20 |
+
# ============ WebSocket Client for Voice ============
|
21 |
class WebSocketClient:
|
22 |
def __init__(self, uri, headers, client_id):
|
23 |
self.uri, self.headers, self.client_id = uri, headers, client_id
|
|
|
74 |
if cid in connections: connections[cid].transcript = ""
|
75 |
return ""
|
76 |
|
77 |
+
# ============ Chat Assistant Logic ============
|
78 |
def handle_chat(user_input, history, thread_id, image_url):
|
79 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
80 |
return "β Missing secrets!", history, thread_id, image_url
|
|
|
106 |
except Exception as e:
|
107 |
return f"β {e}", history, thread_id, image_url
|
108 |
|
109 |
+
# ============ Gradio UI ============
|
110 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
111 |
gr.Markdown("# π Document AI Assistant")
|
112 |
|
|
|
116 |
image_state = gr.State()
|
117 |
client_id = gr.State()
|
118 |
|
119 |
+
with gr.Row(equal_height=True):
|
120 |
+
# LEFT COLUMN β Image + Voice
|
121 |
+
with gr.Column(scale=1, min_width=400):
|
122 |
+
image_display = gr.Image(label="πΌοΈ Document Image", type="filepath", show_download_button=False, show_label=False)
|
123 |
+
with gr.Row():
|
124 |
+
voice_input = gr.Audio(label="ποΈ Mic", streaming=True)
|
125 |
+
voice_transcript = gr.Textbox(label="π Transcript", lines=3, interactive=False)
|
|
|
126 |
clear_btn = gr.Button("π§Ή Clear Transcript")
|
127 |
|
128 |
+
# RIGHT COLUMN β Chatbot
|
129 |
+
with gr.Column(scale=1.4, min_width=500):
|
130 |
+
chat = gr.Chatbot(label="π¬ Chat", height=480)
|
131 |
+
user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False)
|
132 |
+
send_btn = gr.Button("Send", variant="primary")
|
133 |
|
134 |
+
# HOOKS
|
135 |
send_btn.click(handle_chat,
|
136 |
inputs=[user_prompt, chat_state, thread_state, image_state],
|
137 |
outputs=[user_prompt, chat, thread_state, image_state])
|