awacke1 commited on
Commit
725b458
·
verified ·
1 Parent(s): 9bcf4ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -160
app.py CHANGED
@@ -1,193 +1,120 @@
 
1
  import os
2
  import base64
3
- import re
4
- import requests
5
- import pytz
6
- import json
7
- from io import BytesIO
8
- from datetime import datetime
9
  import gradio as gr
10
  import openai
11
- import fitz # pymupdf
12
- from bs4 import BeautifulSoup
13
  from moviepy.video.io.VideoFileClip import VideoFileClip
 
 
14
 
15
  # 🔐 CONFIG
16
  KEY_FILE = "openai_api_key.txt"
17
  MODEL = "gpt-4o-2024-05-13"
18
 
19
- # 🍿 Default key load
 
20
  if os.path.exists(KEY_FILE):
21
  with open(KEY_FILE, 'r') as f:
22
  DEFAULT_KEY = f.read().strip()
23
- else:
24
- DEFAULT_KEY = ''
25
 
26
- # 🔧 HELPERS
27
- def save_api_key(api_key):
28
  with open(KEY_FILE, 'w') as f:
29
- f.write(api_key.strip())
30
  return "🔑 Key saved!"
31
 
32
- # 🗒️ Chat
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- def chat_with_openai(api_key, user_message, history):
35
- openai.api_key = api_key.strip()
 
 
36
  messages = []
37
  for u, a in history:
38
- messages.append({"role": "user", "content": u})
39
- messages.append({"role": "assistant", "content": a})
40
- messages.append({"role": "user", "content": user_message})
41
  resp = openai.ChatCompletion.create(model=MODEL, messages=messages)
42
  answer = resp.choices[0].message.content
43
- history.append((user_message, answer))
44
  return history
45
 
