File size: 26,855 Bytes
0fa951f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
080f5e3
 
 
0fa951f
 
 
 
 
 
080f5e3
0fa951f
 
 
 
 
080f5e3
0fa951f
 
 
 
080f5e3
0fa951f
080f5e3
0fa951f
080f5e3
 
 
0fa951f
 
 
 
 
 
080f5e3
0fa951f
 
080f5e3
0fa951f
 
080f5e3
0fa951f
080f5e3
0fa951f
 
 
 
 
 
 
 
080f5e3
 
 
 
0fa951f
 
080f5e3
0fa951f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
080f5e3
0fa951f
 
 
 
 
 
 
 
080f5e3
 
af0b644
 
0d7d701
0df46b2
080f5e3
bf5b316
 
0d7d701
 
af0b644
080f5e3
af0b644
 
080f5e3
af0b644
080f5e3
af0b644
 
080f5e3
0fa951f
 
 
 
 
 
 
080f5e3
 
0fa951f
 
080f5e3
 
0fa951f
080f5e3
 
0fa951f
080f5e3
0fa951f
080f5e3
 
 
 
 
0fa951f
080f5e3
0fa951f
 
 
 
080f5e3
0fa951f
 
080f5e3
0fa951f
 
0d7d701
080f5e3
0fa951f
 
080f5e3
 
 
0df46b2
080f5e3
0df46b2
080f5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af0b644
0fa951f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
080f5e3
 
 
0fa951f
0df46b2
 
080f5e3
0fa951f
 
 
080f5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af0b644
080f5e3
af0b644
 
080f5e3
 
 
 
 
 
af0b644
080f5e3
af0b644
080f5e3
 
 
af0b644
080f5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af0b644
080f5e3
 
 
af0b644
080f5e3
 
 
 
 
 
bf5b316
080f5e3
 
 
 
 
af0b644
080f5e3
 
 
 
 
bf5b316
 
 
 
 
 
080f5e3
af0b644
080f5e3
 
0df46b2
 
080f5e3
 
 
 
bf5b316
080f5e3
 
 
af0b644
bf5b316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af0b644
080f5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf5b316
 
 
 
 
 
 
 
 
 
 
 
080f5e3
bf5b316
 
 
 
080f5e3
bf5b316
 
080f5e3
 
 
 
 
 
0df46b2
 
bf5b316
080f5e3
 
 
 
 
af0b644
080f5e3
0fa951f
080f5e3
 
 
 
 
0fa951f
080f5e3
 
 
 
 
 
 
 
 
0fa951f
0d7d701
080f5e3
0d7d701
0fa951f
0d7d701
080f5e3
 
 
 
0d7d701
 
 
080f5e3
 
 
0d7d701
080f5e3
0d7d701
 
 
 
0fa951f
af0b644
0fa951f
080f5e3
0d7d701
 
 
 
 
 
 
 
 
 
 
 
 
af0b644
080f5e3
0d7d701
 
af0b644
080f5e3
0fa951f
 
0d7d701
 
 
 
080f5e3
 
 
 
 
 
af0b644
080f5e3
 
 
 
 
af0b644
 
080f5e3
af0b644
 
080f5e3
 
0d7d701
 
080f5e3
af0b644
080f5e3
af0b644
080f5e3
 
 
 
 
 
0fa951f
bf5b316
0df46b2
 
 
 
 
 
 
 
 
bf5b316
080f5e3
 
bf5b316
080f5e3
0fa951f
0df46b2
 
 
 
 
 
 
 
080f5e3
bf5b316
080f5e3
 
 
 
 
 
 
0df46b2
080f5e3
0fa951f
080f5e3
 
 
bf5b316
080f5e3
 
0df46b2
 
 
bf5b316
080f5e3
0fa951f
0df46b2
080f5e3
0fa951f
080f5e3
 
