File size: 4,295 Bytes
0de03c5
d881b0d
0de03c5
 
725b458
 
d881b0d
725b458
 
0de03c5
d881b0d
0de03c5
d881b0d
0de03c5
725b458
 
0de03c5
d881b0d
0de03c5
 
725b458
 
d881b0d
725b458
d881b0d
 
725b458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0de03c5
725b458
 
 
 
0de03c5
 
725b458
 
 
d881b0d
0de03c5
725b458
0de03c5
 
725b458
 
 
d881b0d
725b458
d881b0d
725b458
 
d881b0d
725b458
d881b0d
725b458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d881b0d
 
725b458
 
 
0de03c5
 
cc11cd1
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
import os
import base64
import gradio as gr
import openai
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"

# Load stored API key
DEFAULT_KEY = ""
if os.path.exists(KEY_FILE):
    with open(KEY_FILE, 'r') as f:
        DEFAULT_KEY = f.read().strip()

# 🔧 Helpers
def save_api_key(key):
    with open(KEY_FILE, 'w') as f:
        f.write(key.strip())
    return "🔑 Key saved!"

def get_api_key(input_key):
    # Priority: user input > stored > ENV
    if input_key and input_key.strip():
        save_api_key(input_key)
        return input_key.strip()
    if DEFAULT_KEY:
        return DEFAULT_KEY
    env = os.getenv('OPENAI_KEY', '')
    if env:
        save_api_key(env)
        return env
    raise gr.Error("❗ OpenAI API key required.")

# 📋 Gallery Processing
def apply_filters(files, filters):
    selected = []
    for f in files:
        lower = f.lower()
        if any(lower.endswith(ext) for ext in ['.png', '.jpg', '.jpeg']) and '🖼️ Images' in filters:
            selected.append((f, 'image'))
        if any(lower.endswith(ext) for ext in ['.wav', '.mp3']) and '🎤 Audio' in filters:
            selected.append((f, 'audio'))
        if any(lower.endswith(ext) for ext in ['.mp4', '.webm']) and '🎥 Video' in filters:
            selected.append((f, 'video'))
        if lower.endswith('.pdf') and '📄 PDF' in filters:
            selected.append((f, 'pdf'))
    return selected

def generate_table(selected, api_key):
    key = get_api_key(api_key)
    openai.api_key = key
    # Build markdown table
    md = "| ✅ | Type | Filename |\n|---|------|----------|\n"
    for path, typ in selected:
        emoji = '🖼️' if typ=='image' else '🎤' if typ=='audio' else '🎥' if typ=='video' else '📄'
        name = os.path.basename(path)
        md += f"| ✅ | {emoji} {typ.capitalize()} | {name} |\n"
    return md

# 🗨️ Chat Handler
def chat_handler(api_key, message, history):
    key = get_api_key(api_key)
    openai.api_key = key
    messages = []
    for u, a in history:
        messages.append({"role": "user",    "content": u})
        messages.append({"role": "assistant","content": a})
    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

# 🎞️ Example: Video Summarizer (placeholder)
# def summarize_video(api_key, file_path, prompt):
#     ... implementation ...

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

    with gr.Row():
        api_input = gr.Textbox(label="🔑 OpenAI Key", value=DEFAULT_KEY, type="password")
        save_btn = gr.Button("💾 Save Key")
        status_txt = gr.Textbox(interactive=False)
        save_btn.click(save_api_key, inputs=api_input, outputs=status_txt)

    gr.Markdown("## 📋 Media Gallery & Filters")
    upload = gr.File(file_count="multiple", label="Upload files (images, audio, videos, PDFs)")
    gallery = gr.Gallery(label="Filtered Gallery").style(grid=[4], height="auto")
    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.Variable()
    select_btn.click(apply_filters, inputs=[upload, filters], outputs=[gallery, selected])

    gr.Markdown("## ✅ Include in Discussion")
    disc = gr.CheckboxGroup(label="Select items", choices=[])
    gallery.select(lambda x: [x], None, disc)

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

    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()