Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import anthropic
|
3 |
-
import openai
|
4 |
import base64
|
5 |
from datetime import datetime
|
6 |
import plotly.graph_objects as go
|
@@ -32,8 +32,8 @@ from xml.etree import ElementTree as ET
|
|
32 |
from openai import OpenAI
|
33 |
|
34 |
# Configuration and Setup
|
35 |
-
Site_Name = 'π€π§
|
36 |
-
title = "π€π§
|
37 |
helpURL = 'https://huggingface.co/awacke1'
|
38 |
bugURL = 'https://huggingface.co/spaces/awacke1'
|
39 |
icons = 'π€π§ π¬π'
|
@@ -69,26 +69,21 @@ if anthropic_key == None:
|
|
69 |
anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
|
70 |
claude_client = anthropic.Anthropic(api_key=anthropic_key)
|
71 |
|
72 |
-
# HuggingFace setup
|
73 |
-
API_URL = os.getenv('API_URL')
|
74 |
-
HF_KEY = os.getenv('HF_KEY')
|
75 |
-
MODEL1 = "meta-llama/Llama-2-7b-chat-hf"
|
76 |
-
MODEL2 = "openai/whisper-small.en"
|
77 |
-
|
78 |
-
headers = {
|
79 |
-
"Authorization": f"Bearer {HF_KEY}",
|
80 |
-
"Content-Type": "application/json"
|
81 |
-
}
|
82 |
-
|
83 |
# Initialize session states
|
84 |
if "chat_history" not in st.session_state:
|
85 |
st.session_state.chat_history = []
|
86 |
if "openai_model" not in st.session_state:
|
87 |
-
st.session_state["openai_model"] = "gpt-
|
88 |
if "messages" not in st.session_state:
|
89 |
st.session_state.messages = []
|
90 |
if "search_queries" not in st.session_state:
|
91 |
st.session_state.search_queries = []
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Custom CSS
|
94 |
st.markdown("""
|
@@ -183,8 +178,10 @@ bike_collections = {
|
|
183 |
}
|
184 |
|
185 |
# File Operations Functions
|
186 |
-
def create_file(filename, prompt, response, is_image=False):
|
187 |
"""Basic file creation with prompt and response."""
|
|
|
|
|
188 |
with open(filename, "w", encoding="utf-8") as f:
|
189 |
f.write(prompt + "\n\n" + response)
|
190 |
|
@@ -274,12 +271,13 @@ def SpeechSynthesis(result):
|
|
274 |
'''
|
275 |
components.html(documentHTML5, width=1280, height=300)
|
276 |
|
277 |
-
# ArXiv Search Functions
|
278 |
-
def search_arxiv(query):
|
279 |
"""Search ArXiv papers using Hugging Face client."""
|
280 |
-
|
281 |
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
282 |
-
|
|
|
283 |
# First query - Get papers
|
284 |
response1 = client.predict(
|
285 |
query,
|
@@ -288,7 +286,7 @@ def search_arxiv(query):
|
|
288 |
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
289 |
api_name="/update_with_rag_md"
|
290 |
)
|
291 |
-
|
292 |
# Second query - Get summary
|
293 |
response2 = client.predict(
|
294 |
query,
|
@@ -296,26 +294,27 @@ def search_arxiv(query):
|
|
296 |
True,
|
297 |
api_name="/ask_llm"
|
298 |
)
|
299 |
-
|
300 |
Question = '### π ' + query + '\r\n'
|
301 |
References = response1[0]
|
302 |
-
References2 = response1[1]
|
303 |
ReferenceLinks = extract_urls(References)
|
304 |
-
|
305 |
-
filename = generate_filename(query, "md")
|
306 |
-
create_file(filename, query, References + ReferenceLinks)
|
307 |
-
|
308 |
results = Question + '\r\n' + response2 + '\r\n' + References + '\r\n' + ReferenceLinks
|
309 |
-
|
|
|
|
|
|
|
310 |
end_time = time.strftime("%Y-%m-%d %H:%M:%S")
|
311 |
start_timestamp = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))
|
312 |
end_timestamp = time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S"))
|
313 |
elapsed_seconds = end_timestamp - start_timestamp
|
314 |
-
|
315 |
st.write(f"Start time: {start_time}")
|
316 |
st.write(f"Finish time: {end_time}")
|
317 |
st.write(f"Elapsed time: {elapsed_seconds:.2f} seconds")
|
318 |
-
|
|
|
|
|
319 |
return results
|
320 |
|
321 |
def extract_urls(text):
|
@@ -325,7 +324,7 @@ def extract_urls(text):
|
|
325 |
abs_link_pattern = re.compile(r'\[(.*?)\]\((https://arxiv\.org/abs/\d+\.\d+)\)')
|
326 |
pdf_link_pattern = re.compile(r'\[β¬οΈ\]\((https://arxiv\.org/pdf/\d+\.\d+)\)')
|
327 |
title_pattern = re.compile(r'### \d{2} \w{3} \d{4} \| \[(.*?)\]')
|
328 |
-
|
329 |
date_matches = date_pattern.findall(text)
|
330 |
abs_link_matches = abs_link_pattern.findall(text)
|
331 |
pdf_link_matches = pdf_link_pattern.findall(text)
|
@@ -349,16 +348,14 @@ def extract_urls(text):
|
|
349 |
|
350 |
# Media Processing Functions
|
351 |
def process_image(image_input, user_prompt):
|
352 |
-
"""Process image with GPT-
|
353 |
if isinstance(image_input, str):
|
354 |
with open(image_input, "rb") as image_file:
|
355 |
image_input = image_file.read()
|
356 |
-
|
357 |
-
base64_image = base64.b64encode(image_input).decode("utf-8")
|
358 |
-
|
359 |
|
|
|
360 |
|
361 |
-
response =
|
362 |
model=st.session_state["openai_model"],
|
363 |
messages=[
|
364 |
{"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
|
@@ -371,7 +368,7 @@ def process_image(image_input, user_prompt):
|
|
371 |
],
|
372 |
temperature=0.0,
|
373 |
)
|
374 |
-
|
375 |
return response.choices[0].message.content
|
376 |
|
377 |
def process_audio(audio_input, text_input=''):
|
@@ -380,17 +377,17 @@ def process_audio(audio_input, text_input=''):
|
|
380 |
with open(audio_input, "rb") as file:
|
381 |
audio_input = file.read()
|
382 |
|
383 |
-
transcription =
|
384 |
model="whisper-1",
|
385 |
file=audio_input,
|
386 |
)
|
387 |
-
|
388 |
st.session_state.messages.append({"role": "user", "content": transcription.text})
|
389 |
-
|
390 |
with st.chat_message("assistant"):
|
391 |
st.markdown(transcription.text)
|
392 |
SpeechSynthesis(transcription.text)
|
393 |
-
|
394 |
filename = generate_filename(transcription.text, "wav")
|
395 |
create_and_save_file(audio_input, "wav", transcription.text, True)
|
396 |
|
@@ -412,7 +409,7 @@ def process_video(video_path, seconds_per_frame=1):
|
|
412 |
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
413 |
fps = video.get(cv2.CAP_PROP_FPS)
|
414 |
frames_to_skip = int(fps * seconds_per_frame)
|
415 |
-
|
416 |
for frame_idx in range(0, total_frames, frames_to_skip):
|
417 |
video.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
|
418 |
success, frame = video.read()
|
@@ -420,9 +417,9 @@ def process_video(video_path, seconds_per_frame=1):
|
|
420 |
break
|
421 |
_, buffer = cv2.imencode(".jpg", frame)
|
422 |
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
423 |
-
|
424 |
video.release()
|
425 |
-
|
426 |
# Extract audio
|
427 |
base_video_path = os.path.splitext(video_path)[0]
|
428 |
audio_path = f"{base_video_path}.mp3"
|
@@ -433,14 +430,14 @@ def process_video(video_path, seconds_per_frame=1):
|
|
433 |
except:
|
434 |
st.warning("No audio track found in video")
|
435 |
audio_path = None
|
436 |
-
|
437 |
return base64Frames, audio_path
|
438 |
|
439 |
def process_video_with_gpt(video_input, user_prompt):
|
440 |
-
"""Process video with GPT-
|
441 |
base64Frames, audio_path = process_video(video_input)
|
442 |
-
|
443 |
-
response =
|
444 |
model=st.session_state["openai_model"],
|
445 |
messages=[
|
446 |
{"role": "system", "content": "Analyze the video frames and provide a detailed description."},
|
@@ -451,15 +448,15 @@ def process_video_with_gpt(video_input, user_prompt):
|
|
451 |
]}
|
452 |
]
|
453 |
)
|
454 |
-
|
455 |
return response.choices[0].message.content
|
456 |
|
457 |
def create_media_gallery():
|
458 |
"""Create the media gallery interface."""
|
459 |
st.header("π¬ Media Gallery")
|
460 |
-
|
461 |
tabs = st.tabs(["πΌοΈ Images", "π΅ Audio", "π₯ Video", "π¨ Scene Generator"])
|
462 |
-
|
463 |
with tabs[0]: # Images
|
464 |
image_files = glob.glob("*.png") + glob.glob("*.jpg")
|
465 |
if image_files:
|
@@ -469,13 +466,13 @@ def create_media_gallery():
|
|
469 |
with cols[idx % num_cols]:
|
470 |
img = Image.open(image_file)
|
471 |
st.image(img, use_column_width=True)
|
472 |
-
|
473 |
if st.button(f"Analyze {os.path.basename(image_file)}"):
|
474 |
-
analysis = process_image(image_file,
|
475 |
"Describe this image in detail and identify key elements.")
|
476 |
st.markdown(analysis)
|
477 |
SpeechSynthesis(analysis)
|
478 |
-
|
479 |
with tabs[1]: # Audio
|
480 |
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
|
481 |
for audio_file in audio_files:
|
@@ -486,23 +483,23 @@ def create_media_gallery():
|
|
486 |
transcription = process_audio(f)
|
487 |
st.write(transcription)
|
488 |
SpeechSynthesis(transcription)
|
489 |
-
|
490 |
with tabs[2]: # Video
|
491 |
video_files = glob.glob("*.mp4")
|
492 |
for video_file in video_files:
|
493 |
with st.expander(f"π₯ {os.path.basename(video_file)}"):
|
494 |
st.markdown(get_media_html(video_file, "video"), unsafe_allow_html=True)
|
495 |
if st.button(f"Analyze {os.path.basename(video_file)}"):
|
496 |
-
analysis = process_video_with_gpt(video_file,
|
497 |
"Describe what's happening in this video.")
|
498 |
st.markdown(analysis)
|
499 |
SpeechSynthesis(analysis)
|
500 |
-
|
501 |
with tabs[3]: # Scene Generator
|
502 |
for collection_name, bikes in bike_collections.items():
|
503 |
st.subheader(collection_name)
|
504 |
cols = st.columns(len(bikes))
|
505 |
-
|
506 |
for idx, (bike_name, details) in enumerate(bikes.items()):
|
507 |
with cols[idx]:
|
508 |
st.markdown(f"""
|
@@ -511,7 +508,7 @@ def create_media_gallery():
|
|
511 |
<p>{details['prompt']}</p>
|
512 |
</div>
|
513 |
""", unsafe_allow_html=True)
|
514 |
-
|
515 |
if st.button(f"Generate {bike_name} Scene"):
|
516 |
prompt = details['prompt']
|
517 |
st.write(f"Generated scene description for {bike_name}:")
|
@@ -519,16 +516,16 @@ def create_media_gallery():
|
|
519 |
SpeechSynthesis(prompt)
|
520 |
|
521 |
# Chat Processing Functions
|
522 |
-
def process_with_gpt(text_input):
|
523 |
-
"""Process text with GPT-
|
524 |
if text_input:
|
525 |
st.session_state.messages.append({"role": "user", "content": text_input})
|
526 |
-
|
527 |
with st.chat_message("user"):
|
528 |
st.markdown(text_input)
|
529 |
-
|
530 |
with st.chat_message("assistant"):
|
531 |
-
completion =
|
532 |
model=st.session_state["openai_model"],
|
533 |
messages=[
|
534 |
{"role": m["role"], "content": m["content"]}
|
@@ -537,29 +534,27 @@ def process_with_gpt(text_input):
|
|
537 |
stream=False
|
538 |
)
|
539 |
return_text = completion.choices[0].message.content
|
540 |
-
st.write("GPT-
|
541 |
-
|
542 |
filename = generate_filename(text_input, "md")
|
543 |
-
create_file(filename, text_input, return_text)
|
544 |
st.session_state.messages.append({"role": "assistant", "content": return_text})
|
545 |
return return_text
|
546 |
|
547 |
-
def process_with_claude(text_input):
|
548 |
"""Process text with Claude."""
|
549 |
if text_input:
|
550 |
-
response = claude_client.
|
551 |
-
model="claude-
|
552 |
-
|
553 |
-
|
554 |
-
{"role": "user", "content": text_input}
|
555 |
-
]
|
556 |
)
|
557 |
-
response_text = response.
|
558 |
st.write("Claude: " + response_text)
|
559 |
-
|
560 |
filename = generate_filename(text_input, "md")
|
561 |
-
create_file(filename, text_input, response_text)
|
562 |
-
|
563 |
st.session_state.chat_history.append({
|
564 |
"user": text_input,
|
565 |
"claude": response_text
|
@@ -569,7 +564,7 @@ def process_with_claude(text_input):
|
|
569 |
def display_file_manager():
|
570 |
"""Display file management sidebar."""
|
571 |
st.sidebar.title("π File Management")
|
572 |
-
|
573 |
all_files = glob.glob("*.md")
|
574 |
all_files.sort(reverse=True)
|
575 |
|
@@ -586,95 +581,106 @@ def display_file_manager():
|
|
586 |
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
587 |
with col1:
|
588 |
if st.button("π", key="view_"+file):
|
589 |
-
st.session_state.
|
590 |
st.session_state.file_content = load_file(file)
|
591 |
SpeechSynthesis(st.session_state.file_content)
|
592 |
with col2:
|
593 |
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
594 |
with col3:
|
595 |
if st.button("π", key="edit_"+file):
|
596 |
-
st.session_state.
|
597 |
st.session_state.file_content = load_file(file)
|
598 |
with col4:
|
599 |
if st.button("π", key="delete_"+file):
|
600 |
os.remove(file)
|
601 |
st.rerun()
|
602 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
603 |
def main():
|
604 |
-
st.title("
|
605 |
-
|
606 |
-
# Main navigation
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
if tab_main == "π¬ Chat":
|
612 |
-
# Model Selection
|
613 |
-
model_choice = st.sidebar.radio(
|
614 |
-
"Choose AI Model:",
|
615 |
-
["GPT-4o", "Claude-3", "Both"]
|
616 |
-
)
|
617 |
-
|
618 |
-
# Chat Interface
|
619 |
user_input = st.text_area("Message:", height=100)
|
620 |
-
|
621 |
if st.button("Send π¨"):
|
622 |
if user_input:
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
elif model_choice == "Claude-3":
|
627 |
-
claude_response = process_with_claude(user_input)
|
628 |
-
SpeechSynthesis(claude_response)
|
629 |
-
else: # Both
|
630 |
-
col1, col2 = st.columns(2)
|
631 |
-
with col1:
|
632 |
-
st.subheader("GPT-4o Response")
|
633 |
-
gpt_response = process_with_gpt(user_input)
|
634 |
-
SpeechSynthesis(gpt_response)
|
635 |
-
with col2:
|
636 |
-
st.subheader("Claude-3 Response")
|
637 |
-
claude_response = process_with_claude(user_input)
|
638 |
-
SpeechSynthesis(claude_response)
|
639 |
-
|
640 |
# Display Chat History
|
641 |
st.subheader("Chat History π")
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
|
|
|
|
658 |
elif tab_main == "π Search ArXiv":
|
659 |
-
|
|
|
|
|
660 |
if query:
|
661 |
with st.spinner("Searching ArXiv..."):
|
662 |
results = search_arxiv(query)
|
663 |
-
|
|
|
|
|
|
|
|
|
664 |
SpeechSynthesis(results)
|
665 |
-
|
|
|
|
|
|
|
666 |
elif tab_main == "π File Editor":
|
667 |
-
if
|
668 |
-
st.subheader(f"Editing: {st.session_state.
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
file.write(new_content)
|
673 |
-
st.success("File updated successfully!")
|
674 |
-
SpeechSynthesis("File updated successfully!")
|
675 |
|
676 |
# Always show file manager in sidebar
|
677 |
display_file_manager()
|
678 |
|
679 |
if __name__ == "__main__":
|
680 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
import anthropic
|
3 |
+
import openai
|
4 |
import base64
|
5 |
from datetime import datetime
|
6 |
import plotly.graph_objects as go
|
|
|
32 |
from openai import OpenAI
|
33 |
|
34 |
# Configuration and Setup
|
35 |
+
Site_Name = 'π€π§ Combined AI Appππ¬'
|
36 |
+
title = "π€π§ Combined AI Appππ¬"
|
37 |
helpURL = 'https://huggingface.co/awacke1'
|
38 |
bugURL = 'https://huggingface.co/spaces/awacke1'
|
39 |
icons = 'π€π§ π¬π'
|
|
|
69 |
anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
|
70 |
claude_client = anthropic.Anthropic(api_key=anthropic_key)
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
# Initialize session states
|
73 |
if "chat_history" not in st.session_state:
|
74 |
st.session_state.chat_history = []
|
75 |
if "openai_model" not in st.session_state:
|
76 |
+
st.session_state["openai_model"] = "gpt-4"
|
77 |
if "messages" not in st.session_state:
|
78 |
st.session_state.messages = []
|
79 |
if "search_queries" not in st.session_state:
|
80 |
st.session_state.search_queries = []
|
81 |
+
if 'selected_file' not in st.session_state:
|
82 |
+
st.session_state.selected_file = None
|
83 |
+
if 'view_mode' not in st.session_state:
|
84 |
+
st.session_state.view_mode = 'view'
|
85 |
+
if 'files' not in st.session_state:
|
86 |
+
st.session_state.files = []
|
87 |
|
88 |
# Custom CSS
|
89 |
st.markdown("""
|
|
|
178 |
}
|
179 |
|
180 |
# File Operations Functions
|
181 |
+
def create_file(filename, prompt, response, is_image=False, should_save=True):
|
182 |
"""Basic file creation with prompt and response."""
|
183 |
+
if not should_save:
|
184 |
+
return None
|
185 |
with open(filename, "w", encoding="utf-8") as f:
|
186 |
f.write(prompt + "\n\n" + response)
|
187 |
|
|
|
271 |
'''
|
272 |
components.html(documentHTML5, width=1280, height=300)
|
273 |
|
274 |
+
# ArXiv Search Functions (Combined into one function)
|
275 |
+
def search_arxiv(query, should_save=True):
|
276 |
"""Search ArXiv papers using Hugging Face client."""
|
277 |
+
st.write("Performing AI Lookup...")
|
278 |
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
279 |
+
start_time = time.strftime("%Y-%m-%d %H:%M:%S")
|
280 |
+
|
281 |
# First query - Get papers
|
282 |
response1 = client.predict(
|
283 |
query,
|
|
|
286 |
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
287 |
api_name="/update_with_rag_md"
|
288 |
)
|
289 |
+
|
290 |
# Second query - Get summary
|
291 |
response2 = client.predict(
|
292 |
query,
|
|
|
294 |
True,
|
295 |
api_name="/ask_llm"
|
296 |
)
|
297 |
+
|
298 |
Question = '### π ' + query + '\r\n'
|
299 |
References = response1[0]
|
|
|
300 |
ReferenceLinks = extract_urls(References)
|
301 |
+
|
|
|
|
|
|
|
302 |
results = Question + '\r\n' + response2 + '\r\n' + References + '\r\n' + ReferenceLinks
|
303 |
+
|
304 |
+
st.markdown(results)
|
305 |
+
SpeechSynthesis(results)
|
306 |
+
|
307 |
end_time = time.strftime("%Y-%m-%d %H:%M:%S")
|
308 |
start_timestamp = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))
|
309 |
end_timestamp = time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S"))
|
310 |
elapsed_seconds = end_timestamp - start_timestamp
|
311 |
+
|
312 |
st.write(f"Start time: {start_time}")
|
313 |
st.write(f"Finish time: {end_time}")
|
314 |
st.write(f"Elapsed time: {elapsed_seconds:.2f} seconds")
|
315 |
+
|
316 |
+
filename = generate_filename(query, "md")
|
317 |
+
create_file(filename, query, results, should_save=should_save)
|
318 |
return results
|
319 |
|
320 |
def extract_urls(text):
|
|
|
324 |
abs_link_pattern = re.compile(r'\[(.*?)\]\((https://arxiv\.org/abs/\d+\.\d+)\)')
|
325 |
pdf_link_pattern = re.compile(r'\[β¬οΈ\]\((https://arxiv\.org/pdf/\d+\.\d+)\)')
|
326 |
title_pattern = re.compile(r'### \d{2} \w{3} \d{4} \| \[(.*?)\]')
|
327 |
+
|
328 |
date_matches = date_pattern.findall(text)
|
329 |
abs_link_matches = abs_link_pattern.findall(text)
|
330 |
pdf_link_matches = pdf_link_pattern.findall(text)
|
|
|
348 |
|
349 |
# Media Processing Functions
|
350 |
def process_image(image_input, user_prompt):
|
351 |
+
"""Process image with GPT-4 vision."""
|
352 |
if isinstance(image_input, str):
|
353 |
with open(image_input, "rb") as image_file:
|
354 |
image_input = image_file.read()
|
|
|
|
|
|
|
355 |
|
356 |
+
base64_image = base64.b64encode(image_input).decode("utf-8")
|
357 |
|
358 |
+
response = openai.ChatCompletion.create(
|
359 |
model=st.session_state["openai_model"],
|
360 |
messages=[
|
361 |
{"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
|
|
|
368 |
],
|
369 |
temperature=0.0,
|
370 |
)
|
371 |
+
|
372 |
return response.choices[0].message.content
|
373 |
|
374 |
def process_audio(audio_input, text_input=''):
|
|
|
377 |
with open(audio_input, "rb") as file:
|
378 |
audio_input = file.read()
|
379 |
|
380 |
+
transcription = openai.Audio.transcribe(
|
381 |
model="whisper-1",
|
382 |
file=audio_input,
|
383 |
)
|
384 |
+
|
385 |
st.session_state.messages.append({"role": "user", "content": transcription.text})
|
386 |
+
|
387 |
with st.chat_message("assistant"):
|
388 |
st.markdown(transcription.text)
|
389 |
SpeechSynthesis(transcription.text)
|
390 |
+
|
391 |
filename = generate_filename(transcription.text, "wav")
|
392 |
create_and_save_file(audio_input, "wav", transcription.text, True)
|
393 |
|
|
|
409 |
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
410 |
fps = video.get(cv2.CAP_PROP_FPS)
|
411 |
frames_to_skip = int(fps * seconds_per_frame)
|
412 |
+
|
413 |
for frame_idx in range(0, total_frames, frames_to_skip):
|
414 |
video.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
|
415 |
success, frame = video.read()
|
|
|
417 |
break
|
418 |
_, buffer = cv2.imencode(".jpg", frame)
|
419 |
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
420 |
+
|
421 |
video.release()
|
422 |
+
|
423 |
# Extract audio
|
424 |
base_video_path = os.path.splitext(video_path)[0]
|
425 |
audio_path = f"{base_video_path}.mp3"
|
|
|
430 |
except:
|
431 |
st.warning("No audio track found in video")
|
432 |
audio_path = None
|
433 |
+
|
434 |
return base64Frames, audio_path
|
435 |
|
436 |
def process_video_with_gpt(video_input, user_prompt):
|
437 |
+
"""Process video with GPT-4 vision."""
|
438 |
base64Frames, audio_path = process_video(video_input)
|
439 |
+
|
440 |
+
response = openai.ChatCompletion.create(
|
441 |
model=st.session_state["openai_model"],
|
442 |
messages=[
|
443 |
{"role": "system", "content": "Analyze the video frames and provide a detailed description."},
|
|
|
448 |
]}
|
449 |
]
|
450 |
)
|
451 |
+
|
452 |
return response.choices[0].message.content
|
453 |
|
454 |
def create_media_gallery():
|
455 |
"""Create the media gallery interface."""
|
456 |
st.header("π¬ Media Gallery")
|
457 |
+
|
458 |
tabs = st.tabs(["πΌοΈ Images", "π΅ Audio", "π₯ Video", "π¨ Scene Generator"])
|
459 |
+
|
460 |
with tabs[0]: # Images
|
461 |
image_files = glob.glob("*.png") + glob.glob("*.jpg")
|
462 |
if image_files:
|
|
|
466 |
with cols[idx % num_cols]:
|
467 |
img = Image.open(image_file)
|
468 |
st.image(img, use_column_width=True)
|
469 |
+
|
470 |
if st.button(f"Analyze {os.path.basename(image_file)}"):
|
471 |
+
analysis = process_image(image_file,
|
472 |
"Describe this image in detail and identify key elements.")
|
473 |
st.markdown(analysis)
|
474 |
SpeechSynthesis(analysis)
|
475 |
+
|
476 |
with tabs[1]: # Audio
|
477 |
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
|
478 |
for audio_file in audio_files:
|
|
|
483 |
transcription = process_audio(f)
|
484 |
st.write(transcription)
|
485 |
SpeechSynthesis(transcription)
|
486 |
+
|
487 |
with tabs[2]: # Video
|
488 |
video_files = glob.glob("*.mp4")
|
489 |
for video_file in video_files:
|
490 |
with st.expander(f"π₯ {os.path.basename(video_file)}"):
|
491 |
st.markdown(get_media_html(video_file, "video"), unsafe_allow_html=True)
|
492 |
if st.button(f"Analyze {os.path.basename(video_file)}"):
|
493 |
+
analysis = process_video_with_gpt(video_file,
|
494 |
"Describe what's happening in this video.")
|
495 |
st.markdown(analysis)
|
496 |
SpeechSynthesis(analysis)
|
497 |
+
|
498 |
with tabs[3]: # Scene Generator
|
499 |
for collection_name, bikes in bike_collections.items():
|
500 |
st.subheader(collection_name)
|
501 |
cols = st.columns(len(bikes))
|
502 |
+
|
503 |
for idx, (bike_name, details) in enumerate(bikes.items()):
|
504 |
with cols[idx]:
|
505 |
st.markdown(f"""
|
|
|
508 |
<p>{details['prompt']}</p>
|
509 |
</div>
|
510 |
""", unsafe_allow_html=True)
|
511 |
+
|
512 |
if st.button(f"Generate {bike_name} Scene"):
|
513 |
prompt = details['prompt']
|
514 |
st.write(f"Generated scene description for {bike_name}:")
|
|
|
516 |
SpeechSynthesis(prompt)
|
517 |
|
518 |
# Chat Processing Functions
|
519 |
+
def process_with_gpt(text_input, should_save=True):
|
520 |
+
"""Process text with GPT-4."""
|
521 |
if text_input:
|
522 |
st.session_state.messages.append({"role": "user", "content": text_input})
|
523 |
+
|
524 |
with st.chat_message("user"):
|
525 |
st.markdown(text_input)
|
526 |
+
|
527 |
with st.chat_message("assistant"):
|
528 |
+
completion = openai.ChatCompletion.create(
|
529 |
model=st.session_state["openai_model"],
|
530 |
messages=[
|
531 |
{"role": m["role"], "content": m["content"]}
|
|
|
534 |
stream=False
|
535 |
)
|
536 |
return_text = completion.choices[0].message.content
|
537 |
+
st.write("GPT-4: " + return_text)
|
538 |
+
|
539 |
filename = generate_filename(text_input, "md")
|
540 |
+
create_file(filename, text_input, return_text, should_save=should_save)
|
541 |
st.session_state.messages.append({"role": "assistant", "content": return_text})
|
542 |
return return_text
|
543 |
|
544 |
+
def process_with_claude(text_input, should_save=True):
|
545 |
"""Process text with Claude."""
|
546 |
if text_input:
|
547 |
+
response = claude_client.completions.create(
|
548 |
+
model="claude-2",
|
549 |
+
max_tokens_to_sample=1000,
|
550 |
+
prompt=text_input
|
|
|
|
|
551 |
)
|
552 |
+
response_text = response.completion
|
553 |
st.write("Claude: " + response_text)
|
554 |
+
|
555 |
filename = generate_filename(text_input, "md")
|
556 |
+
create_file(filename, text_input, response_text, should_save=should_save)
|
557 |
+
|
558 |
st.session_state.chat_history.append({
|
559 |
"user": text_input,
|
560 |
"claude": response_text
|
|
|
564 |
def display_file_manager():
|
565 |
"""Display file management sidebar."""
|
566 |
st.sidebar.title("π File Management")
|
567 |
+
|
568 |
all_files = glob.glob("*.md")
|
569 |
all_files.sort(reverse=True)
|
570 |
|
|
|
581 |
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
582 |
with col1:
|
583 |
if st.button("π", key="view_"+file):
|
584 |
+
st.session_state.selected_file = file
|
585 |
st.session_state.file_content = load_file(file)
|
586 |
SpeechSynthesis(st.session_state.file_content)
|
587 |
with col2:
|
588 |
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
589 |
with col3:
|
590 |
if st.button("π", key="edit_"+file):
|
591 |
+
st.session_state.selected_file = file
|
592 |
st.session_state.file_content = load_file(file)
|
593 |
with col4:
|
594 |
if st.button("π", key="delete_"+file):
|
595 |
os.remove(file)
|
596 |
st.rerun()
|
597 |
|
598 |
+
def display_file_content(file_path):
|
599 |
+
"""Display file content with editing capabilities."""
|
600 |
+
try:
|
601 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
602 |
+
content = f.read()
|
603 |
+
|
604 |
+
if st.session_state.view_mode == 'view':
|
605 |
+
st.markdown(content)
|
606 |
+
else:
|
607 |
+
edited_content = st.text_area(
|
608 |
+
"Edit content",
|
609 |
+
content,
|
610 |
+
height=400,
|
611 |
+
key=f"edit_{os.path.basename(file_path)}"
|
612 |
+
)
|
613 |
+
|
614 |
+
if st.button("Save Changes", key=f"save_{os.path.basename(file_path)}"):
|
615 |
+
try:
|
616 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
617 |
+
f.write(edited_content)
|
618 |
+
st.success(f"Successfully saved changes to {file_path}")
|
619 |
+
except Exception as e:
|
620 |
+
st.error(f"Error saving changes: {e}")
|
621 |
+
except Exception as e:
|
622 |
+
st.error(f"Error reading file: {e}")
|
623 |
+
|
624 |
def main():
|
625 |
+
st.title("π Combined AI Assistant App")
|
626 |
+
|
627 |
+
# Main navigation with radio buttons
|
628 |
+
ai_options = ["π¬ Chat with GPT-4", "π¬ Chat with Claude", "π Search ArXiv", "πΈ Media Gallery", "π File Editor"]
|
629 |
+
tab_main = st.radio("Choose Action:", ai_options, horizontal=True)
|
630 |
+
|
631 |
+
if tab_main == "π¬ Chat with GPT-4":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
632 |
user_input = st.text_area("Message:", height=100)
|
|
|
633 |
if st.button("Send π¨"):
|
634 |
if user_input:
|
635 |
+
gpt_response = process_with_gpt(user_input)
|
636 |
+
SpeechSynthesis(gpt_response)
|
637 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
638 |
# Display Chat History
|
639 |
st.subheader("Chat History π")
|
640 |
+
for message in st.session_state.messages:
|
641 |
+
with st.chat_message(message["role"]):
|
642 |
+
st.markdown(message["content"])
|
643 |
+
|
644 |
+
elif tab_main == "π¬ Chat with Claude":
|
645 |
+
user_input = st.text_area("Message:", height=100)
|
646 |
+
if st.button("Send π¨"):
|
647 |
+
if user_input:
|
648 |
+
claude_response = process_with_claude(user_input)
|
649 |
+
SpeechSynthesis(claude_response)
|
650 |
+
|
651 |
+
# Display Chat History
|
652 |
+
st.subheader("Chat History π")
|
653 |
+
for chat in st.session_state.chat_history:
|
654 |
+
st.text_area("You:", chat["user"], height=100, disabled=True)
|
655 |
+
st.text_area("Claude:", chat["claude"], height=200, disabled=True)
|
656 |
+
st.markdown("---")
|
657 |
+
|
658 |
elif tab_main == "π Search ArXiv":
|
659 |
+
query_params = st.experimental_get_query_params()
|
660 |
+
query = query_params.get('q', [''])[0]
|
661 |
+
query = st.text_input("Enter your research query:", value=query)
|
662 |
if query:
|
663 |
with st.spinner("Searching ArXiv..."):
|
664 |
results = search_arxiv(query)
|
665 |
+
# Save the query and results
|
666 |
+
filename = generate_filename(query, "md")
|
667 |
+
create_file(filename, query, results)
|
668 |
+
st.session_state.selected_file = filename
|
669 |
+
st.session_state.file_content = results
|
670 |
SpeechSynthesis(results)
|
671 |
+
|
672 |
+
elif tab_main == "πΈ Media Gallery":
|
673 |
+
create_media_gallery()
|
674 |
+
|
675 |
elif tab_main == "π File Editor":
|
676 |
+
if st.session_state.selected_file:
|
677 |
+
st.subheader(f"Editing: {st.session_state.selected_file}")
|
678 |
+
display_file_content(st.session_state.selected_file)
|
679 |
+
else:
|
680 |
+
st.write("No file selected.")
|
|
|
|
|
|
|
681 |
|
682 |
# Always show file manager in sidebar
|
683 |
display_file_manager()
|
684 |
|
685 |
if __name__ == "__main__":
|
686 |
+
main()
|