bf5b316
080f5e3
 
 
 
 
0df46b2
080f5e3
 
 
bf5b316
080f5e3
 
 
0df46b2
080f5e3
 
 
0fa951f
080f5e3
0fa951f
 
 
 
 
0df46b2
 
 
0fa951f
080f5e3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import base64
import cv2
import glob
import json
import math
import os
import pytz
import random
import re
import requests
import streamlit as st
import streamlit.components.v1 as components
import textract
import time
import zipfile
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
import concurrent

from audio_recorder_streamlit import audio_recorder
from bs4 import BeautifulSoup
from collections import deque
from datetime import datetime
from dotenv import load_dotenv
from gradio_client import Client
from io import BytesIO
from moviepy import VideoFileClip
from PIL import Image
from PyPDF2 import PdfReader
from templates import bot_template, css, user_template
from urllib.parse import quote
from xml.etree import ElementTree as ET

import openai
from openai import OpenAI
import pandas as pd

# Configuration
Site_Name = 'Scholarly-Article-Document-Search-With-Memory'
title = "πŸ”¬πŸ§ ScienceBrain.AI"
helpURL = 'https://huggingface.co/awacke1'
bugURL = 'https://huggingface.co/spaces/awacke1'
icons = Image.open("icons.ico")
st.set_page_config(
    page_title=title,
    page_icon=icons,
    layout="wide",
    initial_sidebar_state="auto",
    menu_items={'Get Help': helpURL, 'Report a bug': bugURL, 'About': title}
)

# API Configuration
API_KEY = os.getenv('API_KEY')
HF_KEY = os.getenv('HF_KEY')
headers = {"Authorization": f"Bearer {HF_KEY}", "Content-Type": "application/json"}
key = os.getenv('OPENAI_API_KEY')
client = OpenAI(api_key=key, organization=os.getenv('OPENAI_ORG_ID'))
MODEL = "gpt-4o-2024-05-13"
if "openai_model" not in st.session_state:
    st.session_state["openai_model"] = MODEL
if "messages" not in st.session_state:
    st.session_state.messages = []
if st.button("Clear Session"):
    st.session_state.messages = []

# Sidebar Options
should_save = st.sidebar.checkbox("πŸ’Ύ Save", value=True, help="Save your session data.")

# HTML5 Speech Synthesis
@st.cache_resource
def SpeechSynthesis(result):
    documentHTML5 = '''
    <!DOCTYPE html>
    <html>
    <head>
        <title>Read It Aloud</title>
        <script type="text/javascript">
            function readAloud() {
                const text = document.getElementById("textArea").value;
                const speech = new SpeechSynthesisUtterance(text);
                window.speechSynthesis.speak(speech);
            }
        </script>
    </head>
    <body>
        <h1>πŸ”Š Read It Aloud</h1>
        <textarea id="textArea" rows="10" cols="80">
    '''
    documentHTML5 += result + '''
        </textarea>
        <br>
        <button onclick="readAloud()">πŸ”Š Read Aloud</button>
    </body>
    </html>
    '''
    components.html(documentHTML5, width=1280, height=300)

# File Naming and Saving
def generate_filename(prompt, file_type, original_name=None):
    central = pytz.timezone('US/Central')
    safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
    safe_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt).strip()[:50]
    if original_name and file_type == "md":  # For images
        base_name = os.path.splitext(original_name)[0]
        file_stem = f"{safe_date_time}_{safe_prompt}_{base_name}"[:100]  # Cap at 100 chars
        return f"{file_stem}.{file_type}"
    file_stem = f"{safe_date_time}_{safe_prompt}"[:100]  # Cap at 100 chars
    return f"{file_stem}.{file_type}"

def create_and_save_file(content, file_type="md", prompt=None, original_name=None, should_save=True):
    if not should_save:
        return None
    filename = generate_filename(prompt, file_type, original_name)
    with open(filename, "w", encoding="utf-8") as f:
        f.write(content if not prompt else prompt + "\n\n" + content)
    return filename

