File size: 5,000 Bytes
0de03c5
 
 
5badf6b
 
 
725b458
 
d881b0d
725b458
 
0de03c5
d881b0d
0de03c5
d881b0d
0de03c5
725b458
e55f97b
 
 
 
725b458
d881b0d
 
e55f97b
 
 
5badf6b
725b458
 
 
e55f97b
5badf6b
e55f97b
 
 
 
 
 
 
 
 
 
5badf6b
725b458
 
e55f97b
725b458
e55f97b
 
5badf6b
e55f97b
 
 
 
 
 
 
725b458
e55f97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
725b458
 
e55f97b
 
 
725b458
 
e55f97b
725b458
e55f97b
 
 
 
 
 
 
725b458
 
e55f97b
 
725b458
0de03c5
e55f97b
725b458
e55f97b
 
 
725b458
 
e55f97b
0de03c5
e55f97b
 
 
 
725b458
d881b0d
e55f97b
0de03c5
725b458
0de03c5
 
725b458
d881b0d
725b458
 
5badf6b
d881b0d
e55f97b
5badf6b
e55f97b
d881b0d
725b458
 
 
5badf6b
725b458
 
e55f97b
725b458
 
 
5badf6b
725b458
e55f97b
 
 
 
 
 
5badf6b
725b458
 
 
e55f97b
 
5badf6b
e55f97b
 
d881b0d
5badf6b
d881b0d
725b458
 
e55f97b
 
 
 
 
0de03c5
 
e55f97b
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
import os
import gradio as gr
import openai

# Optional imports for extended functionality
import base64
import fitz  # PyMuPDF
from io import BytesIO
from moviepy.video.io.VideoFileClip import VideoFileClip
from gtts import gTTS
from gradio_client import Client

# 🔐 CONFIG
KEY_FILE = "openai_api_key.txt"
MODEL = "gpt-4o-2024-05-13"

# 🔧 Helpers

def save_api_key(key: str) -> str:
    """Save a new OpenAI API key to disk."""
    with open(KEY_FILE, "w") as f:
        f.write(key.strip())
    return "🔑 Key saved!"


def get_api_key(input_key: str) -> str:
    """Determine which OpenAI key to use: input > saved > env."""
    # 1️⃣ User-provided key
    if input_key and input_key.strip():
        save_api_key(input_key)
        return input_key.strip()

    # 2️⃣ Previously saved key
    if os.path.exists(KEY_FILE):
        with open(KEY_FILE) as f:
            return f.read().strip()

    # 3️⃣ Environment variable
    env_key = os.getenv("OPENAI_KEY", "")
    if env_key:
        save_api_key(env_key)
        return env_key

    # ❗ No key found
    raise gr.Error("❗ OpenAI API key required.")


def apply_filters(files, filters):
    """
    Filter uploaded files by extension and user-selected filters.
    Returns a list for the Gallery display and tracking info for table generation.
    """
    gallery_items = []
    tracking = []

    if not files:
        return gallery_items, tracking

    for f in files:
        path = getattr(f, "name", f)
        ext = os.path.splitext(path)[1].lower()

        if ext in ['.png', '.jpg', '.jpeg'] and '🖼️ Images' in filters:
            gallery_items.append(f)
            tracking.append((path, 'image'))
        if ext in ['.wav', '.mp3'] and '🎤 Audio' in filters:
            gallery_items.append(f)
            tracking.append((path, 'audio'))
        if ext in ['.mp4', '.webm'] and '🎥 Video' in filters:
            gallery_items.append(f)
            tracking.append((path, 'video'))
        if ext == '.pdf' and '📄 PDF' in filters:
            gallery_items.append(f)
            tracking.append((path, 'pdf'))

    return gallery_items, tracking


def generate_table(selected, api_key):
    """
    Build a Markdown table of selected items with type icons.
    """
    key = get_api_key(api_key)
    openai.api_key = key

    md = "| ✅ | Type | Filename |\n|---|------|----------|\n"
    emoji_map = {
        'image': '🖼️',
        'audio': '🎤',
        'video': '🎥',
        'pdf': '📄'
    }

    for path, typ in selected:
        name = os.path.basename(path)
        md += f"| ✅ | {emoji_map.get(typ, '')} {typ.capitalize()} | {name} |\n"

    return md


def chat_handler(api_key, message, history):
    """
    Send a user message + chat history to OpenAI and append the response.
    """
    key = get_api_key(api_key)
    openai.api_key = key

    messages = []
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})

    messages.append({"role": "user", "content": message})
    resp = openai.ChatCompletion.create(model=MODEL, messages=messages)

    answer = resp.choices[0].message.content
    history.append((message, answer))
    return history

# 🔑 UI Definition
with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
    gr.Markdown("# 🔬🧠 ScienceBrain Gradio")

    # --- API Key Input & Save ---
    with gr.Row():
        api_input = gr.Textbox(
            label="🔑 OpenAI Key", type="password", value=os.getenv("OPENAI_KEY", "")
        )
        save_btn = gr.Button("💾 Save Key")
        status_txt = gr.Textbox(interactive=False)
        save_btn.click(save_api_key, inputs=api_input, outputs=status_txt)

    # --- Media Gallery & Filters ---
    gr.Markdown("## 📋 Media Gallery & Filters")
    upload = gr.File(file_count="multiple", label="Upload files (images, audio, videos, PDFs)")
    gallery = gr.Gallery(label="Filtered Gallery", columns=4)
    filter_opts = ["🖼️ Images", "🎤 Audio", "🎥 Video", "📄 PDF"]
    filters = gr.CheckboxGroup(filter_opts, value=filter_opts, label="🔍 Filter Types")
    select_btn = gr.Button("⚙️ Apply Filters")
    selected = gr.State()

    select_btn.click(
        apply_filters,
        inputs=[upload, filters],
        outputs=[gallery, selected]
    )

    # --- Summary Table ---
    gr.Markdown("## 📝 Summary Table")
    table_md = gr.Markdown()
    table_btn = gr.Button("Generate Table")
    table_btn.click(
        generate_table,
        inputs=[selected, api_input],
        outputs=[table_md]
    )

    # --- Chat Tab ---
    with gr.Tab("💬 Chat"):
        chatbot = gr.Chatbot()
        msg_in = gr.Textbox(placeholder="Type your message...")
        msg_in.submit(
            chat_handler,
            inputs=[api_input, msg_in, chatbot],
            outputs=[chatbot]
        )

if __name__ == "__main__":
    demo.launch()