awacke1 commited on
Commit
5badf6b
·
verified ·
1 Parent(s): e55f97b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -26
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import os
2
- import base64
3
  import gradio as gr
4
  import openai
 
 
 
5
  import fitz # PyMuPDF
6
  from io import BytesIO
7
  from moviepy.video.io.VideoFileClip import VideoFileClip
@@ -23,12 +25,12 @@ def save_api_key(key: str) -> str:
23
 
24
  def get_api_key(input_key: str) -> str:
25
  """Determine which OpenAI key to use: input > saved > env."""
26
- # 1️⃣ User-provided
27
  if input_key and input_key.strip():
28
  save_api_key(input_key)
29
  return input_key.strip()
30
 
31
- # 2️⃣ Previously saved
32
  if os.path.exists(KEY_FILE):
33
  with open(KEY_FILE) as f:
34
  return f.read().strip()
@@ -39,14 +41,14 @@ def get_api_key(input_key: str) -> str:
39
  save_api_key(env_key)
40
  return env_key
41
 
42
- # ❗ None found
43
  raise gr.Error("❗ OpenAI API key required.")
44
 
45
 
46
  def apply_filters(files, filters):
47
  """
48
  Filter uploaded files by extension and user-selected filters.
49
- Returns a list for the Gallery display and a list of (path, type) for tracking.
50
  """
51
  gallery_items = []
52
  tracking = []
@@ -55,7 +57,6 @@ def apply_filters(files, filters):
55
  return gallery_items, tracking
56
 
57
  for f in files:
58
- # file may be a tempfile object or filepath string
59
  path = getattr(f, "name", f)
60
  ext = os.path.splitext(path)[1].lower()
61
 
@@ -82,7 +83,6 @@ def generate_table(selected, api_key):
82
  key = get_api_key(api_key)
83
  openai.api_key = key
84
 
85
- # Header
86
  md = "| ✅ | Type | Filename |\n|---|------|----------|\n"
