IAMTFRMZA commited on
Commit
7f2459b
Β·
verified Β·
1 Parent(s): 28c7bd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
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
- # ---------------- Environment & Client Setup ----------------
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
- # ---------------- 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,7 +74,7 @@ def clear_transcript(cid):
74
  if cid in connections: connections[cid].transcript = ""
75
  return ""
76
 
77
- # ---------------- Chat Functionality ----------------
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
- # ---------------- Gradio UI Layout ----------------
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
- with gr.Column(scale=1):
121
- # IMAGE VIEWER (left)
122
- image_display = gr.Image(label="πŸ–ΌοΈ Document", type="filepath")
123
-
124
- # VOICE (under)
125
- voice_transcript = gr.Textbox(label="πŸŽ™οΈ Transcript", lines=4, interactive=False)
126
- voice_input = gr.Audio(label="πŸ”΄ Record", streaming=True)
127
  clear_btn = gr.Button("🧹 Clear Transcript")
128
 
129
- with gr.Column(scale=2):
130
- # CHATBOT (right)
131
- chat = gr.Chatbot(label="πŸ’¬ Chat", height=450)
132
- user_prompt = gr.Textbox(show_label=False, placeholder="Ask your question...")
133
- send_btn = gr.Button("Send")
134
 
135
- # HANDLERS
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])