# Text Processing
def process_text(text_input):
    if text_input:
        st.session_state.messages.append({"role": "user", "content": text_input})
        with st.chat_message("user"):
            st.markdown(text_input)
        with st.chat_message("assistant"):
            completion = client.chat.completions.create(
                model=st.session_state["openai_model"],
                messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
                stream=False
            )
            response = completion.choices[0].message.content
            st.markdown(response)
            filename = generate_filename(text_input, "md")
            create_and_save_file(response, "md", text_input, should_save=should_save)
            st.session_state.messages.append({"role": "assistant", "content": response})

# Image Processing
def process_image(image_input, user_prompt):
    original_name = image_input.name
    image_bytes = image_input.read()
    with open(original_name, "wb") as f:
        f.write(image_bytes)  # Save original image
    base64_image = base64.b64encode(image_bytes).decode("utf-8")
    response = client.chat.completions.create(
        model=st.session_state["openai_model"],
        messages=[
            {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
            {"role": "user", "content": [
                {"type": "text", "text": user_prompt},
                {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
            ]}
        ],
        temperature=0.0
    )
    image_response = response.choices[0].message.content
    filename = generate_filename(user_prompt, "md", original_name)  # Include prompt in filename
    create_and_save_file(image_response, "md", user_prompt, original_name, should_save=should_save)
    return image_response

# Audio Processing
def process_audio(audio_input, text_input=''):
    if audio_input:
        audio_bytes = audio_input if isinstance(audio_input, bytes) else audio_input.read()
        supported_formats = ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']
        file_ext = "wav" if isinstance(audio_input, bytes) else os.path.splitext(audio_input.name)[1][1:].lower()
        if file_ext not in supported_formats:
            st.error(f"Unsupported format: {file_ext}. Supported formats: {supported_formats}")
            return
        if len(audio_bytes) > 200 * 1024 * 1024:  # 200MB limit
            st.error("File exceeds 200MB limit.")
            return
        with st.spinner("Transcribing audio..."):
            try:
                transcription = client.audio.transcriptions.create(
                    model="whisper-1",
                    file=BytesIO(audio_bytes)
                ).text
                st.session_state.messages.append({"role": "user", "content": transcription})
                with st.chat_message("user"):
                    st.markdown(transcription)
                with st.chat_message("assistant"):
                    completion = client.chat.completions.create(
                        model=st.session_state["openai_model"],
                        messages=[{"role": "user", "content": text_input + "\n\nTranscription: " + transcription}]
                    )
                    response = completion.choices[0].message.content
                    st.markdown(response)
                    filename = generate_filename(transcription, "md")
                    create_and_save_file(response, "md", text_input, should_save=should_save)
                    st.session_state.messages.append({"role": "assistant", "content": response})
            except openai.BadRequestError as e:
                st.error(f"Audio processing error: {str(e)}")

# Video Processing
def save_video(video_input):
    with open(video_input.name, "wb") as f:
        f.write(video_input.read())
    return video_input.name

def process_video(video_path, seconds_per_frame=2):
    base64Frames = []
    base_video_path, _ = os.path.splitext(video_path)
    video = cv2.VideoCapture(video_path)
    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = video.get(cv2.CAP_PROP_FPS)
    frames_to_skip = int(fps * seconds_per_frame)
    curr_frame = 0
    while curr_frame < total_frames - 1:
        video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)
        success, frame = video.read()
        if not success:
            break
        _, buffer = cv2.imencode(".jpg", frame)
        base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
        curr_frame += frames_to_skip
    video.release()
    audio_path = f"{base_video_path}.mp3"
    try:
        clip = VideoFileClip(video_path)
        if clip.audio:
            clip.audio.write_audiofile(audio_path, bitrate="32k")
            clip.audio.close()
        clip.close()
    except Exception as e:
        st.warning(f"No audio track found or error: {str(e)}")
        audio_path = None
    return base64Frames, audio_path

def process_audio_and_video(video_input):
    if video_input:
        video_path = save_video(video_input)
        with st.spinner("Extracting frames and audio..."):
            base64Frames, audio_path = process_video(video_path)
        if audio_path:
            with st.spinner("Transcribing video audio..."):
                try:
                    with open(audio_path, "rb") as audio_file:
                        transcript = client.audio.transcriptions.create(
                            model="whisper-1",
                            file=audio_file
                        ).text
                    with st.chat_message("user"):
                        st.markdown(f"Video Transcription: {transcript}")
                    with st.chat_message("assistant"):
                        response = client.chat.completions.create(
                            model=st.session_state["openai_model"],
                            messages=[
                                {"role": "system", "content": "Summarize the video and its transcript in Markdown."},
                                {"role": "user", "content": [
                                    "Video frames:", *map(lambda x: {"type": "image_url", "image_url": {"url": f"data:image/jpg;base64,{x}"}}, base64Frames),
                                    {"type": "text", "text": f"Transcription: {transcript}"}
                                ]}
                            ]
                        )
                        result = response.choices[0].message.content
                        st.markdown(result)
                        filename = generate_filename(transcript, "md")
                        create_and_save_file(result, "md", "Video summary", should_save=should_save)
                except openai.BadRequestError as e:
                    st.error(f"Video audio processing error: {str(e)}")
        else:
            st.warning("No audio to transcribe.")

# ArXiv Search
def search_arxiv(query):
    client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
    response = client.predict(
        message=query,
        llm_results_use=5,
        database_choice="Semantic Search",
        llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
        api_name="/update_with_rag_md"
    )
    result = response[0] + response[1]
    filename = generate_filename(query, "md")
    create_and_save_file(result, "md", query, should_save=should_save)
    st.session_state.messages.append({"role": "assistant", "content": result})
    return result

# RAG PDF Gallery
def upload_pdf_files_to_vector_store(vector_store_id, pdf_files):
    stats = {"total_files": len(pdf_files), "successful_uploads": 0, "failed_uploads": 0, "errors": []}
    def upload_single_pdf(file_path):
        file_name = os.path.basename(file_path)
        try:
            with open(file_path, "rb") as f:
                file_response = client.files.create(file=f, purpose="assistants")
            client.vector_stores.files.create(vector_store_id=vector_store_id, file_id=file_response.id)
            return {"file": file_name, "status": "success"}
        except Exception as e:
            return {"file": file_name, "status": "failed", "error": str(e)}
    with ThreadPoolExecutor(max_workers=5) as executor:
        futures = [executor.submit(upload_single_pdf, f) for f in pdf_files]
        for future in tqdm(concurrent.futures.as_completed(futures), total=len(pdf_files)):
            result = future.result()
            if result["status"] == "success":
                stats["successful_uploads"] += 1
            else:
                stats["failed_uploads"] += 1
                stats["errors"].append(result)
    return stats

def create_vector_store(store_name):
    vector_store = client.vector_stores.create(name=store_name)
    return {"id": vector_store.id, "name": vector_store.name, "created_at": vector_store.created_at, "file_count": vector_store.file_counts.completed}

def generate_questions(pdf_path):
    text = ""
    with open(pdf_path, "rb") as f:
        pdf = PdfReader(f)
        for page in pdf.pages:
            text += page.extract_text() or ""
    prompt = f"Generate a 10-question quiz with answers based only on this document. Format as markdown with numbered questions and answers:\n{text[:2000]}\n\n"
    response = client.chat.completions.create(
        model="gpt-4o-2024-05-13",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

def process_rag_query(query, vector_store_id):
    try:
        response = client.chat.completions.create(
            model="gpt-4o-2024-05-13",
            messages=[{"role": "user", "content": query}],
            tools=[{
                "type": "file_search",
                "file_search": {
                    "vector_store_ids": [vector_store_id]
                }
            }],
            tool_choice="auto"
        )
        tool_calls = response.choices[0].message.tool_calls if response.choices[0].message.tool_calls else []
        return response.choices[0].message.content, tool_calls
    except openai.BadRequestError as e:
        st.error(f"RAG query error: {str(e)}")
        return None, []

def evaluate_rag(vector_store_id, questions_dict):
    k = 5
    total_queries = len(questions_dict) * 10  # 10 questions per PDF
    correct_retrievals_at_k = 0
    reciprocal_ranks = []
    average_precisions = []
    
    for filename, quiz in questions_dict.items():
        questions = re.findall(r"\d+\.\s(.*?)\n\s*Answer:\s(.*?)\n", quiz, re.DOTALL)
        for question, _ in questions:
            expected_file = filename
            response, tool_calls = process_rag_query(question, vector_store_id)
            if not tool_calls:
                continue
            retrieved_files = [call.arguments.get("file_id", "") for call in tool_calls if "file_search" in call.type][:k]
            if expected_file in retrieved_files:
                rank = retrieved_files.index(expected_file) + 1
                correct_retrievals_at_k += 1
                reciprocal_ranks.append(1 / rank)
                precisions = [1 if f == expected_file else 0 for f in retrieved_files[:rank]]
                average_precisions.append(sum(precisions) / len(precisions))
            else:
                reciprocal_ranks.append(0)
                average_precisions.append(0)
    
    recall_at_k = correct_retrievals_at_k / total_queries if total_queries else 0
    mrr = sum(reciprocal_ranks) / total_queries if total_queries else 0
    map_score = sum(average_precisions) / total_queries if total_queries else 0
    return {"recall@k": recall_at_k, "mrr": mrr, "map": map_score, "k": k}

def rag_pdf_gallery():
    st.subheader("RAG PDF Gallery")
    pdf_files = st.file_uploader("Upload PDFs", type=["pdf"], accept_multiple_files=True)
    if pdf_files:
        pdf_paths = [save_video(f) for f in pdf_files]  # Reuse save_video for simplicity
        with st.spinner("Creating vector store..."):
            vector_store_details = create_vector_store("PDF_Gallery_Store")
            stats = upload_pdf_files_to_vector_store(vector_store_details["id"], pdf_paths)
            st.json(stats)
        
        col1, col2, col3 = st.columns(3)
        with col1:
            if st.button("πŸ“ Quiz"):
                st.session_state["rag_prompt"] = "Generate a 10-question quiz with answers based only on this document."
        with col2:
            if st.button("πŸ“‘ Summary"):
                st.session_state["rag_prompt"] = "Summarize this per page and output as markdown outline with emojis and numbered outline with multiple levels summarizing everything unique per page in method steps or fact steps."
        with col3:
            if st.button("πŸ” Key Facts"):
                st.session_state["rag_prompt"] = "Extract 10 key facts from this document in markdown with emojis."
        
        with st.spinner("Generating questions..."):
            questions_dict = {os.path.basename(p): generate_questions(p) for p in pdf_paths}
            st.markdown("### Generated Quiz")
            for filename, quiz in questions_dict.items():
                st.markdown(f"#### {filename}")
                st.markdown(quiz)
        
        query = st.text_input("Ask a question about the PDFs:", value=st.session_state.get("rag_prompt", ""))
        if query and st.button("Submit RAG Query"):
            with st.spinner("Processing RAG query..."):
                response, tool_calls = process_rag_query(query, vector_store_details["id"])
                if response:
                    st.markdown(response)
                    st.write("Retrieved chunks:")
                    for call in tool_calls:
                        if "file_search" in call.type:
                            st.json(call.arguments)
            st.rerun()
        
        if st.button("Evaluate RAG Performance"):
            with st.spinner("Evaluating..."):
                metrics = evaluate_rag(vector_store_details["id"], questions_dict)
                st.json(metrics)

# File Sidebar
def FileSidebar():
    st.sidebar.title("File Operations")
    default_types = [".md", ".png", ".pdf"]
    file_types = st.sidebar.multiselect("Filter by type", [".md", ".wav", ".png", ".mp4", ".mp3", ".pdf"], default=default_types)
    all_files = [f for f in glob.glob("*.*") if os.path.splitext(f)[1] in file_types and len(os.path.splitext(f)[0]) >= 10]
    all_files.sort(key=lambda x: os.path.getmtime(x), reverse=True)

    if st.sidebar.button("πŸ—‘ Delete All Filtered"):
        for file in all_files:
            os.remove(file)
        st.rerun()
    
    if st.sidebar.button("⬇️ Download All Filtered"):
        zip_file = create_zip_of_files(all_files)
        st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
    
    for file in all_files:
        ext = os.path.splitext(file)[1].lower()
        col1, col2, col3, col4, col5 = st.sidebar.columns([1, 6, 1, 1, 1])
        colFollowUp=""
        with col1:
            icon = "πŸ“œ" if ext == ".md" else "πŸ“„" if ext == ".pdf" else "πŸ–ΌοΈ" if ext in [".png", ".jpg", ".jpeg"] else "🎡" if ext in [".wav", ".mp3"] else "πŸŽ₯" if ext == ".mp4" else "πŸ“Ž"
            if st.button(icon, key=f"view_{file}"):
                with open(file, "rb") as f:
                    content = f.read()
                if ext == ".md":
                    colFollowUp=ext
                    #st.markdown(content.decode("utf-8"))
                    #SpeechSynthesis(content.decode("utf-8"))
                elif ext == ".pdf":
                    st.download_button("Download PDF", content, file, "application/pdf")
                    st.write("PDF Viewer not natively supported; download to view.")
                elif ext in [".png", ".jpg", ".jpeg"]:
                    st.image(content, use_column_width=True)
                elif ext in [".wav", ".mp3"]:
                    st.audio(content)
                elif ext == ".mp4":
                    st.video(content)
        with col2:
            st.markdown(get_table_download_link(file), unsafe_allow_html=True)
        with col3:
            if st.button("πŸ“‚", key=f"open_{file}"):
                with open(file, "rb") as f:
                    content = f.read()
                if ext == ".md":
                    st.text_area(f"Editing {file}", value=content.decode("utf-8"), height=300, key=f"edit_{file}")
                elif ext == ".pdf":
                    st.download_button("Download PDF to Edit", content, file, "application/pdf")
                    st.write("PDF editing not supported in-app; download to edit externally.")
                elif ext in [".png", ".jpg", ".jpeg"]:
                    st.image(content, use_column_width=True, caption=f"Viewing {file}")
                elif ext in [".wav", ".mp3"]:
                    st.audio(content, format=f"audio/{ext[1:]}")
                elif ext == ".mp4":
                    st.video(content, format="video/mp4")
        with col4:
            if st.button("▢️", key=f"run_{file}"):
                if ext == ".md":
                    process_text(open(file, "r", encoding="utf-8").read())
        with col5:
            if st.button("πŸ—‘", key=f"delete_{file}"):
                os.remove(file)
                st.rerun()
        if colFollowUp == ".md":
            colFollowUp=ext
            st.markdown(content.decode("utf-8"))
            SpeechSynthesis(content.decode("utf-8"))
def create_zip_of_files(files):
    zip_name = "Files.zip"
    with zipfile.ZipFile(zip_name, 'w') as zipf:
        for file in files:
            zipf.write(file)
    return zip_name

def get_zip_download_link(zip_file):
    with open(zip_file, 'rb') as f:
        data = f.read()
    b64 = base64.b64encode(data).decode()
    return f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'

@st.cache_resource
def get_table_download_link(file_path):
    with open(file_path, 'rb') as f:
        data = f.read()
    b64 = base64.b64encode(data).decode()
    file_name = os.path.basename(file_path)
    ext = os.path.splitext(file_name)[1].lower()
    mime_type = "text/markdown" if ext == ".md" else "application/pdf" if ext == ".pdf" else "image/png" if ext in [".png", ".jpg", ".jpeg"] else "audio/wav" if ext == ".wav" else "audio/mpeg" if ext == ".mp3" else "video/mp4" if ext == ".mp4" else "application/octet-stream"
    return f'<a href="data:{mime_type};base64,{b64}" download="{file_name}">{file_name}</a>'

# Main Function
def main():
    st.markdown("##### GPT-4o Omni Model: Text, Audio, Image, Video & RAG")
    model_options = ["gpt-4o-2024-05-13", "gpt-3.5-turbo"]
    st.session_state["openai_model"] = st.selectbox("Select GPT Model", model_options, index=0)
    
    option = st.selectbox("Select Input Type", ("Text", "Image", "Audio", "Video", "ArXiv Search", "RAG PDF Gallery"))
    
    if option == "Text":
        default_text = "Create a summary of PDF py libraries and usage in py with emojis in markdown. Maybe a buckeyball feature rating comparing them against each other in markdown emoji outline or tables."
        col1, col2 = st.columns([1, 5])
        with col1:
            if st.button("πŸ“ MD", key="md_button"):
                st.session_state["text_input"] = default_text
                with st.spinner("Processing..."):
                    process_text(default_text)
                st.rerun()
        with col2:
            text_input = st.text_input("Enter your text:", value=st.session_state.get("text_input", ""), key="text_input_field")
        if text_input and st.button("Submit Text"):
            with st.spinner("Processing..."):
                process_text(text_input)
            st.rerun()
    
    elif option == "Image":
        col1, col2 = st.columns(2)
        with col1:
            if st.button("πŸ“ Describe"):
                st.session_state["image_prompt"] = "Describe this image and list ten facts in a markdown outline with emojis."
        with col2:
            if st.button("πŸ” OCR"):
                st.session_state["image_prompt"] = "Show electronic text of text in the image."
        text_input = st.text_input("Image Prompt:", value=st.session_state.get("image_prompt", "Describe this image and list ten facts in a markdown outline with emojis."))
        image_input = st.file_uploader("Upload an image (max 200MB)", type=["png", "jpg", "jpeg"], accept_multiple_files=False)
        if image_input and text_input and st.button("Submit Image"):
            if image_input.size > 200 * 1024 * 1024:
                st.error("Image exceeds 200MB limit.")
            else:
                with st.spinner("Processing..."):
                    image_response = process_image(image_input, text_input)
                    with st.chat_message("ai", avatar="πŸ¦–"):
                        st.markdown(image_response)
                st.rerun()
    
    elif option == "Audio":
        text_input = st.text_input("Audio Prompt:", value="Summarize this audio transcription in Markdown.")
        audio_input = st.file_uploader("Upload an audio file (max 200MB)", type=["mp3", "wav", "flac", "m4a"], accept_multiple_files=False)
        audio_bytes = audio_recorder()
        if audio_bytes and text_input and st.button("Submit Audio Recording"):
            with open("recorded_audio.wav", "wb") as f:
                f.write(audio_bytes)
            with st.spinner("Processing..."):
                process_audio(audio_bytes, text_input)
            st.rerun()
        elif audio_input and text_input and st.button("Submit Audio File"):
            with st.spinner("Processing..."):
                process_audio(audio_input, text_input)
            st.rerun()
    
    elif option == "Video":
        text_input = st.text_input("Video Prompt:", value="Summarize this video and its transcription in Markdown.")
        video_input = st.file_uploader("Upload a video file (max 200MB)", type=["mp4"], accept_multiple_files=False)
        if video_input and text_input and st.button("Submit Video"):
            if video_input.size > 200 * 1024 * 1024:
                st.error("Video exceeds 200MB limit.")
            else:
                with st.spinner("Processing..."):
                    process_audio_and_video(video_input)
                st.rerun()
    
    elif option == "ArXiv Search":
        query = st.text_input("AI Search ArXiv Scholarly Articles:")
        if query and st.button("Search ArXiv"):
            with st.spinner("Searching ArXiv..."):
                result = search_arxiv(query)
                st.markdown(result)
            st.rerun()
    
    elif option == "RAG PDF Gallery":
        rag_pdf_gallery()

# Chat Display and Input
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("GPT-4o Multimodal ChatBot - What can I help you with?"):
    with st.spinner("Processing..."):
        process_text(prompt)
    st.rerun()

FileSidebar()
main()