IAMTFRMZA commited on
Commit
d6d49d6
Β·
verified Β·
1 Parent(s): 4a071d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import os
3
  import uuid
4
- import threading
5
  from openai import OpenAI
6
  from realtime_transcriber import WebSocketClient, connections, WEBSOCKET_URI, WEBSOCKET_HEADERS
7
 
@@ -15,12 +14,14 @@ client = OpenAI(api_key=OPENAI_API_KEY)
15
  session_id = str(uuid.uuid4())
16
  if session_id not in connections:
17
  connections[session_id] = WebSocketClient(WEBSOCKET_URI, WEBSOCKET_HEADERS, session_id)
 
18
  threading.Thread(target=connections[session_id].run, daemon=True).start()
19
 
20
  # Functions for Document Assistant
21
  def process_user_input(message, history):
22
  if not message:
23
  return "Please enter a message.", history
 
24
  try:
25
  thread = client.beta.threads.create()
26
  client.beta.threads.messages.create(
@@ -44,7 +45,7 @@ def process_user_input(message, history):
44
  history.append((message, assistant_reply))
45
  return "", history
46
  except Exception as e:
47
- return f"\u274c Error: {str(e)}", history
48
 
49
  # Functions for Realtime Voice Transcription
50
  def send_audio_chunk_realtime(mic_chunk):
@@ -61,23 +62,23 @@ def clear_transcript():
61
  return ""
62
 
63
  # Gradio UI Components
64
- doc_image = gr.Image(label="\ud83d\udcd8 Extracted Document Image", show_label=True, elem_id="docimg", height=480, width=340)
65
- chatbot = gr.Chatbot(label="\ud83e\udde0 Document Assistant", elem_id="chatbox", bubble_full_width=False, height=480)
66
  prompt = gr.Textbox(placeholder="Ask about the document...", label="Ask about the document")
67
  send_btn = gr.Button("Send")
68
 
69
  # Voice Section
70
- audio_in = gr.Audio(label="\ud83c\udfb5 Audio", type="numpy", streaming=True)
71
  live_transcript = gr.Textbox(label="Live Transcript", lines=6)
72
  clear_btn = gr.Button("Clear Transcript")
73
 
74
  with gr.Blocks(theme=gr.themes.Base(), css="""
75
  #docimg img { object-fit: contain !important; }
76
- #chatbox { border-radius: 10px; }
77
  .gr-box { border-radius: 12px; }
78
  """) as demo:
79
 
80
- gr.Markdown("# \ud83e\uddd0 Document AI + \ud83c\udfa7 Voice Assistant")
81
  with gr.Row():
82
  with gr.Column(scale=1):
83
  doc_image.render()
@@ -90,7 +91,7 @@ with gr.Blocks(theme=gr.themes.Base(), css="""
90
 
91
  send_btn.click(fn=process_user_input, inputs=[prompt, chatbot], outputs=[prompt, chatbot])
92
 
93
- with gr.Accordion("\ud83c\udf99\ufe0f Or Use Voice Instead", open=False):
94
  live_transcript.render()
95
  with gr.Row():
96
  audio_in.render()
@@ -98,4 +99,5 @@ with gr.Blocks(theme=gr.themes.Base(), css="""
98
  audio_in.stream(fn=send_audio_chunk_realtime, inputs=audio_in, outputs=live_transcript)
99
  clear_btn.click(fn=clear_transcript, outputs=live_transcript)
100
 
101
- demo.launch()
 
 
1
  import gradio as gr
2
  import os
3
  import uuid
 
4
  from openai import OpenAI
5
  from realtime_transcriber import WebSocketClient, connections, WEBSOCKET_URI, WEBSOCKET_HEADERS
6
 
 
14
  session_id = str(uuid.uuid4())
15
  if session_id not in connections:
16
  connections[session_id] = WebSocketClient(WEBSOCKET_URI, WEBSOCKET_HEADERS, session_id)
17
+ import threading
18
  threading.Thread(target=connections[session_id].run, daemon=True).start()
19
 
20
  # Functions for Document Assistant
21
  def process_user_input(message, history):
22
  if not message:
23
  return "Please enter a message.", history
24
+
25
  try:
26
  thread = client.beta.threads.create()
27
  client.beta.threads.messages.create(
 
45
  history.append((message, assistant_reply))
46
  return "", history
47
  except Exception as e:
48
+ return f"❌ Error: {str(e)}", history
49
 
50
  # Functions for Realtime Voice Transcription
51
  def send_audio_chunk_realtime(mic_chunk):
 
62
  return ""
63
 
64
  # Gradio UI Components
65
+ doc_image = gr.Image(label="πŸ“˜ Extracted Document Image", show_label=True, elem_id="docimg", height=500, width=360)
66
+ chatbot = gr.Chatbot(label="🧠 Document Assistant", elem_id="chatbox", bubble_full_width=False)
67
  prompt = gr.Textbox(placeholder="Ask about the document...", label="Ask about the document")
68
  send_btn = gr.Button("Send")
69
 
70
  # Voice Section
71
+ audio_in = gr.Audio(label="🎡 Audio", type="numpy", streaming=True)
72
  live_transcript = gr.Textbox(label="Live Transcript", lines=6)
73
  clear_btn = gr.Button("Clear Transcript")
74
 
75
  with gr.Blocks(theme=gr.themes.Base(), css="""
76
  #docimg img { object-fit: contain !important; }
77
+ #chatbox { height: 500px; }
78
  .gr-box { border-radius: 12px; }
79
  """) as demo:
80
 
81
+ gr.Markdown("# 🧠 Document AI + πŸŽ™οΈ Voice Assistant")
82
  with gr.Row():
83
  with gr.Column(scale=1):
84
  doc_image.render()
 
91
 
92
  send_btn.click(fn=process_user_input, inputs=[prompt, chatbot], outputs=[prompt, chatbot])
93
 
94
+ with gr.Accordion("πŸŽ™οΈ Or Use Voice Instead", open=False):
95
  live_transcript.render()
96
  with gr.Row():
97
  audio_in.render()
 
99
  audio_in.stream(fn=send_audio_chunk_realtime, inputs=audio_in, outputs=live_transcript)
100
  clear_btn.click(fn=clear_transcript, outputs=live_transcript)
101
 
102
+ # LAUNCH WITH SHARE ENABLED FOR PUBLIC URL
103
+ demo.launch(share=True)