Spaces:
Runtime error
Runtime error
File size: 7,939 Bytes
0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d 0de03c5 d881b0d |
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 |
import os
import base64
import re
import requests
import pytz
import json
from io import BytesIO
from datetime import datetime
import gradio as gr
import openai
import fitz # pymupdf
from bs4 import BeautifulSoup
from moviepy.video.io.VideoFileClip import VideoFileClip
# 🔐 CONFIG
KEY_FILE = "openai_api_key.txt"
MODEL = "gpt-4o-2024-05-13"
# 🍿 Default key load
if os.path.exists(KEY_FILE):
with open(KEY_FILE, 'r') as f:
DEFAULT_KEY = f.read().strip()
else:
DEFAULT_KEY = ''
# 🔧 HELPERS
def save_api_key(api_key):
with open(KEY_FILE, 'w') as f:
f.write(api_key.strip())
return "🔑 Key saved!"
# 🗒️ Chat
def chat_with_openai(api_key, user_message, history):
openai.api_key = api_key.strip()
messages = []
for u, a in history:
messages.append({"role": "user", "content": u})
messages.append({"role": "assistant", "content": a})
messages.append({"role": "user", "content": user_message})
resp = openai.ChatCompletion.create(model=MODEL, messages=messages)
answer = resp.choices[0].message.content
history.append((user_message, answer))
return history
# 🖼️ Image analysis
def image_to_base64(file):
return base64.b64encode(file.read()).decode()
def analyze_image(api_key, file, prompt):
data_uri = f"data:image/png;base64,{image_to_base64(file)}"
openai.api_key = api_key.strip()
resp = openai.ChatCompletion.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
{"role": "user", "content": [
{"type":"text","text":prompt},
{"type":"image_url","image_url":{"url":data_uri}}
]}
]
)
return resp.choices[0].message.content
# 🎤 Audio transcription + chat
def transcribe_audio_file(api_key, file):
openai.api_key = api_key.strip()
resp = openai.Audio.transcriptions.create(model="whisper-1", file=file)
return resp.text
# 🎥 Video summarize
def summarize_video(api_key, file, prompt, seconds=2):
# save tmp
with open("tmp_vid.mp4", 'wb') as f: f.write(file.read())
clip = VideoFileClip("tmp_vid.mp4")
frames = []
step = int(clip.fps * seconds)
for t in range(0, int(clip.duration), seconds):
frame = clip.get_frame(t)
buf = BytesIO()
from PIL import Image
Image.fromarray(frame).save(buf, format='JPEG')
frames.append(base64.b64encode(buf.getvalue()).decode())
transcript = transcribe_audio_file(api_key, open("tmp_vid.mp4", 'rb'))
openai.api_key = api_key.strip()
messages = [{"role":"system","content":"You are a helpful assistant."},
{"role":"user","content": prompt}]
for f64 in frames:
messages.append({"role":"user","content": {"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{f64}"}}})
messages.append({"role":"user","content": f"Transcript: {transcript}"})
resp = openai.ChatCompletion.create(model=MODEL, messages=messages)
return resp.choices[0].message.content
# 📄 PDF->Markdown
def pdf_to_markdown(path):
doc = fitz.open(path)
md = ''
for page in doc:
md += page.get_text('markdown') + '\n'
return md
# 🔍 ArXiv RAG
from gradio_client import Client
def arxiv_search(query):
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
refs = client.predict(query, 10, "Semantic Search - up to 10 Mar 2024", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md")
ans = client.predict(query, "mistralai/Mixtral-8x7B-Instruct-v0.1", True, api_name="/ask_llm")
return refs + "\n" + ans
# 🔈 TTS
from gtts import gTTS
def tts_bytes(text):
buf = BytesIO()
gTTS(text=text, lang='en').write_to_fp(buf)
buf.seek(0)
return buf.read()
# UI CONFIG
ui_config = {
"chat": {"label":"💬 ChatGPT-4o", "placeholder":"Say something..."},
"image_prompt": {"label":"🖼️ Image Prompt", "default":"Describe this image..."},
"audio_prompt": {"label":"🎤 Audio Prompt", "default":"Transcribe and summarize..."},
"video_prompt": {"label":"🎥 Video Prompt", "default":"Summarize this video..."},
"pdf_prompt": {"label":"📄 PDF Prompt", "default":"Convert PDF to markdown..."},
"arxiv_prompt": {"label":"🔍 Arxiv Query", "default":"Search papers..."}
}
with gr.Blocks(title="🔬🧠 ScienceBrain.Gradio") as demo:
gr.Markdown("# 🔬🧠 ScienceBrain Gradio
Enter API key below."
)
with gr.Row():
api_key = gr.Textbox(label="🔑 OpenAI Key", value=DEFAULT_KEY, type="password")
save_btn = gr.Button("💾 Save Key")
status = gr.Textbox(label="Status", interactive=False)
save_btn.click(save_api_key, inputs=api_key, outputs=status)
# Tabs for each modality
with gr.Tab("💬 Chat"):
chatbot = gr.Chatbot(label=ui_config['chat']['label'], value=[])
msg = gr.Textbox(label=ui_config['chat']['label'], placeholder=ui_config['chat']['placeholder'])
msg.submit(chat_with_openai, inputs=[api_key, msg, chatbot], outputs=chatbot)
with gr.Tab("🖼️ Image"):
img_in = gr.File(file_types=['png','jpg','jpeg'])
img_prompt = gr.Textbox(label=ui_config['image_prompt']['label'], value=ui_config['image_prompt']['default'])
img_btn = gr.Button("🔍 Analyze Image")
img_out = gr.Markdown()
img_btn.click(analyze_image, inputs=[api_key, img_in, img_prompt], outputs=img_out)
with gr.Tab("🎤 Audio"):
aud_in = gr.File(file_types=['wav','mp3'])
aud_btn = gr.Button("🔊 Transcribe + Chat")
aud_out = gr.Markdown()
def audio_pipeline(key, f):
text = transcribe_audio_file(key, f)
reply = chat_with_openai(key, text, [])[-1][1]
return f"**Transcript:** {text}\n\n**Reply:** {reply}"
aud_btn.click(audio_pipeline, inputs=[api_key, aud_in], outputs=aud_out)
with gr.Tab("🎥 Video"):
vid_in = gr.File(file_types=['mp4'])
vid_prompt = gr.Textbox(label=ui_config['video_prompt']['label'], value=ui_config['video_prompt']['default'])
vid_btn = gr.Button("🎞️ Summarize Video")
vid_out = gr.Markdown()
vid_btn.click(summarize_video, inputs=[api_key, vid_in, vid_prompt], outputs=vid_out)
with gr.Tab("📄 PDF"):
pdf_in = gr.File(file_types=['pdf'])
pdf_btn = gr.Button("📝 Convert PDF")
pdf_out = gr.Markdown()
pdf_btn.click(lambda f: pdf_to_markdown(f.name), inputs=[pdf_in], outputs=pdf_out)
with gr.Tab("🔍 ArXiv"):
arxiv_in = gr.Textbox(label=ui_config['arxiv_prompt']['label'], value=ui_config['arxiv_prompt']['default'])
arxiv_btn = gr.Button("🔎 Search ArXiv")
arxiv_out = gr.Markdown()
arxiv_btn.click(arxiv_search, inputs=[arxiv_in], outputs=arxiv_out)
with gr.Tab("⚙️ Quick Tests"):
tests = [
("📝 Text","What is 2+2?"),
("🖼️ Image","Analyze image https://via.placeholder.com/150.png"),
("🎤 Audio","Transcribe https://www2.cs.uic.edu/~i101/SoundFiles/gettysburg10.wav"),
("🎥 Video","Summarize video https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4"),
("🖼️+📝 Img+Txt","Given image https://via.placeholder.com/150.png list 3 facts."),
("🎤+📝 Aud+Txt","Given audio https://www2.cs.uic.edu/~i101/SoundFiles/gettysburg10.wav summarize."),
("🎥+📝 Vid+Txt","Given video https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4 transcript+summary.")
]
for idx, (e,p) in enumerate(tests,1):
btn = gr.Button(f"{idx}. {e} Test")
btn.click(chat_with_openai, inputs=[api_key, gr.State(p), gr.State([])], outputs=chatbot)
if __name__ == "__main__":
demo.launch()
|