Update app.py
Browse files
app.py
CHANGED
|
@@ -53,6 +53,24 @@ st.set_page_config(
|
|
| 53 |
}
|
| 54 |
)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
client = OpenAI(api_key= os.getenv('OPENAI_API_KEY'), organization=os.getenv('OPENAI_ORG_ID'))
|
| 57 |
MODEL = "gpt-4o-2024-05-13"
|
| 58 |
if "openai_model" not in st.session_state:
|
|
@@ -94,6 +112,269 @@ def SpeechSynthesis(result):
|
|
| 94 |
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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629 |
filesearch = PromptPrefix + file_contents
|
| 630 |
st.markdown(filesearch)
|
| 631 |
-
search_glossary(filesearch)
|
|
|
|
|
|
|
| 632 |
|
| 633 |
if next_action=='md':
|
| 634 |
st.markdown(file_contents)
|
|
@@ -869,32 +1152,16 @@ def display_buttons_with_scores(num_columns_text):
|
|
| 869 |
key = f"{category}_{game}_{term}".replace(' ', '_').lower()
|
| 870 |
score = load_score(key)
|
| 871 |
if st.button(f"{game_emoji} {category} {game} {term} {score}", key=key):
|
| 872 |
-
newscore = update_score(key.replace('?',''))
|
| 873 |
-
query_prefix = f"{category_emoji} {game_emoji} ** {category} - {game} - {term} - **"
|
| 874 |
-
st.markdown("Scored " + query_prefix + ' with score ' + str(newscore) + '.')
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
def get_all_query_params(key):
|
| 878 |
-
return st.query_params().get(key, [])
|
| 879 |
-
|
| 880 |
-
def clear_query_params():
|
| 881 |
-
st.query_params()
|
| 882 |
-
|
| 883 |
-
# My Inference API Copy
|
| 884 |
-
API_URL = 'https://qe55p8afio98s0u3.us-east-1.aws.endpoints.huggingface.cloud' # Dr Llama
|
| 885 |
-
# Meta's Original - Chat HF Free Version:
|
| 886 |
-
#API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
| 887 |
-
API_KEY = os.getenv('API_KEY')
|
| 888 |
-
MODEL1="meta-llama/Llama-2-7b-chat-hf"
|
| 889 |
-
MODEL1URL="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf"
|
| 890 |
-
HF_KEY = os.getenv('HF_KEY')
|
| 891 |
-
headers = {
|
| 892 |
-
"Authorization": f"Bearer {HF_KEY}",
|
| 893 |
-
"Content-Type": "application/json"
|
| 894 |
-
}
|
| 895 |
-
key = os.getenv('OPENAI_API_KEY')
|
| 896 |
-
prompt = "...."
|
| 897 |
-
should_save = st.sidebar.checkbox("💾 Save", value=True, help="Save your session data.")
|
| 898 |
|
| 899 |
|
| 900 |
|
|
@@ -951,15 +1218,6 @@ def query(payload):
|
|
| 951 |
def get_output(prompt):
|
| 952 |
return query({"inputs": prompt})
|
| 953 |
|
| 954 |
-
# 5. Auto name generated output files from time and content
|
| 955 |
-
def generate_filename(prompt, file_type):
|
| 956 |
-
central = pytz.timezone('US/Central')
|
| 957 |
-
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
| 958 |
-
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
| 959 |
-
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:255] # 255 is linux max, 260 is windows max
|
| 960 |
-
#safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:45]
|
| 961 |
-
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
| 962 |
-
|
| 963 |
# 6. Speech transcription via OpenAI service
|
| 964 |
def transcribe_audio(openai_key, file_path, model):
|
| 965 |
openai.api_key = openai_key
|
|
@@ -1444,244 +1702,6 @@ if AddAFileForContext:
|
|
| 1444 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
| 1445 |
|
| 1446 |
|
| 1447 |
-
# GPT4o documentation
|
| 1448 |
-
# 1. Cookbook: https://cookbook.openai.com/examples/gpt4o/introduction_to_gpt4o
|
| 1449 |
-
# 2. Configure your Project and Orgs to limit/allow Models: https://platform.openai.com/settings/organization/general
|
| 1450 |
-
# 3. Watch your Billing! https://platform.openai.com/settings/organization/billing/overview
|
| 1451 |
-
|
| 1452 |
-
|
| 1453 |
-
# Set API key and organization ID from environment variables
|
| 1454 |
-
|
| 1455 |
-
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 1456 |
-
openai.organization = os.getenv('OPENAI_ORG_ID')
|
| 1457 |
-
client = OpenAI(api_key= os.getenv('OPENAI_API_KEY'), organization=os.getenv('OPENAI_ORG_ID'))
|
| 1458 |
-
|
| 1459 |
-
# Define the model to be used
|
| 1460 |
-
#MODEL = "gpt-4o"
|
| 1461 |
-
MODEL = "gpt-4o-2024-05-13"
|
| 1462 |
-
|
| 1463 |
-
def process_text(text_input):
|
| 1464 |
-
if text_input:
|
| 1465 |
-
|
| 1466 |
-
st.session_state.messages.append({"role": "user", "content": text_input})
|
| 1467 |
-
|
| 1468 |
-
with st.chat_message("user"):
|
| 1469 |
-
st.markdown(text_input)
|
| 1470 |
-
|
| 1471 |
-
with st.chat_message("assistant"):
|
| 1472 |
-
completion = client.chat.completions.create(
|
| 1473 |
-
model=MODEL,
|
| 1474 |
-
messages=[
|
| 1475 |
-
{"role": m["role"], "content": m["content"]}
|
| 1476 |
-
for m in st.session_state.messages
|
| 1477 |
-
],
|
| 1478 |
-
stream=False
|
| 1479 |
-
)
|
| 1480 |
-
return_text = completion.choices[0].message.content
|
| 1481 |
-
st.write("Assistant: " + return_text)
|
| 1482 |
-
filename = generate_filename(text_input, "md")
|
| 1483 |
-
create_file(filename, text_input, return_text, should_save)
|
| 1484 |
-
st.session_state.messages.append({"role": "assistant", "content": return_text})
|
| 1485 |
-
|
| 1486 |
-
#st.write("Assistant: " + completion.choices[0].message.content)
|
| 1487 |
-
|
| 1488 |
-
def create_file(filename, prompt, response, is_image=False):
|
| 1489 |
-
with open(filename, "w", encoding="utf-8") as f:
|
| 1490 |
-
f.write(prompt + "\n\n" + response)
|
| 1491 |
-
|
| 1492 |
-
def save_image_old2(image, filename):
|
| 1493 |
-
with open(filename, "wb") as f:
|
| 1494 |
-
f.write(image.getbuffer())
|
| 1495 |
-
|
| 1496 |
-
# Now filename length protected for linux and windows filename lengths
|
| 1497 |
-
def save_image(image, filename):
|
| 1498 |
-
max_filename_length = 250
|
| 1499 |
-
filename_stem, extension = os.path.splitext(filename)
|
| 1500 |
-
truncated_stem = filename_stem[:max_filename_length - len(extension)] if len(filename) > max_filename_length else filename_stem
|
| 1501 |
-
filename = f"{truncated_stem}{extension}"
|
| 1502 |
-
with open(filename, "wb") as f:
|
| 1503 |
-
f.write(image.getbuffer())
|
| 1504 |
-
return filename
|
| 1505 |
-
|
| 1506 |
-
def extract_boldface_terms(text):
|
| 1507 |
-
return re.findall(r'\*\*(.*?)\*\*', text)
|
| 1508 |
-
|
| 1509 |
-
def extract_title(text):
|
| 1510 |
-
boldface_terms = re.findall(r'\*\*(.*?)\*\*', text)
|
| 1511 |
-
if boldface_terms:
|
| 1512 |
-
title = ' '.join(boldface_terms)
|
| 1513 |
-
else:
|
| 1514 |
-
title = re.sub(r'[^a-zA-Z0-9_\-]', ' ', text[-200:])
|
| 1515 |
-
return title[-200:]
|
| 1516 |
-
|
| 1517 |
-
def process_image(image_input, user_prompt):
|
| 1518 |
-
if image_input:
|
| 1519 |
-
st.markdown('Processing image: ' + image_input.name )
|
| 1520 |
-
if image_input:
|
| 1521 |
-
base64_image = base64.b64encode(image_input.read()).decode("utf-8")
|
| 1522 |
-
response = client.chat.completions.create(
|
| 1523 |
-
model=MODEL,
|
| 1524 |
-
messages=[
|
| 1525 |
-
{"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
|
| 1526 |
-
{"role": "user", "content": [
|
| 1527 |
-
{"type": "text", "text": user_prompt},
|
| 1528 |
-
{"type": "image_url", "image_url": {
|
| 1529 |
-
"url": f"data:image/png;base64,{base64_image}"}
|
| 1530 |
-
}
|
| 1531 |
-
]}
|
| 1532 |
-
],
|
| 1533 |
-
temperature=0.0,
|
| 1534 |
-
)
|
| 1535 |
-
image_response = response.choices[0].message.content
|
| 1536 |
-
st.markdown(image_response)
|
| 1537 |
-
|
| 1538 |
-
# Save markdown on image AI output from gpt4o
|
| 1539 |
-
filename_md = generate_filename(image_input.name + '- ' + image_response, "md")
|
| 1540 |
-
# Save markdown on image AI output from gpt4o
|
| 1541 |
-
filename_png = filename_md.replace('.md', '.' + image_input.name.split('.')[-1])
|
| 1542 |
-
|
| 1543 |
-
create_file(filename_md, image_response, '', True) #create_file() # create_file() 3 required positional arguments: 'filename', 'prompt', and 'response'
|
| 1544 |
-
|
| 1545 |
-
with open(filename_md, "w", encoding="utf-8") as f:
|
| 1546 |
-
f.write(image_response)
|
| 1547 |
-
|
| 1548 |
-
# Extract boldface terms from image_response then autoname save file
|
| 1549 |
-
#boldface_terms = extract_boldface_terms(image_response)
|
| 1550 |
-
boldface_terms = extract_title(image_response).replace(':','')
|
| 1551 |
-
filename_stem, extension = os.path.splitext(image_input.name)
|
| 1552 |
-
filename_img = f"{filename_stem} {''.join(boldface_terms)}{extension}"
|
| 1553 |
-
newfilename = save_image(image_input, filename_img)
|
| 1554 |
-
filename_md = newfilename.replace('.png', '.md')
|
| 1555 |
-
create_file(filename_md, '', image_response, True)
|
| 1556 |
-
|
| 1557 |
-
return image_response
|
| 1558 |
-
|
| 1559 |
-
def create_audio_file(filename, audio_data, should_save):
|
| 1560 |
-
if should_save:
|
| 1561 |
-
with open(filename, "wb") as file:
|
| 1562 |
-
file.write(audio_data.getvalue())
|
| 1563 |
-
st.success(f"Audio file saved as {filename}")
|
| 1564 |
-
else:
|
| 1565 |
-
st.warning("Audio file not saved.")
|
| 1566 |
-
|
| 1567 |
-
def process_audio(audio_input, text_input):
|
| 1568 |
-
if audio_input:
|
| 1569 |
-
transcription = client.audio.transcriptions.create(
|
| 1570 |
-
model="whisper-1",
|
| 1571 |
-
file=audio_input,
|
| 1572 |
-
)
|
| 1573 |
-
st.session_state.messages.append({"role": "user", "content": transcription.text})
|
| 1574 |
-
with st.chat_message("assistant"):
|
| 1575 |
-
st.markdown(transcription.text)
|
| 1576 |
-
|
| 1577 |
-
SpeechSynthesis(transcription.text)
|
| 1578 |
-
filename = generate_filename(transcription.text, "wav")
|
| 1579 |
-
|
| 1580 |
-
create_audio_file(filename, audio_input, should_save)
|
| 1581 |
-
|
| 1582 |
-
#SpeechSynthesis(transcription.text)
|
| 1583 |
-
|
| 1584 |
-
filename = generate_filename(transcription.text, "md")
|
| 1585 |
-
create_file(filename, transcription.text, transcription.text, should_save)
|
| 1586 |
-
#st.markdown(response.choices[0].message.content)
|
| 1587 |
-
|
| 1588 |
-
def process_audio_for_video(video_input):
|
| 1589 |
-
if video_input:
|
| 1590 |
-
try:
|
| 1591 |
-
transcription = client.audio.transcriptions.create(
|
| 1592 |
-
model="whisper-1",
|
| 1593 |
-
file=video_input,
|
| 1594 |
-
)
|
| 1595 |
-
response = client.chat.completions.create(
|
| 1596 |
-
model=MODEL,
|
| 1597 |
-
messages=[
|
| 1598 |
-
{"role": "system", "content":"""You are generating a transcript summary. Create a summary of the provided transcription. Respond in Markdown."""},
|
| 1599 |
-
{"role": "user", "content": [{"type": "text", "text": f"The audio transcription is: {transcription}"}],}
|
| 1600 |
-
],
|
| 1601 |
-
temperature=0,
|
| 1602 |
-
)
|
| 1603 |
-
st.markdown(response.choices[0].message.content)
|
| 1604 |
-
return response.choices[0].message.content
|
| 1605 |
-
except:
|
| 1606 |
-
st.write('No transcript')
|
| 1607 |
-
|
| 1608 |
-
def save_video(video_file):
|
| 1609 |
-
# Save the uploaded video file
|
| 1610 |
-
with open(video_file.name, "wb") as f:
|
| 1611 |
-
f.write(video_file.getbuffer())
|
| 1612 |
-
return video_file.name
|
| 1613 |
-
|
| 1614 |
-
def process_video(video_path, seconds_per_frame=2):
|
| 1615 |
-
base64Frames = []
|
| 1616 |
-
base_video_path, _ = os.path.splitext(video_path)
|
| 1617 |
-
video = cv2.VideoCapture(video_path)
|
| 1618 |
-
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 1619 |
-
fps = video.get(cv2.CAP_PROP_FPS)
|
| 1620 |
-
frames_to_skip = int(fps * seconds_per_frame)
|
| 1621 |
-
curr_frame = 0
|
| 1622 |
-
|
| 1623 |
-
# Loop through the video and extract frames at specified sampling rate
|
| 1624 |
-
while curr_frame < total_frames - 1:
|
| 1625 |
-
video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)
|
| 1626 |
-
success, frame = video.read()
|
| 1627 |
-
if not success:
|
| 1628 |
-
break
|
| 1629 |
-
_, buffer = cv2.imencode(".jpg", frame)
|
| 1630 |
-
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
| 1631 |
-
curr_frame += frames_to_skip
|
| 1632 |
-
|
| 1633 |
-
video.release()
|
| 1634 |
-
|
| 1635 |
-
# Extract audio from video
|
| 1636 |
-
audio_path = f"{base_video_path}.mp3"
|
| 1637 |
-
try:
|
| 1638 |
-
clip = VideoFileClip(video_path)
|
| 1639 |
-
|
| 1640 |
-
clip.audio.write_audiofile(audio_path, bitrate="32k")
|
| 1641 |
-
clip.audio.close()
|
| 1642 |
-
|
| 1643 |
-
clip.close()
|
| 1644 |
-
except:
|
| 1645 |
-
st.write('No audio track found, moving on..')
|
| 1646 |
-
|
| 1647 |
-
|
| 1648 |
-
print(f"Extracted {len(base64Frames)} frames")
|
| 1649 |
-
print(f"Extracted audio to {audio_path}")
|
| 1650 |
-
|
| 1651 |
-
return base64Frames, audio_path
|
| 1652 |
-
|
| 1653 |
-
def process_audio_and_video(video_input):
|
| 1654 |
-
if video_input is not None:
|
| 1655 |
-
# Save the uploaded video file
|
| 1656 |
-
video_path = save_video(video_input )
|
| 1657 |
-
|
| 1658 |
-
# Process the saved video
|
| 1659 |
-
base64Frames, audio_path = process_video(video_path, seconds_per_frame=1)
|
| 1660 |
-
|
| 1661 |
-
# Get the transcript for the video model call
|
| 1662 |
-
transcript = process_audio_for_video(video_input)
|
| 1663 |
-
|
| 1664 |
-
# Generate a summary with visual and audio
|
| 1665 |
-
response = client.chat.completions.create(
|
| 1666 |
-
model=MODEL,
|
| 1667 |
-
messages=[
|
| 1668 |
-
{"role": "system", "content": """You are generating a video summary. Create a summary of the provided video and its transcript. Respond in Markdown"""},
|
| 1669 |
-
{"role": "user", "content": [
|
| 1670 |
-
"These are the frames from the video.",
|
| 1671 |
-
*map(lambda x: {"type": "image_url",
|
| 1672 |
-
"image_url": {"url": f'data:image/jpg;base64,{x}', "detail": "low"}}, base64Frames),
|
| 1673 |
-
{"type": "text", "text": f"The audio transcription is: {transcript}"}
|
| 1674 |
-
]},
|
| 1675 |
-
],
|
| 1676 |
-
temperature=0,
|
| 1677 |
-
)
|
| 1678 |
-
results = response.choices[0].message.content
|
| 1679 |
-
st.markdown(results)
|
| 1680 |
-
|
| 1681 |
-
if transcript:
|
| 1682 |
-
filename = generate_filename(transcript, "md")
|
| 1683 |
-
create_file(filename, transcript, results, should_save)
|
| 1684 |
-
|
| 1685 |
|
| 1686 |
|
| 1687 |
def main():
|
|
|
|
| 53 |
}
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# My Inference API Copy
|
| 57 |
+
API_URL = 'https://qe55p8afio98s0u3.us-east-1.aws.endpoints.huggingface.cloud' # Dr Llama
|
| 58 |
+
# Meta's Original - Chat HF Free Version:
|
| 59 |
+
#API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
| 60 |
+
API_KEY = os.getenv('API_KEY')
|
| 61 |
+
MODEL1="meta-llama/Llama-2-7b-chat-hf"
|
| 62 |
+
MODEL1URL="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf"
|
| 63 |
+
HF_KEY = os.getenv('HF_KEY')
|
| 64 |
+
headers = {
|
| 65 |
+
"Authorization": f"Bearer {HF_KEY}",
|
| 66 |
+
"Content-Type": "application/json"
|
| 67 |
+
}
|
| 68 |
+
key = os.getenv('OPENAI_API_KEY')
|
| 69 |
+
prompt = "...."
|
| 70 |
+
should_save = st.sidebar.checkbox("💾 Save", value=True, help="Save your session data.")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
client = OpenAI(api_key= os.getenv('OPENAI_API_KEY'), organization=os.getenv('OPENAI_ORG_ID'))
|
| 75 |
MODEL = "gpt-4o-2024-05-13"
|
| 76 |
if "openai_model" not in st.session_state:
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# GPT4o documentation
|
| 120 |
+
# 1. Cookbook: https://cookbook.openai.com/examples/gpt4o/introduction_to_gpt4o
|
| 121 |
+
# 2. Configure your Project and Orgs to limit/allow Models: https://platform.openai.com/settings/organization/general
|
| 122 |
+
# 3. Watch your Billing! https://platform.openai.com/settings/organization/billing/overview
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# Set API key and organization ID from environment variables
|
| 126 |
+
|
| 127 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 128 |
+
openai.organization = os.getenv('OPENAI_ORG_ID')
|
| 129 |
+
client = OpenAI(api_key= os.getenv('OPENAI_API_KEY'), organization=os.getenv('OPENAI_ORG_ID'))
|
| 130 |
+
|
| 131 |
+
# Define the model to be used
|
| 132 |
+
#MODEL = "gpt-4o"
|
| 133 |
+
MODEL = "gpt-4o-2024-05-13"
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# 5. Auto name generated output files from time and content
|
| 140 |
+
def generate_filename(prompt, file_type):
|
| 141 |
+
central = pytz.timezone('US/Central')
|
| 142 |
+
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
| 143 |
+
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
| 144 |
+
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:240] # 255 is linux max, 260 is windows max
|
| 145 |
+
#safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:45]
|
| 146 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def process_text(text_input):
|
| 151 |
+
if text_input:
|
| 152 |
+
|
| 153 |
+
st.session_state.messages.append({"role": "user", "content": text_input})
|
| 154 |
+
|
| 155 |
+
with st.chat_message("user"):
|
| 156 |
+
st.markdown(text_input)
|
| 157 |
+
|
| 158 |
+
with st.chat_message("assistant"):
|
| 159 |
+
completion = client.chat.completions.create(
|
| 160 |
+
model=MODEL,
|
| 161 |
+
messages=[
|
| 162 |
+
{"role": m["role"], "content": m["content"]}
|
| 163 |
+
for m in st.session_state.messages
|
| 164 |
+
],
|
| 165 |
+
stream=False
|
| 166 |
+
)
|
| 167 |
+
return_text = completion.choices[0].message.content
|
| 168 |
+
st.write("Assistant: " + return_text)
|
| 169 |
+
filename = generate_filename(text_input, "md")
|
| 170 |
+
create_file(filename, text_input, return_text, should_save)
|
| 171 |
+
st.session_state.messages.append({"role": "assistant", "content": return_text})
|
| 172 |
+
|
| 173 |
+
#st.write("Assistant: " + completion.choices[0].message.content)
|
| 174 |
+
|
| 175 |
+
def create_file(filename, prompt, response, is_image=False):
|
| 176 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 177 |
+
f.write(prompt + "\n\n" + response)
|
| 178 |
+
|
| 179 |
+
def save_image_old2(image, filename):
|
| 180 |
+
with open(filename, "wb") as f:
|
| 181 |
+
f.write(image.getbuffer())
|
| 182 |
+
|
| 183 |
+
# Now filename length protected for linux and windows filename lengths
|
| 184 |
+
def save_image(image, filename):
|
| 185 |
+
max_filename_length = 250
|
| 186 |
+
filename_stem, extension = os.path.splitext(filename)
|
| 187 |
+
truncated_stem = filename_stem[:max_filename_length - len(extension)] if len(filename) > max_filename_length else filename_stem
|
| 188 |
+
filename = f"{truncated_stem}{extension}"
|
| 189 |
+
with open(filename, "wb") as f:
|
| 190 |
+
f.write(image.getbuffer())
|
| 191 |
+
return filename
|
| 192 |
+
|
| 193 |
+
def extract_boldface_terms(text):
|
| 194 |
+
return re.findall(r'\*\*(.*?)\*\*', text)
|
| 195 |
+
|
| 196 |
+
def extract_title(text):
|
| 197 |
+
boldface_terms = re.findall(r'\*\*(.*?)\*\*', text)
|
| 198 |
+
if boldface_terms:
|
| 199 |
+
title = ' '.join(boldface_terms)
|
| 200 |
+
else:
|
| 201 |
+
title = re.sub(r'[^a-zA-Z0-9_\-]', ' ', text[-200:])
|
| 202 |
+
return title[-200:]
|
| 203 |
+
|
| 204 |
+
def process_image(image_input, user_prompt):
|
| 205 |
+
if image_input:
|
| 206 |
+
st.markdown('Processing image: ' + image_input.name )
|
| 207 |
+
if image_input:
|
| 208 |
+
base64_image = base64.b64encode(image_input.read()).decode("utf-8")
|
| 209 |
+
response = client.chat.completions.create(
|
| 210 |
+
model=MODEL,
|
| 211 |
+
messages=[
|
| 212 |
+
{"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
|
| 213 |
+
{"role": "user", "content": [
|
| 214 |
+
{"type": "text", "text": user_prompt},
|
| 215 |
+
{"type": "image_url", "image_url": {
|
| 216 |
+
"url": f"data:image/png;base64,{base64_image}"}
|
| 217 |
+
}
|
| 218 |
+
]}
|
| 219 |
+
],
|
| 220 |
+
temperature=0.0,
|
| 221 |
+
)
|
| 222 |
+
image_response = response.choices[0].message.content
|
| 223 |
+
st.markdown(image_response)
|
| 224 |
+
|
| 225 |
+
# Save markdown on image AI output from gpt4o
|
| 226 |
+
filename_md = generate_filename(image_input.name + '- ' + image_response, "md")
|
| 227 |
+
# Save markdown on image AI output from gpt4o
|
| 228 |
+
filename_png = filename_md.replace('.md', '.' + image_input.name.split('.')[-1])
|
| 229 |
+
|
| 230 |
+
create_file(filename_md, image_response, '', True) #create_file() # create_file() 3 required positional arguments: 'filename', 'prompt', and 'response'
|
| 231 |
+
|
| 232 |
+
with open(filename_md, "w", encoding="utf-8") as f:
|
| 233 |
+
f.write(image_response)
|
| 234 |
+
|
| 235 |
+
# Extract boldface terms from image_response then autoname save file
|
| 236 |
+
#boldface_terms = extract_boldface_terms(image_response)
|
| 237 |
+
boldface_terms = extract_title(image_response).replace(':','')
|
| 238 |
+
filename_stem, extension = os.path.splitext(image_input.name)
|
| 239 |
+
filename_img = f"{filename_stem} {''.join(boldface_terms)}{extension}"
|
| 240 |
+
newfilename = save_image(image_input, filename_img)
|
| 241 |
+
filename_md = newfilename.replace('.png', '.md')
|
| 242 |
+
create_file(filename_md, '', image_response, True)
|
| 243 |
+
|
| 244 |
+
return image_response
|
| 245 |
+
|
| 246 |
+
def create_audio_file(filename, audio_data, should_save):
|
| 247 |
+
if should_save:
|
| 248 |
+
with open(filename, "wb") as file:
|
| 249 |
+
file.write(audio_data.getvalue())
|
| 250 |
+
st.success(f"Audio file saved as {filename}")
|
| 251 |
+
else:
|
| 252 |
+
st.warning("Audio file not saved.")
|
| 253 |
+
|
| 254 |
+
def process_audio(audio_input, text_input):
|
| 255 |
+
if audio_input:
|
| 256 |
+
transcription = client.audio.transcriptions.create(
|
| 257 |
+
model="whisper-1",
|
| 258 |
+
file=audio_input,
|
| 259 |
+
)
|
| 260 |
+
st.session_state.messages.append({"role": "user", "content": transcription.text})
|
| 261 |
+
with st.chat_message("assistant"):
|
| 262 |
+
st.markdown(transcription.text)
|
| 263 |
+
|
| 264 |
+
SpeechSynthesis(transcription.text)
|
| 265 |
+
filename = generate_filename(transcription.text, "wav")
|
| 266 |
+
|
| 267 |
+
create_audio_file(filename, audio_input, should_save)
|
| 268 |
+
|
| 269 |
+
#SpeechSynthesis(transcription.text)
|
| 270 |
+
|
| 271 |
+
filename = generate_filename(transcription.text, "md")
|
| 272 |
+
create_file(filename, transcription.text, transcription.text, should_save)
|
| 273 |
+
#st.markdown(response.choices[0].message.content)
|
| 274 |
+
|
| 275 |
+
def process_audio_for_video(video_input):
|
| 276 |
+
if video_input:
|
| 277 |
+
try:
|
| 278 |
+
transcription = client.audio.transcriptions.create(
|
| 279 |
+
model="whisper-1",
|
| 280 |
+
file=video_input,
|
| 281 |
+
)
|
| 282 |
+
response = client.chat.completions.create(
|
| 283 |
+
model=MODEL,
|
| 284 |
+
messages=[
|
| 285 |
+
{"role": "system", "content":"""You are generating a transcript summary. Create a summary of the provided transcription. Respond in Markdown."""},
|
| 286 |
+
{"role": "user", "content": [{"type": "text", "text": f"The audio transcription is: {transcription}"}],}
|
| 287 |
+
],
|
| 288 |
+
temperature=0,
|
| 289 |
+
)
|
| 290 |
+
st.markdown(response.choices[0].message.content)
|
| 291 |
+
return response.choices[0].message.content
|
| 292 |
+
except:
|
| 293 |
+
st.write('No transcript')
|
| 294 |
+
|
| 295 |
+
def save_video(video_file):
|
| 296 |
+
# Save the uploaded video file
|
| 297 |
+
with open(video_file.name, "wb") as f:
|
| 298 |
+
f.write(video_file.getbuffer())
|
| 299 |
+
return video_file.name
|
| 300 |
+
|
| 301 |
+
def process_video(video_path, seconds_per_frame=2):
|
| 302 |
+
base64Frames = []
|
| 303 |
+
base_video_path, _ = os.path.splitext(video_path)
|
| 304 |
+
video = cv2.VideoCapture(video_path)
|
| 305 |
+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 306 |
+
fps = video.get(cv2.CAP_PROP_FPS)
|
| 307 |
+
frames_to_skip = int(fps * seconds_per_frame)
|
| 308 |
+
curr_frame = 0
|
| 309 |
+
|
| 310 |
+
# Loop through the video and extract frames at specified sampling rate
|
| 311 |
+
while curr_frame < total_frames - 1:
|
| 312 |
+
video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)
|
| 313 |
+
success, frame = video.read()
|
| 314 |
+
if not success:
|
| 315 |
+
break
|
| 316 |
+
_, buffer = cv2.imencode(".jpg", frame)
|
| 317 |
+
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
| 318 |
+
curr_frame += frames_to_skip
|
| 319 |
+
|
| 320 |
+
video.release()
|
| 321 |
+
|
| 322 |
+
# Extract audio from video
|
| 323 |
+
audio_path = f"{base_video_path}.mp3"
|
| 324 |
+
try:
|
| 325 |
+
clip = VideoFileClip(video_path)
|
| 326 |
+
|
| 327 |
+
clip.audio.write_audiofile(audio_path, bitrate="32k")
|
| 328 |
+
clip.audio.close()
|
| 329 |
+
|
| 330 |
+
clip.close()
|
| 331 |
+
except:
|
| 332 |
+
st.write('No audio track found, moving on..')
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
print(f"Extracted {len(base64Frames)} frames")
|
| 336 |
+
print(f"Extracted audio to {audio_path}")
|
| 337 |
+
|
| 338 |
+
return base64Frames, audio_path
|
| 339 |
+
|
| 340 |
+
def process_audio_and_video(video_input):
|
| 341 |
+
if video_input is not None:
|
| 342 |
+
# Save the uploaded video file
|
| 343 |
+
video_path = save_video(video_input )
|
| 344 |
+
|
| 345 |
+
# Process the saved video
|
| 346 |
+
base64Frames, audio_path = process_video(video_path, seconds_per_frame=1)
|
| 347 |
+
|
| 348 |
+
# Get the transcript for the video model call
|
| 349 |
+
transcript = process_audio_for_video(video_input)
|
| 350 |
+
|
| 351 |
+
# Generate a summary with visual and audio
|
| 352 |
+
response = client.chat.completions.create(
|
| 353 |
+
model=MODEL,
|
| 354 |
+
messages=[
|
| 355 |
+
{"role": "system", "content": """You are generating a video summary. Create a summary of the provided video and its transcript. Respond in Markdown"""},
|
| 356 |
+
{"role": "user", "content": [
|
| 357 |
+
"These are the frames from the video.",
|
| 358 |
+
*map(lambda x: {"type": "image_url",
|
| 359 |
+
"image_url": {"url": f'data:image/jpg;base64,{x}', "detail": "low"}}, base64Frames),
|
| 360 |
+
{"type": "text", "text": f"The audio transcription is: {transcript}"}
|
| 361 |
+
]},
|
| 362 |
+
],
|
| 363 |
+
temperature=0,
|
| 364 |
+
)
|
| 365 |
+
results = response.choices[0].message.content
|
| 366 |
+
st.markdown(results)
|
| 367 |
+
|
| 368 |
+
if transcript:
|
| 369 |
+
filename = generate_filename(transcript, "md")
|
| 370 |
+
create_file(filename, transcript, results, should_save)
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
|
| 378 |
# 🔍Search Glossary
|
| 379 |
# @st.cache_resource
|
| 380 |
def search_glossary(query):
|
|
|
|
| 909 |
if next_action=='search':
|
| 910 |
filesearch = PromptPrefix + file_contents
|
| 911 |
st.markdown(filesearch)
|
| 912 |
+
#search_glossary(filesearch)
|
| 913 |
+
|
| 914 |
+
process_text(filesearch)
|
| 915 |
|
| 916 |
if next_action=='md':
|
| 917 |
st.markdown(file_contents)
|
|
|
|
| 1152 |
key = f"{category}_{game}_{term}".replace(' ', '_').lower()
|
| 1153 |
score = load_score(key)
|
| 1154 |
if st.button(f"{game_emoji} {category} {game} {term} {score}", key=key):
|
| 1155 |
+
newscore = update_score(key.replace('?',''))
|
| 1156 |
+
query_prefix = f"{category_emoji} {game_emoji} ** {category} - {game} - {term} - **"
|
| 1157 |
+
st.markdown("Scored " + query_prefix + ' with score ' + str(newscore) + '.')
|
| 1158 |
+
|
| 1159 |
+
|
| 1160 |
+
def get_all_query_params(key):
|
| 1161 |
+
return st.query_params().get(key, [])
|
| 1162 |
+
|
| 1163 |
+
def clear_query_params():
|
| 1164 |
+
st.query_params()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
|
|
|
| 1218 |
def get_output(prompt):
|
| 1219 |
return query({"inputs": prompt})
|
| 1220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1221 |
# 6. Speech transcription via OpenAI service
|
| 1222 |
def transcribe_audio(openai_key, file_path, model):
|
| 1223 |
openai.api_key = openai_key
|
|
|
|
| 1702 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
| 1703 |
|
| 1704 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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