87
  emoji_map = {
88
  'image': '🖼️',
@@ -117,30 +117,27 @@ def chat_handler(api_key, message, history):
117
  history.append((message, answer))
118
  return history
119
 
120
-
121
  # 🔑 UI Definition
122
  with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
123
  gr.Markdown("# 🔬🧠 ScienceBrain Gradio")
124
 
 
125
  with gr.Row():
126
  api_input = gr.Textbox(
127
- label="🔑 OpenAI Key",
128
- type="password",
129
- value=os.getenv("OPENAI_KEY", "")
130
  )
131
  save_btn = gr.Button("💾 Save Key")
132
  status_txt = gr.Textbox(interactive=False)
133
-
134
  save_btn.click(save_api_key, inputs=api_input, outputs=status_txt)
135
 
136
- # 📋 Media Gallery & Filters
137
  gr.Markdown("## 📋 Media Gallery & Filters")
138
  upload = gr.File(file_count="multiple", label="Upload files (images, audio, videos, PDFs)")
139
  gallery = gr.Gallery(label="Filtered Gallery", columns=4)
140
  filter_opts = ["🖼️ Images", "🎤 Audio", "🎥 Video", "📄 PDF"]
141
  filters = gr.CheckboxGroup(filter_opts, value=filter_opts, label="🔍 Filter Types")
142
  select_btn = gr.Button("⚙️ Apply Filters")
143
- selected = gr.Variable()
144
 
145
  select_btn.click(
146
  apply_filters,
@@ -148,26 +145,17 @@ with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
148
  outputs=[gallery, selected]
149
  )
150
 
151
- # Include in Discussion
152
- gr.Markdown("## ✅ Include in Discussion")
153
- disc = gr.CheckboxGroup(label="Select items", choices=[])
154
- gallery.select(
155
- lambda x: [x],
156
- inputs=[gallery],
157
- outputs=[disc]
158
- )
159
-
160
- # 📝 Summary Table
161
  gr.Markdown("## 📝 Summary Table")
162
  table_md = gr.Markdown()
163
  table_btn = gr.Button("Generate Table")
164
  table_btn.click(
165
  generate_table,
166
- inputs=[disc, api_input],
167
  outputs=[table_md]
168
  )
169
 
170
- # 💬 Chat Tab
171
  with gr.Tab("💬 Chat"):
172
  chatbot = gr.Chatbot()
173
  msg_in = gr.Textbox(placeholder="Type your message...")
 
1
  import os
 
2
  import gradio as gr
3
  import openai
4
+
5
+ # Optional imports for extended functionality
6
+ import base64
7
  import fitz # PyMuPDF
8
  from io import BytesIO
9
  from moviepy.video.io.VideoFileClip import VideoFileClip
 
25
 
26
  def get_api_key(input_key: str) -> str:
27
  """Determine which OpenAI key to use: input > saved > env."""
28
+ # 1️⃣ User-provided key
29
  if input_key and input_key.strip():
30
  save_api_key(input_key)
31
  return input_key.strip()
32
 
33
+ # 2️⃣ Previously saved key
34
  if os.path.exists(KEY_FILE):
35
  with open(KEY_FILE) as f:
36
  return f.read().strip()
 
41
  save_api_key(env_key)
42
  return env_key
43
 
44
+ # ❗ No key found
45
  raise gr.Error("❗ OpenAI API key required.")
46
 
47
 
48
  def apply_filters(files, filters):
49
  """
50
  Filter uploaded files by extension and user-selected filters.
51
+ Returns a list for the Gallery display and tracking info for table generation.
52
  """
53
  gallery_items = []
54
  tracking = []
 
57
  return gallery_items, tracking
58
 
59
  for f in files:
 
60
  path = getattr(f, "name", f)
61
  ext = os.path.splitext(path)[1].lower()
62
 
 
83
  key = get_api_key(api_key)
84
  openai.api_key = key
85
 
 
86
  md = "| ✅ | Type | Filename |\n|---|------|----------|\n"
87
  emoji_map = {
88
  'image': '🖼️',
 
117
  history.append((message, answer))
118
  return history
119
 
 
120
  # 🔑 UI Definition
121
  with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
122
  gr.Markdown("# 🔬🧠 ScienceBrain Gradio")
123
 
124
+ # --- API Key Input & Save ---
125
  with gr.Row():
126
  api_input = gr.Textbox(
127
+ label="🔑 OpenAI Key", type="password", value=os.getenv("OPENAI_KEY", "")
 
 
128
  )
129
  save_btn = gr.Button("💾 Save Key")
130
  status_txt = gr.Textbox(interactive=False)
 
131
  save_btn.click(save_api_key, inputs=api_input, outputs=status_txt)
132
 
133
+ # --- Media Gallery & Filters ---
134
  gr.Markdown("## 📋 Media Gallery & Filters")
135
  upload = gr.File(file_count="multiple", label="Upload files (images, audio, videos, PDFs)")
136
  gallery = gr.Gallery(label="Filtered Gallery", columns=4)
137
  filter_opts = ["🖼️ Images", "🎤 Audio", "🎥 Video", "📄 PDF"]
138
  filters = gr.CheckboxGroup(filter_opts, value=filter_opts, label="🔍 Filter Types")
139
  select_btn = gr.Button("⚙️ Apply Filters")
140
+ selected = gr.State()
141
 
142
  select_btn.click(
143
  apply_filters,
 
145
  outputs=[gallery, selected]
146
  )
147
 
148
+ # --- Summary Table ---
 
 
 
 
 
 
 
 
 
149
  gr.Markdown("## 📝 Summary Table")
150
  table_md = gr.Markdown()
151
  table_btn = gr.Button("Generate Table")
152
  table_btn.click(
153
  generate_table,
154
+ inputs=[selected, api_input],
155
  outputs=[table_md]
156
  )
157
 
158
+ # --- Chat Tab ---
159
  with gr.Tab("💬 Chat"):
160
  chatbot = gr.Chatbot()
161
  msg_in = gr.Textbox(placeholder="Type your message...")