46
- # 🖼️ Image analysis
47
- def image_to_base64(file):
48
- return base64.b64encode(file.read()).decode()
49
-
50
- def analyze_image(api_key, file, prompt):
51
- data_uri = f"data:image/png;base64,{image_to_base64(file)}"
52
- openai.api_key = api_key.strip()
53
- resp = openai.ChatCompletion.create(
54
- model=MODEL,
55
- messages=[
56
- {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
57
- {"role": "user", "content": [
58
- {"type":"text","text":prompt},
59
- {"type":"image_url","image_url":{"url":data_uri}}
60
- ]}
61
- ]
62
- )
63
- return resp.choices[0].message.content
64
-
65
- # 🎤 Audio transcription + chat
66
- def transcribe_audio_file(api_key, file):
67
- openai.api_key = api_key.strip()
68
- resp = openai.Audio.transcriptions.create(model="whisper-1", file=file)
69
- return resp.text
70
-
71
- # 🎥 Video summarize
72
- def summarize_video(api_key, file, prompt, seconds=2):
73
- # save tmp
74
- with open("tmp_vid.mp4", 'wb') as f: f.write(file.read())
75
- clip = VideoFileClip("tmp_vid.mp4")
76
- frames = []
77
- step = int(clip.fps * seconds)
78
- for t in range(0, int(clip.duration), seconds):
79
- frame = clip.get_frame(t)
80
- buf = BytesIO()
81
- from PIL import Image
82
- Image.fromarray(frame).save(buf, format='JPEG')
83
- frames.append(base64.b64encode(buf.getvalue()).decode())
84
- transcript = transcribe_audio_file(api_key, open("tmp_vid.mp4", 'rb'))
85
- openai.api_key = api_key.strip()
86
- messages = [{"role":"system","content":"You are a helpful assistant."},
87
- {"role":"user","content": prompt}]
88
- for f64 in frames:
89
- messages.append({"role":"user","content": {"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{f64}"}}})
90
- messages.append({"role":"user","content": f"Transcript: {transcript}"})
91
- resp = openai.ChatCompletion.create(model=MODEL, messages=messages)
92
- return resp.choices[0].message.content
93
-
94
- # 📄 PDF->Markdown
95
- def pdf_to_markdown(path):
96
- doc = fitz.open(path)
97
- md = ''
98
- for page in doc:
99
- md += page.get_text('markdown') + '\n'
100
- return md
101
-
102
- # 🔍 ArXiv RAG
103
- from gradio_client import Client
104
- def arxiv_search(query):
105
- client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
106
- refs = client.predict(query, 10, "Semantic Search - up to 10 Mar 2024", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md")
107
- ans = client.predict(query, "mistralai/Mixtral-8x7B-Instruct-v0.1", True, api_name="/ask_llm")
108
- return refs + "\n" + ans
109
-
110
- # 🔈 TTS
111
- from gtts import gTTS
112
- def tts_bytes(text):
113
- buf = BytesIO()
114
- gTTS(text=text, lang='en').write_to_fp(buf)
115
- buf.seek(0)
116
- return buf.read()
117
-
118
- # UI CONFIG
119
- ui_config = {
120
- "chat": {"label":"💬 ChatGPT-4o", "placeholder":"Say something..."},
121
- "image_prompt": {"label":"🖼️ Image Prompt", "default":"Describe this image..."},
122
- "audio_prompt": {"label":"🎤 Audio Prompt", "default":"Transcribe and summarize..."},
123
- "video_prompt": {"label":"🎥 Video Prompt", "default":"Summarize this video..."},
124
- "pdf_prompt": {"label":"📄 PDF Prompt", "default":"Convert PDF to markdown..."},
125
- "arxiv_prompt": {"label":"🔍 Arxiv Query", "default":"Search papers..."}
126
- }
127
 
 
128
  with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
129
- gr.Markdown("# 🔬🧠 ScienceBrain Gradio\nEnter API key below.")
 
130
  with gr.Row():
131
- api_key = gr.Textbox(label="🔑 OpenAI Key", value=DEFAULT_KEY, type="password")
132
  save_btn = gr.Button("💾 Save Key")
133
- status = gr.Textbox(label="Status", interactive=False)
134
- save_btn.click(save_api_key, inputs=api_key, outputs=status)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- # Tabs for each modality
137
  with gr.Tab("💬 Chat"):
138
- chatbot = gr.Chatbot(label=ui_config['chat']['label'], value=[])
139
- msg = gr.Textbox(label=ui_config['chat']['label'], placeholder=ui_config['chat']['placeholder'])
140
- msg.submit(chat_with_openai, inputs=[api_key, msg, chatbot], outputs=chatbot)
141
-
142
- with gr.Tab("🖼️ Image"):
143
- img_in = gr.File(file_types=['png','jpg','jpeg'])
144
- img_prompt = gr.Textbox(label=ui_config['image_prompt']['label'], value=ui_config['image_prompt']['default'])
145
- img_btn = gr.Button("🔍 Analyze Image")
146
- img_out = gr.Markdown()
147
- img_btn.click(analyze_image, inputs=[api_key, img_in, img_prompt], outputs=img_out)
148
-
149
- with gr.Tab("🎤 Audio"):
150
- aud_in = gr.File(file_types=['wav','mp3'])
151
- aud_btn = gr.Button("🔊 Transcribe + Chat")
152
- aud_out = gr.Markdown()
153
- def audio_pipeline(key, f):
154
- text = transcribe_audio_file(key, f)
155
- reply = chat_with_openai(key, text, [])[-1][1]
156
- return f"**Transcript:** {text}\n\n**Reply:** {reply}"
157
- aud_btn.click(audio_pipeline, inputs=[api_key, aud_in], outputs=aud_out)
158
-
159
- with gr.Tab("🎥 Video"):
160
- vid_in = gr.File(file_types=['mp4'])
161
- vid_prompt = gr.Textbox(label=ui_config['video_prompt']['label'], value=ui_config['video_prompt']['default'])
162
- vid_btn = gr.Button("🎞️ Summarize Video")
163
- vid_out = gr.Markdown()
164
- vid_btn.click(summarize_video, inputs=[api_key, vid_in, vid_prompt], outputs=vid_out)
165
-
166
- with gr.Tab("📄 PDF"):
167
- pdf_in = gr.File(file_types=['pdf'])
168
- pdf_btn = gr.Button("📝 Convert PDF")
169
- pdf_out = gr.Markdown()
170
- pdf_btn.click(lambda f: pdf_to_markdown(f.name), inputs=[pdf_in], outputs=pdf_out)
171
-
172
- with gr.Tab("🔍 ArXiv"):
173
- arxiv_in = gr.Textbox(label=ui_config['arxiv_prompt']['label'], value=ui_config['arxiv_prompt']['default'])
174
- arxiv_btn = gr.Button("🔎 Search ArXiv")
175
- arxiv_out = gr.Markdown()
176
- arxiv_btn.click(arxiv_search, inputs=[arxiv_in], outputs=arxiv_out)
177
-
178
- with gr.Tab("⚙️ Quick Tests"):
179
- tests = [
180
- ("📝 Text","What is 2+2?"),
181
- ("🖼️ Image","Analyze image https://via.placeholder.com/150.png"),
182
- ("🎤 Audio","Transcribe https://www2.cs.uic.edu/~i101/SoundFiles/gettysburg10.wav"),
183
- ("🎥 Video","Summarize video https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4"),
184
- ("🖼️+📝 Img+Txt","Given image https://via.placeholder.com/150.png list 3 facts."),
185
- ("🎤+📝 Aud+Txt","Given audio https://www2.cs.uic.edu/~i101/SoundFiles/gettysburg10.wav summarize."),
186
- ("🎥+📝 Vid+Txt","Given video https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4 transcript+summary.")
187
- ]
188
- for idx, (e,p) in enumerate(tests,1):
189
- btn = gr.Button(f"{idx}. {e} Test")
190
- btn.click(chat_with_openai, inputs=[api_key, gr.State(p), gr.State([])], outputs=chatbot)
191
 
192
  if __name__ == "__main__":
193
  demo.launch()
 
 
1
+ ```python
2
  import os
3
  import base64
 
 
 
 
 
 
4
  import gradio as gr
5
  import openai
6
+ import fitz # PyMuPDF
7
+ from io import BytesIO
8
  from moviepy.video.io.VideoFileClip import VideoFileClip
9
+ from gtts import gTTS
10
+ from gradio_client import Client
11
 
12
  # 🔐 CONFIG
13
  KEY_FILE = "openai_api_key.txt"
14
  MODEL = "gpt-4o-2024-05-13"
15
 
16
+ # Load stored API key
17
+ DEFAULT_KEY = ""
18
  if os.path.exists(KEY_FILE):
19
  with open(KEY_FILE, 'r') as f:
20
  DEFAULT_KEY = f.read().strip()
 
 
21
 
22
+ # 🔧 Helpers
23
+ def save_api_key(key):
24
  with open(KEY_FILE, 'w') as f:
25
+ f.write(key.strip())
26
  return "🔑 Key saved!"
27
 
28
+ def get_api_key(input_key):
29
+ # Priority: user input > stored > ENV
30
+ if input_key and input_key.strip():
31
+ save_api_key(input_key)
32
+ return input_key.strip()
33
+ if DEFAULT_KEY:
34
+ return DEFAULT_KEY
35
+ env = os.getenv('OPENAI_KEY', '')
36
+ if env:
37
+ save_api_key(env)
38
+ return env
39
+ raise gr.Error("❗ OpenAI API key required.")
40
+
41
+ # 📋 Gallery Processing
42
+ def apply_filters(files, filters):
43
+ selected = []
44
+ for f in files:
45
+ lower = f.lower()
46
+ if any(lower.endswith(ext) for ext in ['.png', '.jpg', '.jpeg']) and '🖼️ Images' in filters:
47
+ selected.append((f, 'image'))
48
+ if any(lower.endswith(ext) for ext in ['.wav', '.mp3']) and '🎤 Audio' in filters:
49
+ selected.append((f, 'audio'))
50
+ if any(lower.endswith(ext) for ext in ['.mp4', '.webm']) and '🎥 Video' in filters:
51
+ selected.append((f, 'video'))
52
+ if lower.endswith('.pdf') and '📄 PDF' in filters:
53
+ selected.append((f, 'pdf'))
54
+ return selected
55
+
56
+ def generate_table(selected, api_key):
57
+ key = get_api_key(api_key)
58
+ openai.api_key = key
59
+ # Build markdown table
60
+ md = "| ✅ | Type | Filename |\n|---|------|----------|\n"
61
+ for path, typ in selected:
62
+ emoji = '🖼️' if typ=='image' else '🎤' if typ=='audio' else '🎥' if typ=='video' else '📄'
63
+ name = os.path.basename(path)
64
+ md += f"| ✅ | {emoji} {typ.capitalize()} | {name} |\n"
65
+ return md
66
 
67
+ # 🗨️ Chat Handler
68
+ def chat_handler(api_key, message, history):
69
+ key = get_api_key(api_key)
70
+ openai.api_key = key
71
  messages = []
72
  for u, a in history:
73
+ messages.append({"role": "user", "content": u})
74
+ messages.append({"role": "assistant","content": a})
75
+ messages.append({"role": "user", "content": message})
76
  resp = openai.ChatCompletion.create(model=MODEL, messages=messages)
77
  answer = resp.choices[0].message.content
78
+ history.append((message, answer))
79
  return history
80
 
81
+ # 🎞️ Example: Video Summarizer (placeholder)
82
+ # def summarize_video(api_key, file_path, prompt):
83
+ # ... implementation ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ # 🔑 UI Definition
86
  with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
87
+ gr.Markdown("# 🔬🧠 ScienceBrain Gradio")
88
+
89
  with gr.Row():
90
+ api_input = gr.Textbox(label="🔑 OpenAI Key", value=DEFAULT_KEY, type="password")
91
  save_btn = gr.Button("💾 Save Key")
92
+ status_txt = gr.Textbox(interactive=False)
93
+ save_btn.click(save_api_key, inputs=api_input, outputs=status_txt)
94
+
95
+ gr.Markdown("## 📋 Media Gallery & Filters")
96
+ upload = gr.File(file_count="multiple", label="Upload files (images, audio, videos, PDFs)")
97
+ gallery = gr.Gallery(label="Filtered Gallery").style(grid=[4], height="auto")
98
+ filter_opts = ["🖼️ Images", "🎤 Audio", "🎥 Video", "📄 PDF"]
99
+ filters = gr.CheckboxGroup(filter_opts, value=filter_opts, label="🔍 Filter Types")
100
+ select_btn = gr.Button("⚙️ Apply Filters")
101
+ selected = gr.Variable()
102
+ select_btn.click(apply_filters, inputs=[upload, filters], outputs=[gallery, selected])
103
+
104
+ gr.Markdown("## ✅ Include in Discussion")
105
+ disc = gr.CheckboxGroup(label="Select items", choices=[])
106
+ gallery.select(lambda x: [x], None, disc)
107
+
108
+ gr.Markdown("## 📝 Summary Table")
109
+ table_md = gr.Markdown()
110
+ table_btn = gr.Button("Generate Table")
111
+ table_btn.click(generate_table, inputs=[disc, api_input], outputs=table_md)
112
 
 
113
  with gr.Tab("💬 Chat"):
114
+ chatbot = gr.Chatbot()
115
+ msg_in = gr.Textbox(placeholder="Type your message...")
116
+ msg_in.submit(chat_handler, inputs=[api_input, msg_in, chatbot], outputs=chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  if __name__ == "__main__":
119
  demo.launch()
120
+ ```