Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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"
|
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="
|
65 |
-
chatbot = gr.Chatbot(label="
|
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="
|
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 {
|
77 |
.gr-box { border-radius: 12px; }
|
78 |
""") as demo:
|
79 |
|
80 |
-
gr.Markdown("#
|
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("
|
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 |
-
|
|
|
|
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)
|