Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,7 @@ custom_css = """
|
|
| 25 |
.gradio-container {
|
| 26 |
max-width: 1200px !important;
|
| 27 |
margin: 0 auto !important;
|
|
|
|
| 28 |
}
|
| 29 |
.container {
|
| 30 |
max-width: 1200px;
|
|
@@ -33,60 +34,100 @@ custom_css = """
|
|
| 33 |
}
|
| 34 |
.chat-container {
|
| 35 |
border: 1px solid #e0e0e0;
|
| 36 |
-
border-radius:
|
| 37 |
-
padding:
|
| 38 |
background: white;
|
| 39 |
-
box-shadow: 0
|
|
|
|
| 40 |
}
|
| 41 |
.file-upload {
|
| 42 |
border: 2px dashed #e0e0e0;
|
| 43 |
-
border-radius:
|
| 44 |
-
padding:
|
| 45 |
text-align: center;
|
| 46 |
background: #f8f9fa;
|
| 47 |
transition: all 0.3s ease;
|
|
|
|
| 48 |
}
|
| 49 |
.file-upload:hover {
|
| 50 |
border-color: #4CAF50;
|
| 51 |
background: #f0f7f0;
|
|
|
|
| 52 |
}
|
| 53 |
.audio-recorder {
|
| 54 |
background: #f8f9fa;
|
| 55 |
-
border-radius:
|
| 56 |
-
padding:
|
| 57 |
text-align: center;
|
|
|
|
| 58 |
}
|
| 59 |
.record-button {
|
| 60 |
-
background: #4CAF50;
|
| 61 |
color: white;
|
| 62 |
border: none;
|
| 63 |
-
padding:
|
| 64 |
-
border-radius:
|
| 65 |
cursor: pointer;
|
| 66 |
transition: all 0.3s ease;
|
| 67 |
font-size: 16px;
|
|
|
|
| 68 |
display: flex;
|
| 69 |
align-items: center;
|
| 70 |
-
gap:
|
| 71 |
margin: 0 auto;
|
|
|
|
| 72 |
}
|
| 73 |
.record-button:hover {
|
| 74 |
-
background: #45a049;
|
| 75 |
-
transform:
|
|
|
|
| 76 |
}
|
| 77 |
.record-button.recording {
|
| 78 |
-
background: #f44336;
|
| 79 |
animation: pulse 1.5s infinite;
|
| 80 |
}
|
| 81 |
.status-text {
|
| 82 |
color: #666;
|
| 83 |
font-size: 14px;
|
| 84 |
-
margin-top:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
@keyframes pulse {
|
| 87 |
-
0% { transform: scale(1); }
|
| 88 |
-
50% { transform: scale(1.05); }
|
| 89 |
-
100% { transform: scale(1); }
|
| 90 |
}
|
| 91 |
"""
|
| 92 |
|
|
@@ -165,7 +206,7 @@ class AdvancedRAG:
|
|
| 165 |
self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
|
| 166 |
|
| 167 |
def create_thread(self) -> str:
|
| 168 |
-
thread = openai.
|
| 169 |
tool_resources={
|
| 170 |
"file_search": {
|
| 171 |
"vector_store_ids": [self.vector_store_id]
|
|
@@ -177,7 +218,7 @@ class AdvancedRAG:
|
|
| 177 |
|
| 178 |
def create_vector_store(self, name: str = "My Vector Store") -> str:
|
| 179 |
try:
|
| 180 |
-
vector_store = openai.
|
| 181 |
self.vector_store_id = vector_store.id
|
| 182 |
return self.vector_store_id
|
| 183 |
except Exception as e:
|
|
@@ -186,11 +227,11 @@ class AdvancedRAG:
|
|
| 186 |
def upload_document(self, file) -> str:
|
| 187 |
if self.vector_store_id:
|
| 188 |
try:
|
| 189 |
-
openai.
|
| 190 |
except Exception as e:
|
| 191 |
print(f"Could not delete previous vector store {self.vector_store_id}: {e}")
|
| 192 |
|
| 193 |
-
vector_store = openai.
|
| 194 |
self.vector_store_id = vector_store.id
|
| 195 |
self.file_ids = []
|
| 196 |
|
|
@@ -198,17 +239,17 @@ class AdvancedRAG:
|
|
| 198 |
tmp.write(file.read())
|
| 199 |
tmp.flush()
|
| 200 |
with open(tmp.name, "rb") as file_obj:
|
| 201 |
-
file_obj = openai.
|
| 202 |
file=file_obj,
|
| 203 |
purpose="assistants"
|
| 204 |
)
|
| 205 |
self.file_ids.append(file_obj.id)
|
| 206 |
-
openai.
|
| 207 |
vector_store_id=self.vector_store_id,
|
| 208 |
file_id=file_obj.id
|
| 209 |
)
|
| 210 |
|
| 211 |
-
thread = openai.
|
| 212 |
tool_resources={
|
| 213 |
"file_search": {
|
| 214 |
"vector_store_ids": [self.vector_store_id]
|
|
@@ -220,20 +261,20 @@ class AdvancedRAG:
|
|
| 220 |
|
| 221 |
def ask_question(self, question: str) -> str:
|
| 222 |
try:
|
| 223 |
-
openai.
|
| 224 |
thread_id=self.thread_id,
|
| 225 |
role="user",
|
| 226 |
content=question
|
| 227 |
)
|
| 228 |
|
| 229 |
-
run = openai.
|
| 230 |
thread_id=self.thread_id,
|
| 231 |
assistant_id=self.assistant_id
|
| 232 |
)
|
| 233 |
|
| 234 |
waited = 0
|
| 235 |
while True:
|
| 236 |
-
run_status = openai.
|
| 237 |
thread_id=self.thread_id,
|
| 238 |
run_id=run.id
|
| 239 |
)
|
|
@@ -246,7 +287,7 @@ class AdvancedRAG:
|
|
| 246 |
if waited > 60:
|
| 247 |
raise Exception("Run timed out after 60 seconds.")
|
| 248 |
|
| 249 |
-
messages = openai.
|
| 250 |
thread_id=self.thread_id,
|
| 251 |
order='desc',
|
| 252 |
limit=1
|
|
@@ -264,7 +305,7 @@ class AdvancedRAG:
|
|
| 264 |
tmp.flush()
|
| 265 |
tmp_path = tmp.name
|
| 266 |
with open(tmp_path, "rb") as audio:
|
| 267 |
-
transcript = openai.
|
| 268 |
model="whisper-1",
|
| 269 |
file=audio,
|
| 270 |
language="en"
|
|
@@ -324,7 +365,7 @@ def process_audio_base64(audio_base64, history):
|
|
| 324 |
|
| 325 |
# Transcribe audio
|
| 326 |
with open(tmp_path, "rb") as audio_file:
|
| 327 |
-
transcript = openai.
|
| 328 |
model="whisper-1",
|
| 329 |
file=audio_file,
|
| 330 |
language="en"
|
|
@@ -341,25 +382,55 @@ def process_audio_base64(audio_base64, history):
|
|
| 341 |
except Exception as e:
|
| 342 |
return "", history + [("Audio input", f"Error: {str(e)}")]
|
| 343 |
|
| 344 |
-
# Create Gradio interface
|
| 345 |
-
with gr.Blocks(title="Document Q&A System") as demo:
|
| 346 |
-
gr.Markdown("
|
| 347 |
-
|
|
|
|
|
|
|
| 348 |
|
| 349 |
with gr.Row():
|
| 350 |
-
with gr.Column():
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
with gr.Row():
|
| 356 |
-
with gr.Column():
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
|
| 361 |
with gr.Row():
|
| 362 |
-
with gr.Column():
|
| 363 |
audio_recorder = create_audio_recorder()
|
| 364 |
audio_input = gr.Audio(label="Voice Input", type="filepath", visible=False)
|
| 365 |
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot])
|
|
@@ -374,7 +445,6 @@ with gr.Blocks(title="Document Q&A System") as demo:
|
|
| 374 |
window.addEventListener('message', function(event) {
|
| 375 |
if (event.data.type === 'audio_data') {
|
| 376 |
const audioData = event.data.data;
|
| 377 |
-
// Convert base64 to blob and create file
|
| 378 |
const byteString = atob(audioData.split(',')[1]);
|
| 379 |
const mimeString = audioData.split(',')[0].split(':')[1].split(';')[0];
|
| 380 |
const ab = new ArrayBuffer(byteString.length);
|
|
@@ -385,7 +455,6 @@ with gr.Blocks(title="Document Q&A System") as demo:
|
|
| 385 |
const blob = new Blob([ab], {type: mimeString});
|
| 386 |
const file = new File([blob], "recording.wav", {type: mimeString});
|
| 387 |
|
| 388 |
-
// Update audio input
|
| 389 |
const audioInput = document.querySelector('input[type="file"]');
|
| 390 |
const dataTransfer = new DataTransfer();
|
| 391 |
dataTransfer.items.add(file);
|
|
|
|
| 25 |
.gradio-container {
|
| 26 |
max-width: 1200px !important;
|
| 27 |
margin: 0 auto !important;
|
| 28 |
+
font-family: 'Inter', sans-serif;
|
| 29 |
}
|
| 30 |
.container {
|
| 31 |
max-width: 1200px;
|
|
|
|
| 34 |
}
|
| 35 |
.chat-container {
|
| 36 |
border: 1px solid #e0e0e0;
|
| 37 |
+
border-radius: 16px;
|
| 38 |
+
padding: 24px;
|
| 39 |
background: white;
|
| 40 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 41 |
+
margin-bottom: 20px;
|
| 42 |
}
|
| 43 |
.file-upload {
|
| 44 |
border: 2px dashed #e0e0e0;
|
| 45 |
+
border-radius: 16px;
|
| 46 |
+
padding: 24px;
|
| 47 |
text-align: center;
|
| 48 |
background: #f8f9fa;
|
| 49 |
transition: all 0.3s ease;
|
| 50 |
+
margin-bottom: 20px;
|
| 51 |
}
|
| 52 |
.file-upload:hover {
|
| 53 |
border-color: #4CAF50;
|
| 54 |
background: #f0f7f0;
|
| 55 |
+
transform: translateY(-2px);
|
| 56 |
}
|
| 57 |
.audio-recorder {
|
| 58 |
background: #f8f9fa;
|
| 59 |
+
border-radius: 16px;
|
| 60 |
+
padding: 24px;
|
| 61 |
text-align: center;
|
| 62 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 63 |
}
|
| 64 |
.record-button {
|
| 65 |
+
background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
|
| 66 |
color: white;
|
| 67 |
border: none;
|
| 68 |
+
padding: 14px 28px;
|
| 69 |
+
border-radius: 30px;
|
| 70 |
cursor: pointer;
|
| 71 |
transition: all 0.3s ease;
|
| 72 |
font-size: 16px;
|
| 73 |
+
font-weight: 600;
|
| 74 |
display: flex;
|
| 75 |
align-items: center;
|
| 76 |
+
gap: 10px;
|
| 77 |
margin: 0 auto;
|
| 78 |
+
box-shadow: 0 4px 6px rgba(76, 175, 80, 0.2);
|
| 79 |
}
|
| 80 |
.record-button:hover {
|
| 81 |
+
background: linear-gradient(135deg, #45a049 0%, #3d8b40 100%);
|
| 82 |
+
transform: translateY(-2px);
|
| 83 |
+
box-shadow: 0 6px 8px rgba(76, 175, 80, 0.3);
|
| 84 |
}
|
| 85 |
.record-button.recording {
|
| 86 |
+
background: linear-gradient(135deg, #f44336 0%, #e53935 100%);
|
| 87 |
animation: pulse 1.5s infinite;
|
| 88 |
}
|
| 89 |
.status-text {
|
| 90 |
color: #666;
|
| 91 |
font-size: 14px;
|
| 92 |
+
margin-top: 12px;
|
| 93 |
+
font-weight: 500;
|
| 94 |
+
}
|
| 95 |
+
.send-button {
|
| 96 |
+
background: linear-gradient(135deg, #2196F3 0%, #1976D2 100%);
|
| 97 |
+
color: white;
|
| 98 |
+
border: none;
|
| 99 |
+
padding: 12px 24px;
|
| 100 |
+
border-radius: 25px;
|
| 101 |
+
cursor: pointer;
|
| 102 |
+
transition: all 0.3s ease;
|
| 103 |
+
font-weight: 600;
|
| 104 |
+
box-shadow: 0 4px 6px rgba(33, 150, 243, 0.2);
|
| 105 |
+
}
|
| 106 |
+
.send-button:hover {
|
| 107 |
+
background: linear-gradient(135deg, #1976D2 0%, #1565C0 100%);
|
| 108 |
+
transform: translateY(-2px);
|
| 109 |
+
box-shadow: 0 6px 8px rgba(33, 150, 243, 0.3);
|
| 110 |
+
}
|
| 111 |
+
.textbox {
|
| 112 |
+
border-radius: 12px !important;
|
| 113 |
+
border: 2px solid #e0e0e0 !important;
|
| 114 |
+
padding: 12px !important;
|
| 115 |
+
font-size: 16px !important;
|
| 116 |
+
transition: all 0.3s ease !important;
|
| 117 |
+
}
|
| 118 |
+
.textbox:focus {
|
| 119 |
+
border-color: #2196F3 !important;
|
| 120 |
+
box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.2) !important;
|
| 121 |
+
}
|
| 122 |
+
.chatbot {
|
| 123 |
+
border-radius: 12px !important;
|
| 124 |
+
border: 2px solid #e0e0e0 !important;
|
| 125 |
+
padding: 16px !important;
|
| 126 |
}
|
| 127 |
@keyframes pulse {
|
| 128 |
+
0% { transform: scale(1); box-shadow: 0 4px 6px rgba(244, 67, 54, 0.2); }
|
| 129 |
+
50% { transform: scale(1.05); box-shadow: 0 6px 8px rgba(244, 67, 54, 0.3); }
|
| 130 |
+
100% { transform: scale(1); box-shadow: 0 4px 6px rgba(244, 67, 54, 0.2); }
|
| 131 |
}
|
| 132 |
"""
|
| 133 |
|
|
|
|
| 206 |
self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
|
| 207 |
|
| 208 |
def create_thread(self) -> str:
|
| 209 |
+
thread = openai.beta.threads.create(
|
| 210 |
tool_resources={
|
| 211 |
"file_search": {
|
| 212 |
"vector_store_ids": [self.vector_store_id]
|
|
|
|
| 218 |
|
| 219 |
def create_vector_store(self, name: str = "My Vector Store") -> str:
|
| 220 |
try:
|
| 221 |
+
vector_store = openai.beta.vector_stores.create(name=name)
|
| 222 |
self.vector_store_id = vector_store.id
|
| 223 |
return self.vector_store_id
|
| 224 |
except Exception as e:
|
|
|
|
| 227 |
def upload_document(self, file) -> str:
|
| 228 |
if self.vector_store_id:
|
| 229 |
try:
|
| 230 |
+
openai.beta.vector_stores.delete(self.vector_store_id)
|
| 231 |
except Exception as e:
|
| 232 |
print(f"Could not delete previous vector store {self.vector_store_id}: {e}")
|
| 233 |
|
| 234 |
+
vector_store = openai.beta.vector_stores.create(name="knowledge_base")
|
| 235 |
self.vector_store_id = vector_store.id
|
| 236 |
self.file_ids = []
|
| 237 |
|
|
|
|
| 239 |
tmp.write(file.read())
|
| 240 |
tmp.flush()
|
| 241 |
with open(tmp.name, "rb") as file_obj:
|
| 242 |
+
file_obj = openai.files.create(
|
| 243 |
file=file_obj,
|
| 244 |
purpose="assistants"
|
| 245 |
)
|
| 246 |
self.file_ids.append(file_obj.id)
|
| 247 |
+
openai.beta.vector_stores.files.create(
|
| 248 |
vector_store_id=self.vector_store_id,
|
| 249 |
file_id=file_obj.id
|
| 250 |
)
|
| 251 |
|
| 252 |
+
thread = openai.beta.threads.create(
|
| 253 |
tool_resources={
|
| 254 |
"file_search": {
|
| 255 |
"vector_store_ids": [self.vector_store_id]
|
|
|
|
| 261 |
|
| 262 |
def ask_question(self, question: str) -> str:
|
| 263 |
try:
|
| 264 |
+
openai.beta.threads.messages.create(
|
| 265 |
thread_id=self.thread_id,
|
| 266 |
role="user",
|
| 267 |
content=question
|
| 268 |
)
|
| 269 |
|
| 270 |
+
run = openai.beta.threads.runs.create(
|
| 271 |
thread_id=self.thread_id,
|
| 272 |
assistant_id=self.assistant_id
|
| 273 |
)
|
| 274 |
|
| 275 |
waited = 0
|
| 276 |
while True:
|
| 277 |
+
run_status = openai.beta.threads.runs.retrieve(
|
| 278 |
thread_id=self.thread_id,
|
| 279 |
run_id=run.id
|
| 280 |
)
|
|
|
|
| 287 |
if waited > 60:
|
| 288 |
raise Exception("Run timed out after 60 seconds.")
|
| 289 |
|
| 290 |
+
messages = openai.beta.threads.messages.list(
|
| 291 |
thread_id=self.thread_id,
|
| 292 |
order='desc',
|
| 293 |
limit=1
|
|
|
|
| 305 |
tmp.flush()
|
| 306 |
tmp_path = tmp.name
|
| 307 |
with open(tmp_path, "rb") as audio:
|
| 308 |
+
transcript = openai.audio.transcriptions.create(
|
| 309 |
model="whisper-1",
|
| 310 |
file=audio,
|
| 311 |
language="en"
|
|
|
|
| 365 |
|
| 366 |
# Transcribe audio
|
| 367 |
with open(tmp_path, "rb") as audio_file:
|
| 368 |
+
transcript = openai.audio.transcriptions.create(
|
| 369 |
model="whisper-1",
|
| 370 |
file=audio_file,
|
| 371 |
language="en"
|
|
|
|
| 382 |
except Exception as e:
|
| 383 |
return "", history + [("Audio input", f"Error: {str(e)}")]
|
| 384 |
|
| 385 |
+
# Create Gradio interface with improved layout
|
| 386 |
+
with gr.Blocks(title="Document Q&A System", css=custom_css) as demo:
|
| 387 |
+
gr.Markdown("""
|
| 388 |
+
# 📚 Document Q&A System
|
| 389 |
+
Upload a document and ask questions about it. You can also use voice input!
|
| 390 |
+
""")
|
| 391 |
|
| 392 |
with gr.Row():
|
| 393 |
+
with gr.Column(scale=1):
|
| 394 |
+
with gr.Box(elem_classes="file-upload"):
|
| 395 |
+
file_input = gr.File(
|
| 396 |
+
label="Upload Document",
|
| 397 |
+
file_types=[".pdf", ".txt", ".doc", ".docx"],
|
| 398 |
+
file_count="single"
|
| 399 |
+
)
|
| 400 |
+
file_output = gr.Textbox(
|
| 401 |
+
label="Upload Status",
|
| 402 |
+
interactive=False,
|
| 403 |
+
elem_classes="textbox"
|
| 404 |
+
)
|
| 405 |
+
file_input.change(process_file, file_input, file_output)
|
| 406 |
|
| 407 |
with gr.Row():
|
| 408 |
+
with gr.Column(scale=2):
|
| 409 |
+
with gr.Box(elem_classes="chat-container"):
|
| 410 |
+
chatbot = gr.Chatbot(
|
| 411 |
+
height=500,
|
| 412 |
+
type="messages",
|
| 413 |
+
show_label=False,
|
| 414 |
+
elem_classes="chatbot"
|
| 415 |
+
)
|
| 416 |
+
with gr.Row():
|
| 417 |
+
question = gr.Textbox(
|
| 418 |
+
label="Ask a question",
|
| 419 |
+
placeholder="Type your question here...",
|
| 420 |
+
show_label=False,
|
| 421 |
+
container=False,
|
| 422 |
+
elem_classes="textbox"
|
| 423 |
+
)
|
| 424 |
+
submit_btn = gr.Button(
|
| 425 |
+
"Send",
|
| 426 |
+
variant="primary",
|
| 427 |
+
elem_classes="send-button"
|
| 428 |
+
)
|
| 429 |
+
question.submit(process_question, [question, chatbot], [question, chatbot])
|
| 430 |
+
submit_btn.click(process_question, [question, chatbot], [question, chatbot])
|
| 431 |
|
| 432 |
with gr.Row():
|
| 433 |
+
with gr.Column(scale=1):
|
| 434 |
audio_recorder = create_audio_recorder()
|
| 435 |
audio_input = gr.Audio(label="Voice Input", type="filepath", visible=False)
|
| 436 |
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot])
|
|
|
|
| 445 |
window.addEventListener('message', function(event) {
|
| 446 |
if (event.data.type === 'audio_data') {
|
| 447 |
const audioData = event.data.data;
|
|
|
|
| 448 |
const byteString = atob(audioData.split(',')[1]);
|
| 449 |
const mimeString = audioData.split(',')[0].split(':')[1].split(';')[0];
|
| 450 |
const ab = new ArrayBuffer(byteString.length);
|
|
|
|
| 455 |
const blob = new Blob([ab], {type: mimeString});
|
| 456 |
const file = new File([blob], "recording.wav", {type: mimeString});
|
| 457 |
|
|
|
|
| 458 |
const audioInput = document.querySelector('input[type="file"]');
|
| 459 |
const dataTransfer = new DataTransfer();
|
| 460 |
dataTransfer.items.add(file);
|