awacke1 commited on
Commit
ec1a55c
โ€ข
1 Parent(s): b850c73

Create backup.02162024.app.py

Browse files
Files changed (1) hide show
  1. backup.02162024.app.py +1210 -0
backup.02162024.app.py ADDED
@@ -0,0 +1,1210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import json
4
+ from PIL import Image
5
+ from urllib.parse import quote # Ensure this import is included
6
+
7
+ # Set page configuration with a title and favicon
8
+ st.set_page_config(
9
+ page_title="๐ŸŒŒ๐Ÿš€ Mixable AI - Voice Search",
10
+ page_icon="๐ŸŒ ",
11
+ layout="wide",
12
+ initial_sidebar_state="expanded",
13
+ menu_items={
14
+ 'Get Help': 'https://huggingface.co/awacke1',
15
+ 'Report a bug': "https://huggingface.co/spaces/awacke1/WebDataDownload",
16
+ 'About': "# Midjourney: https://discord.com/channels/@me/997514686608191558"
17
+ }
18
+ )
19
+
20
+ # Ensure the directory for storing scores exists
21
+ score_dir = "scores"
22
+ os.makedirs(score_dir, exist_ok=True)
23
+
24
+ # Function to generate a unique key for each button, including an emoji
25
+ def generate_key(label, header, idx):
26
+ return f"{header}_{label}_{idx}_key"
27
+
28
+ # Function to increment and save score
29
+ def update_score(key, increment=1):
30
+ score_file = os.path.join(score_dir, f"{key}.json")
31
+ if os.path.exists(score_file):
32
+ with open(score_file, "r") as file:
33
+ score_data = json.load(file)
34
+ else:
35
+ score_data = {"clicks": 0, "score": 0}
36
+
37
+ score_data["clicks"] += 1
38
+ score_data["score"] += increment
39
+
40
+ with open(score_file, "w") as file:
41
+ json.dump(score_data, file)
42
+
43
+ return score_data["score"]
44
+
45
+ # Function to load score
46
+ def load_score(key):
47
+ score_file = os.path.join(score_dir, f"{key}.json")
48
+ if os.path.exists(score_file):
49
+ with open(score_file, "r") as file:
50
+ score_data = json.load(file)
51
+ return score_data["score"]
52
+ return 0
53
+
54
+ # Transhuman Space glossary with full content
55
+ transhuman_glossary = {
56
+ "๐Ÿš€ Core Technologies": ["Nanotechnology๐Ÿ”ฌ", "Artificial Intelligence๐Ÿค–", "Quantum Computing๐Ÿ’ป", "Spacecraft Engineering๐Ÿ›ธ", "Biotechnology๐Ÿงฌ", "Cybernetics๐Ÿฆพ", "Virtual Reality๐Ÿ•ถ๏ธ", "Energy Systemsโšก", "Material Science๐Ÿงช", "Communication Technologies๐Ÿ“ก"],
57
+ "๐ŸŒ Nations": ["Terran Federation๐ŸŒ", "Martian Syndicate๐Ÿ”ด", "Jovian Republics๐Ÿช", "Asteroid Belt Communities๐ŸŒŒ", "Venusian Colonies๐ŸŒ‹", "Lunar States๐ŸŒ–", "Outer System Alliancesโœจ", "Digital Consciousness Collectives๐Ÿง ", "Transhumanist Enclaves๐Ÿฆฟ", "Non-Human Intelligence Tribes๐Ÿ‘ฝ"],
58
+ "๐Ÿ’ก Memes": ["Post-Humanism๐Ÿšถโ€โ™‚๏ธโžก๏ธ๐Ÿš€", "Neo-Evolutionism๐Ÿงฌ๐Ÿ“ˆ", "Digital Ascendancy๐Ÿ’พ๐Ÿ‘‘", "Solar System Nationalism๐ŸŒž๐Ÿ›", "Space Explorationism๐Ÿš€๐Ÿ›ฐ", "Cyber Democracy๐Ÿ–ฅ๏ธ๐Ÿ—ณ๏ธ", "Interstellar Environmentalism๐ŸŒ๐Ÿ’š", "Quantum Mysticism๐Ÿ”ฎ๐Ÿ’ซ", "Techno-Anarchism๐Ÿ”Œ๐Ÿด", "Cosmic Preservationism๐ŸŒŒ๐Ÿ›ก๏ธ"],
59
+ "๐Ÿ› Institutions": ["Interstellar Council๐Ÿช–", "Transhuman Ethical Standards Organization๐Ÿ“œ", "Galactic Trade Union๐Ÿค", "Space Habitat Authority๐Ÿ ", "Artificial Intelligence Safety Commission๐Ÿค–๐Ÿ”’", "Extraterrestrial Relations Board๐Ÿ‘ฝ๐Ÿค", "Quantum Research Institute๐Ÿ”ฌ", "Biogenetics Oversight Committee๐Ÿงซ", "Cyberspace Regulatory Agency๐Ÿ’ป", "Planetary Defense Coalition๐ŸŒ๐Ÿ›ก"],
60
+ "๐Ÿ”— Organizations": ["Neural Network Pioneers๐Ÿง ๐ŸŒ", "Spacecraft Innovators Guild๐Ÿš€๐Ÿ› ", "Quantum Computing Consortium๐Ÿ’ป๐Ÿ”—", "Interplanetary Miners Unionโ›๏ธ๐Ÿช", "Cybernetic Augmentation Advocates๐Ÿฆพโค๏ธ", "Biotechnological Harmony Group๐Ÿงฌ๐Ÿ•Š", "Stellar Navigation Circle๐Ÿงญโœจ", "Virtual Reality Creators Syndicate๐Ÿ•ถ๏ธ๐ŸŽจ", "Renewable Energy Pioneersโšก๐ŸŒฑ", "Transhuman Rights Activists๐Ÿฆฟ๐Ÿ“ข"],
61
+ "โš”๏ธ War": ["Space Warfare Tactics๐Ÿš€โš”๏ธ", "Cyber Warfare๐Ÿ–ฅ๏ธ๐Ÿ”’", "Biological Warfare๐Ÿงฌ๐Ÿ’ฃ", "Nanotech Warfare๐Ÿ”ฌโš”๏ธ", "Psychological Operations๐Ÿง ๐Ÿ—ฃ๏ธ", "Quantum Encryption & Decryption๐Ÿ”๐Ÿ’ป", "Kinetic Bombardment๐Ÿš€๐Ÿ’ฅ", "Energy Shield Defense๐Ÿ›ก๏ธโšก", "Stealth Spacecraft๐Ÿš€๐Ÿ”‡", "Artificial Intelligence Combat๐Ÿค–โš”๏ธ"],
62
+ "๐ŸŽ– Military": ["Interstellar Navy๐Ÿš€๐ŸŽ–", "Planetary Guard๐ŸŒ๐Ÿ›ก", "Cybernetic Marines๐Ÿฆพ๐Ÿ”ซ", "Nanotech Soldiers๐Ÿ”ฌ๐Ÿ’‚", "Space Drone Fleet๐Ÿ›ธ๐Ÿค–", "Quantum Signal Corps๐Ÿ’ป๐Ÿ“ก", "Special Operations Forces๐Ÿ‘ฅโš”๏ธ", "Artificial Intelligence Strategists๐Ÿค–๐Ÿ—บ๏ธ", "Orbital Defense Systems๐ŸŒŒ๐Ÿ›ก๏ธ", "Exoskeleton Brigades๐Ÿฆพ๐Ÿšถโ€โ™‚๏ธ"],
63
+ "๐Ÿฆน Outlaws": ["Pirate Fleets๐Ÿดโ€โ˜ ๏ธ๐Ÿš€", "Hacktivist Collectives๐Ÿ’ป๐Ÿšซ", "Smuggler Caravans๐Ÿ›ธ๐Ÿ’ผ", "Rebel AI Entities๐Ÿค–๐Ÿšฉ", "Black Market Biotech Dealers๐Ÿงฌ๐Ÿ’ฐ", "Quantum Thieves๐Ÿ’ป๐Ÿ•ต๏ธโ€โ™‚๏ธ", "Space Nomad Raiders๐Ÿš€๐Ÿดโ€โ˜ ๏ธ", "Cyberspace Intruders๐Ÿ’ป๐Ÿ‘พ", "Anti-Transhumanist Factions๐Ÿšซ๐Ÿฆพ", "Rogue Nanotech Swarms๐Ÿ”ฌ๐Ÿฆ "],
64
+ "๐Ÿ‘ฝ Terrorists": ["Bioengineered Virus Spreaders๐Ÿงฌ๐Ÿ’‰", "Nanotechnology Saboteurs๐Ÿ”ฌ๐Ÿงจ", "Cyber Terrorist Networks๐Ÿ’ป๐Ÿ”ฅ", "Rogue AI Sects๐Ÿค–๐Ÿ›‘", "Space Anarchist Cells๐Ÿš€โ’ถ", "Quantum Data Hijackers๐Ÿ’ป๐Ÿ”“", "Environmental Extremists๐ŸŒ๐Ÿ’ฃ", "Technological Singularity Cults๐Ÿค–๐Ÿ™", "Interspecies Supremacists๐Ÿ‘ฝ๐Ÿ‘‘", "Orbital Bombardment Threats๐Ÿ›ฐ๏ธ๐Ÿ’ฅ"],
65
+ }
66
+
67
+
68
+ # Function to search glossary and display results
69
+ def search_glossary(query):
70
+ for category, terms in transhuman_glossary.items():
71
+ if query.lower() in (term.lower() for term in terms):
72
+ st.markdown(f"### {category}")
73
+ st.write(f"- {query}")
74
+
75
+ st.write('## Processing query against GPT and Llama:')
76
+ # ------------------------------------------------------------------------------------------------
77
+ st.write('Reasoning with your inputs using GPT...')
78
+ response = chat_with_model(query)
79
+ st.write('Response:')
80
+ st.write(response)
81
+ filename = generate_filename(response, "txt")
82
+ create_file(filename, query, response, should_save)
83
+
84
+ st.write('Reasoning with your inputs using Llama...')
85
+ response = StreamLLMChatResponse(query)
86
+ filename_txt = generate_filename(query, "md")
87
+ create_file(filename_txt, query, response, should_save)
88
+ # ------------------------------------------------------------------------------------------------
89
+
90
+
91
+ # Display the glossary with Streamlit components, ensuring emojis are used
92
+ def display_glossary(area):
93
+ st.subheader(f"๐Ÿ“˜ Glossary for {area}")
94
+ terms = transhuman_glossary[area]
95
+ for idx, term in enumerate(terms, start=1):
96
+ st.write(f"{idx}. {term}")
97
+
98
+
99
+
100
+ def display_glossary_grid(glossary):
101
+ # Search URL functions with emoji as keys, now using quote for URL safety
102
+ search_urls = {
103
+ "๐Ÿ“–": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
104
+ "๐Ÿ”": lambda k: f"https://www.google.com/search?q={quote(k)}",
105
+ "โ–ถ๏ธ": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
106
+ "๐Ÿ”Ž": lambda k: f"https://www.bing.com/search?q={quote(k)}"
107
+ }
108
+
109
+ groupings = [
110
+ ["๐Ÿš€ Core Technologies", "๐ŸŒ Nations", "๐Ÿ’ก Memes"],
111
+ ["๐Ÿ› Institutions", "๐Ÿ”— Organizations", "โš”๏ธ War"],
112
+ ["๐ŸŽ– Military", "๐Ÿฆน Outlaws", "๐Ÿ‘ฝ Terrorists"],
113
+ ]
114
+
115
+ for group in groupings:
116
+ cols = st.columns(3) # Create columns for a 3x3 grid
117
+ for idx, category in enumerate(group):
118
+ with cols[idx]:
119
+ st.write(f"### {category}")
120
+ if category in glossary:
121
+ terms = glossary[category]
122
+ for term in terms:
123
+ # Generate and display links for each term, now safely encoding URLs
124
+ links_md = ' '.join([f"[{emoji}]({url(term)})" for emoji, url in search_urls.items()])
125
+ st.markdown(f"{term} {links_md}", unsafe_allow_html=True)
126
+
127
+
128
+ # Streamlined UI for displaying buttons with scores, integrating emojis
129
+ def display_buttons_with_scores():
130
+ for header, terms in transhuman_glossary.items():
131
+ st.markdown(f"## {header}")
132
+ for term in terms:
133
+ key = generate_key(term, header, terms.index(term))
134
+ score = load_score(key)
135
+ if st.button(f"{term} {score}๐Ÿš€", key=key):
136
+ update_score(key)
137
+ search_glossary('Create a three level markdown outline with 3 subpoints each where each line defines and writes out the core technology descriptions with appropriate emojis for the glossary term: ' + term)
138
+ st.experimental_rerun()
139
+
140
+ def fetch_wikipedia_summary(keyword):
141
+ # Placeholder function for fetching Wikipedia summaries
142
+ # In a real app, you might use requests to fetch from the Wikipedia API
143
+ return f"Summary for {keyword}. For more information, visit Wikipedia."
144
+
145
+ def create_search_url_youtube(keyword):
146
+ base_url = "https://www.youtube.com/results?search_query="
147
+ return base_url + keyword.replace(' ', '+')
148
+
149
+ def create_search_url_bing(keyword):
150
+ base_url = "https://www.bing.com/search?q="
151
+ return base_url + keyword.replace(' ', '+')
152
+
153
+ def create_search_url_wikipedia(keyword):
154
+ base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
155
+ return base_url + keyword.replace(' ', '+')
156
+
157
+ def create_search_url_google(keyword):
158
+ base_url = "https://www.google.com/search?q="
159
+ return base_url + keyword.replace(' ', '+')
160
+
161
+
162
+ def display_images_and_wikipedia_summaries():
163
+ st.title('Gallery with Related Stories')
164
+ image_files = [f for f in os.listdir('.') if f.endswith('.png')]
165
+ if not image_files:
166
+ st.write("No PNG images found in the current directory.")
167
+ return
168
+
169
+ for image_file in image_files:
170
+ image = Image.open(image_file)
171
+ st.image(image, caption=image_file, use_column_width=True)
172
+
173
+ keyword = image_file.split('.')[0] # Assumes keyword is the file name without extension
174
+
175
+ # Display Wikipedia and Google search links
176
+ wikipedia_url = create_search_url_wikipedia(keyword)
177
+ google_url = create_search_url_google(keyword)
178
+ youtube_url = create_search_url_youtube(keyword)
179
+ bing_url = create_search_url_bing(keyword)
180
+
181
+ links_md = f"""
182
+ [Wikipedia]({wikipedia_url}) |
183
+ [Google]({google_url}) |
184
+ [YouTube]({youtube_url}) |
185
+ [Bing]({bing_url})
186
+ """
187
+ st.markdown(links_md)
188
+
189
+
190
+ def get_all_query_params(key):
191
+ return st.query_params().get(key, [])
192
+
193
+ def clear_query_params():
194
+ st.query_params()
195
+
196
+
197
+ # Function to display content or image based on a query
198
+ def display_content_or_image(query):
199
+ # Check if the query matches any glossary term
200
+ for category, terms in transhuman_glossary.items():
201
+ for term in terms:
202
+ if query.lower() in term.lower():
203
+ st.subheader(f"Found in {category}:")
204
+ st.write(term)
205
+ return True # Return after finding and displaying the first match
206
+
207
+ # Check for an image match in a predefined directory (adjust path as needed)
208
+ image_dir = "images" # Example directory where images are stored
209
+ image_path = f"{image_dir}/{query}.png" # Construct image path with query
210
+ if os.path.exists(image_path):
211
+ st.image(image_path, caption=f"Image for {query}")
212
+ return True
213
+
214
+ # If no content or image is found
215
+ st.warning("No matching content or image found.")
216
+ return False
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+ # Imports
225
+ import base64
226
+ import glob
227
+ import json
228
+ import math
229
+ import openai
230
+ import os
231
+ import pytz
232
+ import re
233
+ import requests
234
+ import streamlit as st
235
+ import textract
236
+ import time
237
+ import zipfile
238
+ import huggingface_hub
239
+ import dotenv
240
+ from audio_recorder_streamlit import audio_recorder
241
+ from bs4 import BeautifulSoup
242
+ from collections import deque
243
+ from datetime import datetime
244
+ from dotenv import load_dotenv
245
+ from huggingface_hub import InferenceClient
246
+ from io import BytesIO
247
+ from langchain.chat_models import ChatOpenAI
248
+ from langchain.chains import ConversationalRetrievalChain
249
+ from langchain.embeddings import OpenAIEmbeddings
250
+ from langchain.memory import ConversationBufferMemory
251
+ from langchain.text_splitter import CharacterTextSplitter
252
+ from langchain.vectorstores import FAISS
253
+ from openai import ChatCompletion
254
+ from PyPDF2 import PdfReader
255
+ from templates import bot_template, css, user_template
256
+ from xml.etree import ElementTree as ET
257
+ import streamlit.components.v1 as components # Import Streamlit Components for HTML5
258
+
259
+
260
+ def add_Med_Licensing_Exam_Dataset():
261
+ import streamlit as st
262
+ from datasets import load_dataset
263
+ dataset = load_dataset("augtoma/usmle_step_1")['test'] # Using 'test' split
264
+ st.title("USMLE Step 1 Dataset Viewer")
265
+ if len(dataset) == 0:
266
+ st.write("๐Ÿ˜ข The dataset is empty.")
267
+ else:
268
+ st.write("""
269
+ ๐Ÿ” Use the search box to filter questions or use the grid to scroll through the dataset.
270
+ """)
271
+
272
+ # ๐Ÿ‘ฉโ€๐Ÿ”ฌ Search Box
273
+ search_term = st.text_input("Search for a specific question:", "")
274
+
275
+ # ๐ŸŽ› Pagination
276
+ records_per_page = 100
277
+ num_records = len(dataset)
278
+ num_pages = max(int(num_records / records_per_page), 1)
279
+
280
+ # Skip generating the slider if num_pages is 1 (i.e., all records fit in one page)
281
+ if num_pages > 1:
282
+ page_number = st.select_slider("Select page:", options=list(range(1, num_pages + 1)))
283
+ else:
284
+ page_number = 1 # Only one page
285
+
286
+ # ๐Ÿ“Š Display Data
287
+ start_idx = (page_number - 1) * records_per_page
288
+ end_idx = start_idx + records_per_page
289
+
290
+ # ๐Ÿงช Apply the Search Filter
291
+ filtered_data = []
292
+ for record in dataset[start_idx:end_idx]:
293
+ if isinstance(record, dict) and 'text' in record and 'id' in record:
294
+ if search_term:
295
+ if search_term.lower() in record['text'].lower():
296
+ st.markdown(record)
297
+ filtered_data.append(record)
298
+ else:
299
+ filtered_data.append(record)
300
+
301
+ # ๐ŸŒ Render the Grid
302
+ for record in filtered_data:
303
+ st.write(f"## Question ID: {record['id']}")
304
+ st.write(f"### Question:")
305
+ st.write(f"{record['text']}")
306
+ st.write(f"### Answer:")
307
+ st.write(f"{record['answer']}")
308
+ st.write("---")
309
+
310
+ st.write(f"๐Ÿ˜Š Total Records: {num_records} | ๐Ÿ“„ Displaying {start_idx+1} to {min(end_idx, num_records)}")
311
+
312
+ # 1. Constants and Top Level UI Variables
313
+
314
+ # My Inference API Copy
315
+ API_URL = 'https://qe55p8afio98s0u3.us-east-1.aws.endpoints.huggingface.cloud' # Dr Llama
316
+ # Meta's Original - Chat HF Free Version:
317
+ #API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
318
+ API_KEY = os.getenv('API_KEY')
319
+ MODEL1="meta-llama/Llama-2-7b-chat-hf"
320
+ MODEL1URL="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf"
321
+ HF_KEY = os.getenv('HF_KEY')
322
+ headers = {
323
+ "Authorization": f"Bearer {HF_KEY}",
324
+ "Content-Type": "application/json"
325
+ }
326
+ key = os.getenv('OPENAI_API_KEY')
327
+ prompt = f"Write instructions to teach discharge planning along with guidelines and patient education. List entities, features and relationships to CCDA and FHIR objects in boldface."
328
+ should_save = st.sidebar.checkbox("๐Ÿ’พ Save", value=True, help="Save your session data.")
329
+
330
+ # 2. Prompt label button demo for LLM
331
+ def add_witty_humor_buttons():
332
+ with st.expander("Wit and Humor ๐Ÿคฃ", expanded=True):
333
+ # Tip about the Dromedary family
334
+ st.markdown("๐Ÿ”ฌ **Fun Fact**: Dromedaries, part of the camel family, have a single hump and are adapted to arid environments. Their 'superpowers' include the ability to survive without water for up to 7 days, thanks to their specialized blood cells and water storage in their hump.")
335
+
336
+ # Define button descriptions
337
+ descriptions = {
338
+ "Generate Limericks ๐Ÿ˜‚": "Write ten random adult limericks based on quotes that are tweet length and make you laugh ๐ŸŽญ",
339
+ "Wise Quotes ๐Ÿง™": "Generate ten wise quotes that are tweet length ๐Ÿฆ‰",
340
+ "Funny Rhymes ๐ŸŽค": "Create ten funny rhymes that are tweet length ๐ŸŽถ",
341
+ "Medical Jokes ๐Ÿ’‰": "Create ten medical jokes that are tweet length ๐Ÿฅ",
342
+ "Minnesota Humor โ„๏ธ": "Create ten jokes about Minnesota that are tweet length ๐ŸŒจ๏ธ",
343
+ "Top Funny Stories ๐Ÿ“–": "Create ten funny stories that are tweet length ๐Ÿ“š",
344
+ "More Funny Rhymes ๐ŸŽ™๏ธ": "Create ten more funny rhymes that are tweet length ๐ŸŽต"
345
+ }
346
+
347
+ # Create columns
348
+ col1, col2, col3 = st.columns([1, 1, 1], gap="small")
349
+
350
+ # Add buttons to columns
351
+ if col1.button("Wise Limericks ๐Ÿ˜‚"):
352
+ StreamLLMChatResponse(descriptions["Generate Limericks ๐Ÿ˜‚"])
353
+
354
+ if col2.button("Wise Quotes ๐Ÿง™"):
355
+ StreamLLMChatResponse(descriptions["Wise Quotes ๐Ÿง™"])
356
+
357
+ #if col3.button("Funny Rhymes ๐ŸŽค"):
358
+ # StreamLLMChatResponse(descriptions["Funny Rhymes ๐ŸŽค"])
359
+
360
+ col4, col5, col6 = st.columns([1, 1, 1], gap="small")
361
+
362
+ if col4.button("Top Ten Funniest Clean Jokes ๐Ÿ’‰"):
363
+ StreamLLMChatResponse(descriptions["Top Ten Funniest Clean Jokes ๐Ÿ’‰"])
364
+
365
+ if col5.button("Minnesota Humor โ„๏ธ"):
366
+ StreamLLMChatResponse(descriptions["Minnesota Humor โ„๏ธ"])
367
+
368
+ if col6.button("Origins of Medical Science True Stories"):
369
+ StreamLLMChatResponse(descriptions["Origins of Medical Science True Stories"])
370
+
371
+ col7 = st.columns(1, gap="small")
372
+
373
+ if col7[0].button("Top Ten Best Write a streamlit python program prompts to build AI programs. ๐ŸŽ™๏ธ"):
374
+ StreamLLMChatResponse(descriptions["Top Ten Best Write a streamlit python program prompts to build AI programs. ๐ŸŽ™๏ธ"])
375
+
376
+ def SpeechSynthesis(result):
377
+ documentHTML5='''
378
+ <!DOCTYPE html>
379
+ <html>
380
+ <head>
381
+ <title>Read It Aloud</title>
382
+ <script type="text/javascript">
383
+ function readAloud() {
384
+ const text = document.getElementById("textArea").value;
385
+ const speech = new SpeechSynthesisUtterance(text);
386
+ window.speechSynthesis.speak(speech);
387
+ }
388
+ </script>
389
+ </head>
390
+ <body>
391
+ <h1>๐Ÿ”Š Read It Aloud</h1>
392
+ <textarea id="textArea" rows="10" cols="80">
393
+ '''
394
+ documentHTML5 = documentHTML5 + result
395
+ documentHTML5 = documentHTML5 + '''
396
+ </textarea>
397
+ <br>
398
+ <button onclick="readAloud()">๐Ÿ”Š Read Aloud</button>
399
+ </body>
400
+ </html>
401
+ '''
402
+
403
+ components.html(documentHTML5, width=1280, height=300)
404
+ #return result
405
+
406
+
407
+ # 3. Stream Llama Response
408
+ # @st.cache_resource
409
+ def StreamLLMChatResponse(prompt):
410
+ try:
411
+ endpoint_url = API_URL
412
+ hf_token = API_KEY
413
+ st.write('Running client ' + endpoint_url)
414
+ client = InferenceClient(endpoint_url, token=hf_token)
415
+ gen_kwargs = dict(
416
+ max_new_tokens=512,
417
+ top_k=30,
418
+ top_p=0.9,
419
+ temperature=0.2,
420
+ repetition_penalty=1.02,
421
+ stop_sequences=["\nUser:", "<|endoftext|>", "</s>"],
422
+ )
423
+ stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)
424
+ report=[]
425
+ res_box = st.empty()
426
+ collected_chunks=[]
427
+ collected_messages=[]
428
+ allresults=''
429
+ for r in stream:
430
+ if r.token.special:
431
+ continue
432
+ if r.token.text in gen_kwargs["stop_sequences"]:
433
+ break
434
+ collected_chunks.append(r.token.text)
435
+ chunk_message = r.token.text
436
+ collected_messages.append(chunk_message)
437
+ try:
438
+ report.append(r.token.text)
439
+ if len(r.token.text) > 0:
440
+ result="".join(report).strip()
441
+ res_box.markdown(f'*{result}*')
442
+
443
+ except:
444
+ st.write('Stream llm issue')
445
+ SpeechSynthesis(result)
446
+ return result
447
+ except:
448
+ st.write('Llama model is asleep. Starting up now on A10 - please give 5 minutes then retry as KEDA scales up from zero to activate running container(s).')
449
+
450
+ # 4. Run query with payload
451
+ def query(payload):
452
+ response = requests.post(API_URL, headers=headers, json=payload)
453
+ st.markdown(response.json())
454
+ return response.json()
455
+ def get_output(prompt):
456
+ return query({"inputs": prompt})
457
+
458
+ # 5. Auto name generated output files from time and content
459
+ def generate_filename(prompt, file_type):
460
+ central = pytz.timezone('US/Central')
461
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
462
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
463
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:255] # 255 is linux max, 260 is windows max
464
+ #safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:45]
465
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
466
+
467
+ # 6. Speech transcription via OpenAI service
468
+ def transcribe_audio(openai_key, file_path, model):
469
+ openai.api_key = openai_key
470
+ OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
471
+ headers = {
472
+ "Authorization": f"Bearer {openai_key}",
473
+ }
474
+ with open(file_path, 'rb') as f:
475
+ data = {'file': f}
476
+ st.write('STT transcript ' + OPENAI_API_URL)
477
+ response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
478
+ if response.status_code == 200:
479
+ st.write(response.json())
480
+ chatResponse = chat_with_model(response.json().get('text'), '') # *************************************
481
+ transcript = response.json().get('text')
482
+ filename = generate_filename(transcript, 'txt')
483
+ response = chatResponse
484
+ user_prompt = transcript
485
+ create_file(filename, user_prompt, response, should_save)
486
+ return transcript
487
+ else:
488
+ st.write(response.json())
489
+ st.error("Error in API call.")
490
+ return None
491
+
492
+ # 7. Auto stop on silence audio control for recording WAV files
493
+ def save_and_play_audio(audio_recorder):
494
+ audio_bytes = audio_recorder(key='audio_recorder')
495
+ if audio_bytes:
496
+ filename = generate_filename("Recording", "wav")
497
+ with open(filename, 'wb') as f:
498
+ f.write(audio_bytes)
499
+ st.audio(audio_bytes, format="audio/wav")
500
+ return filename
501
+ return None
502
+
503
+ # 8. File creator that interprets type and creates output file for text, markdown and code
504
+ def create_file(filename, prompt, response, should_save=True):
505
+ if not should_save:
506
+ return
507
+ base_filename, ext = os.path.splitext(filename)
508
+ if ext in ['.txt', '.htm', '.md']:
509
+ with open(f"{base_filename}.md", 'w') as file:
510
+ try:
511
+ content = prompt.strip() + '\r\n' + response
512
+ file.write(content)
513
+ except:
514
+ st.write('.')
515
+
516
+ #has_python_code = re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response)
517
+ #has_python_code = bool(re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response))
518
+ #if has_python_code:
519
+ # python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
520
+ # with open(f"{base_filename}-Code.py", 'w') as file:
521
+ # file.write(python_code)
522
+ # with open(f"{base_filename}.md", 'w') as file:
523
+ # content = prompt.strip() + '\r\n' + response
524
+ # file.write(content)
525
+
526
+ def truncate_document(document, length):
527
+ return document[:length]
528
+ def divide_document(document, max_length):
529
+ return [document[i:i+max_length] for i in range(0, len(document), max_length)]
530
+
531
+ # 9. Sidebar with UI controls to review and re-run prompts and continue responses
532
+ @st.cache_resource
533
+ def get_table_download_link(file_path):
534
+ with open(file_path, 'r') as file:
535
+ data = file.read()
536
+
537
+ b64 = base64.b64encode(data.encode()).decode()
538
+ file_name = os.path.basename(file_path)
539
+ ext = os.path.splitext(file_name)[1] # get the file extension
540
+ if ext == '.txt':
541
+ mime_type = 'text/plain'
542
+ elif ext == '.py':
543
+ mime_type = 'text/plain'
544
+ elif ext == '.xlsx':
545
+ mime_type = 'text/plain'
546
+ elif ext == '.csv':
547
+ mime_type = 'text/plain'
548
+ elif ext == '.htm':
549
+ mime_type = 'text/html'
550
+ elif ext == '.md':
551
+ mime_type = 'text/markdown'
552
+ elif ext == '.wav':
553
+ mime_type = 'audio/wav'
554
+ else:
555
+ mime_type = 'application/octet-stream' # general binary data type
556
+ href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
557
+ return href
558
+
559
+
560
+ def CompressXML(xml_text):
561
+ root = ET.fromstring(xml_text)
562
+ for elem in list(root.iter()):
563
+ if isinstance(elem.tag, str) and 'Comment' in elem.tag:
564
+ elem.parent.remove(elem)
565
+ return ET.tostring(root, encoding='unicode', method="xml")
566
+
567
+ # 10. Read in and provide UI for past files
568
+ @st.cache_resource
569
+ def read_file_content(file,max_length):
570
+ if file.type == "application/json":
571
+ content = json.load(file)
572
+ return str(content)
573
+ elif file.type == "text/html" or file.type == "text/htm":
574
+ content = BeautifulSoup(file, "html.parser")
575
+ return content.text
576
+ elif file.type == "application/xml" or file.type == "text/xml":
577
+ tree = ET.parse(file)
578
+ root = tree.getroot()
579
+ xml = CompressXML(ET.tostring(root, encoding='unicode'))
580
+ return xml
581
+ elif file.type == "text/markdown" or file.type == "text/md":
582
+ md = mistune.create_markdown()
583
+ content = md(file.read().decode())
584
+ return content
585
+ elif file.type == "text/plain":
586
+ return file.getvalue().decode()
587
+ else:
588
+ return ""
589
+
590
+ # 11. Chat with GPT - Caution on quota - now favoring fastest AI pipeline STT Whisper->LLM Llama->TTS
591
+ @st.cache_resource
592
+ def chat_with_model(prompt, document_section='', model_choice='gpt-3.5-turbo'):
593
+ model = model_choice
594
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
595
+ conversation.append({'role': 'user', 'content': prompt})
596
+ if len(document_section)>0:
597
+ conversation.append({'role': 'assistant', 'content': document_section})
598
+ start_time = time.time()
599
+ report = []
600
+ res_box = st.empty()
601
+ collected_chunks = []
602
+ collected_messages = []
603
+
604
+ st.write('LLM stream ' + 'gpt-3.5-turbo')
605
+ for chunk in openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=conversation, temperature=0.5, stream=True):
606
+ collected_chunks.append(chunk)
607
+ chunk_message = chunk['choices'][0]['delta']
608
+ collected_messages.append(chunk_message)
609
+ content=chunk["choices"][0].get("delta",{}).get("content")
610
+ try:
611
+ report.append(content)
612
+ if len(content) > 0:
613
+ result = "".join(report).strip()
614
+ res_box.markdown(f'*{result}*')
615
+ except:
616
+ st.write(' ')
617
+ full_reply_content = ''.join([m.get('content', '') for m in collected_messages])
618
+ st.write("Elapsed time:")
619
+ st.write(time.time() - start_time)
620
+ return full_reply_content
621
+
622
+ # 12. Embedding VectorDB for LLM query of documents to text to compress inputs and prompt together as Chat memory using Langchain
623
+ @st.cache_resource
624
+ def chat_with_file_contents(prompt, file_content, model_choice='gpt-3.5-turbo'):
625
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
626
+ conversation.append({'role': 'user', 'content': prompt})
627
+ if len(file_content)>0:
628
+ conversation.append({'role': 'assistant', 'content': file_content})
629
+ response = openai.ChatCompletion.create(model=model_choice, messages=conversation)
630
+ return response['choices'][0]['message']['content']
631
+
632
+ def extract_mime_type(file):
633
+ if isinstance(file, str):
634
+ pattern = r"type='(.*?)'"
635
+ match = re.search(pattern, file)
636
+ if match:
637
+ return match.group(1)
638
+ else:
639
+ raise ValueError(f"Unable to extract MIME type from {file}")
640
+ elif isinstance(file, streamlit.UploadedFile):
641
+ return file.type
642
+ else:
643
+ raise TypeError("Input should be a string or a streamlit.UploadedFile object")
644
+
645
+ def extract_file_extension(file):
646
+ # get the file name directly from the UploadedFile object
647
+ file_name = file.name
648
+ pattern = r".*?\.(.*?)$"
649
+ match = re.search(pattern, file_name)
650
+ if match:
651
+ return match.group(1)
652
+ else:
653
+ raise ValueError(f"Unable to extract file extension from {file_name}")
654
+
655
+ # Normalize input as text from PDF and other formats
656
+ @st.cache_resource
657
+ def pdf2txt(docs):
658
+ text = ""
659
+ for file in docs:
660
+ file_extension = extract_file_extension(file)
661
+ st.write(f"File type extension: {file_extension}")
662
+ if file_extension.lower() in ['py', 'txt', 'html', 'htm', 'xml', 'json']:
663
+ text += file.getvalue().decode('utf-8')
664
+ elif file_extension.lower() == 'pdf':
665
+ from PyPDF2 import PdfReader
666
+ pdf = PdfReader(BytesIO(file.getvalue()))
667
+ for page in range(len(pdf.pages)):
668
+ text += pdf.pages[page].extract_text() # new PyPDF2 syntax
669
+ return text
670
+
671
+ def txt2chunks(text):
672
+ text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
673
+ return text_splitter.split_text(text)
674
+
675
+ # Vector Store using FAISS
676
+ @st.cache_resource
677
+ def vector_store(text_chunks):
678
+ embeddings = OpenAIEmbeddings(openai_api_key=key)
679
+ return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
680
+
681
+ # Memory and Retrieval chains
682
+ @st.cache_resource
683
+ def get_chain(vectorstore):
684
+ llm = ChatOpenAI()
685
+ memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
686
+ return ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(), memory=memory)
687
+
688
+ def process_user_input(user_question):
689
+ response = st.session_state.conversation({'question': user_question})
690
+ st.session_state.chat_history = response['chat_history']
691
+ for i, message in enumerate(st.session_state.chat_history):
692
+ template = user_template if i % 2 == 0 else bot_template
693
+ st.write(template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
694
+ filename = generate_filename(user_question, 'txt')
695
+ response = message.content
696
+ user_prompt = user_question
697
+ create_file(filename, user_prompt, response, should_save)
698
+
699
+ def divide_prompt(prompt, max_length):
700
+ words = prompt.split()
701
+ chunks = []
702
+ current_chunk = []
703
+ current_length = 0
704
+ for word in words:
705
+ if len(word) + current_length <= max_length:
706
+ current_length += len(word) + 1
707
+ current_chunk.append(word)
708
+ else:
709
+ chunks.append(' '.join(current_chunk))
710
+ current_chunk = [word]
711
+ current_length = len(word)
712
+ chunks.append(' '.join(current_chunk))
713
+ return chunks
714
+
715
+
716
+ # 13. Provide way of saving all and deleting all to give way of reviewing output and saving locally before clearing it
717
+
718
+ @st.cache_resource
719
+ def create_zip_of_files(files):
720
+ zip_name = "all_files.zip"
721
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
722
+ for file in files:
723
+ zipf.write(file)
724
+ return zip_name
725
+
726
+ @st.cache_resource
727
+ def get_zip_download_link(zip_file):
728
+ with open(zip_file, 'rb') as f:
729
+ data = f.read()
730
+ b64 = base64.b64encode(data).decode()
731
+ href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
732
+ return href
733
+
734
+ # 14. Inference Endpoints for Whisper (best fastest STT) on NVIDIA T4 and Llama (best fastest AGI LLM) on NVIDIA A10
735
+ # My Inference Endpoint
736
+ API_URL_IE = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
737
+ # Original
738
+ API_URL_IE = "https://api-inference.huggingface.co/models/openai/whisper-small.en"
739
+ MODEL2 = "openai/whisper-small.en"
740
+ MODEL2_URL = "https://huggingface.co/openai/whisper-small.en"
741
+ #headers = {
742
+ # "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
743
+ # "Content-Type": "audio/wav"
744
+ #}
745
+ # HF_KEY = os.getenv('HF_KEY')
746
+ HF_KEY = st.secrets['HF_KEY']
747
+ headers = {
748
+ "Authorization": f"Bearer {HF_KEY}",
749
+ "Content-Type": "audio/wav"
750
+ }
751
+
752
+ #@st.cache_resource
753
+ def query(filename):
754
+ with open(filename, "rb") as f:
755
+ data = f.read()
756
+ response = requests.post(API_URL_IE, headers=headers, data=data)
757
+ return response.json()
758
+
759
+ def generate_filename(prompt, file_type):
760
+ central = pytz.timezone('US/Central')
761
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
762
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
763
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
764
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
765
+
766
+ # 15. Audio recorder to Wav file
767
+ def save_and_play_audio(audio_recorder):
768
+ audio_bytes = audio_recorder()
769
+ if audio_bytes:
770
+ filename = generate_filename("Recording", "wav")
771
+ with open(filename, 'wb') as f:
772
+ f.write(audio_bytes)
773
+ st.audio(audio_bytes, format="audio/wav")
774
+ return filename
775
+
776
+ # 16. Speech transcription to file output
777
+ def transcribe_audio(filename):
778
+ output = query(filename)
779
+ return output
780
+
781
+ def whisper_main():
782
+ #st.title("Speech to Text")
783
+ #st.write("Record your speech and get the text.")
784
+
785
+ # Audio, transcribe, GPT:
786
+ filename = save_and_play_audio(audio_recorder)
787
+ if filename is not None:
788
+ transcription = transcribe_audio(filename)
789
+ try:
790
+ transcript = transcription['text']
791
+ st.write(transcript)
792
+
793
+ except:
794
+ transcript=''
795
+ st.write(transcript)
796
+
797
+
798
+ # Whisper to GPT: New!! ---------------------------------------------------------------------
799
+ st.write('Reasoning with your inputs with GPT..')
800
+ response = chat_with_model(transcript)
801
+ st.write('Response:')
802
+ st.write(response)
803
+
804
+ filename = generate_filename(response, "txt")
805
+ create_file(filename, transcript, response, should_save)
806
+ # Whisper to GPT: New!! ---------------------------------------------------------------------
807
+
808
+
809
+ # Whisper to Llama:
810
+ response = StreamLLMChatResponse(transcript)
811
+ filename_txt = generate_filename(transcript, "md")
812
+ create_file(filename_txt, transcript, response, should_save)
813
+
814
+ filename_wav = filename_txt.replace('.txt', '.wav')
815
+ import shutil
816
+ try:
817
+ if os.path.exists(filename):
818
+ shutil.copyfile(filename, filename_wav)
819
+ except:
820
+ st.write('.')
821
+
822
+ if os.path.exists(filename):
823
+ os.remove(filename)
824
+
825
+ #st.experimental_rerun()
826
+ #except:
827
+ # st.write('Starting Whisper Model on GPU. Please retry in 30 seconds.')
828
+
829
+
830
+
831
+ # Sample function to demonstrate a response, replace with your own logic
832
+ def StreamMedChatResponse(topic):
833
+ st.write(f"Showing resources or questions related to: {topic}")
834
+
835
+
836
+
837
+ def add_medical_exam_buttons():
838
+ # Medical exam terminology descriptions
839
+ descriptions = {
840
+ "White Blood Cells ๐ŸŒŠ": "3 Q&A with emojis about types, facts, function, inputs and outputs of white blood cells ๐ŸŽฅ",
841
+ "CT Imaging๐Ÿฆ ": "3 Q&A with emojis on CT Imaging post surgery, how to, what to look for ๐Ÿ’Š",
842
+ "Hematoma ๐Ÿ’‰": "3 Q&A with emojis about hematoma and infection care and study including bacteria cultures and tests or labs๐Ÿ’ช",
843
+ "Post Surgery Wound Care ๐ŸŒ": "3 Q&A with emojis on wound care, and good bedside manner ๐Ÿฉธ",
844
+ "Healing and humor ๐Ÿ’Š": "3 Q&A with emojis on stories and humor about healing and caregiving ๐Ÿš‘",
845
+ "Psychology of bedside manner ๐Ÿงฌ": "3 Q&A with emojis on bedside manner and how to make patients feel at ease๐Ÿ› ",
846
+ "CT scan ๐Ÿ’Š": "3 Q&A with analysis on infection using CT scan and packing for skin, cellulitus and fascia ๐Ÿฉบ"
847
+ }
848
+
849
+ # Expander for medical topics
850
+ with st.expander("Medical Licensing Exam Topics ๐Ÿ“š", expanded=False):
851
+ st.markdown("๐Ÿฉบ **Important**: Variety of topics for medical licensing exams.")
852
+
853
+ # Create buttons for each description with unique keys
854
+ for idx, (label, content) in enumerate(descriptions.items()):
855
+ button_key = f"button_{idx}"
856
+ if st.button(label, key=button_key):
857
+ st.write(f"Running {label}")
858
+ input='Create markdown outline for definition of topic ' + label + ' also short quiz with appropriate emojis and definitions for: ' + content
859
+ response=StreamLLMChatResponse(input)
860
+ filename = generate_filename(response, 'txt')
861
+ create_file(filename, input, response, should_save)
862
+
863
+ def add_medical_exam_buttons2():
864
+ with st.expander("Medical Licensing Exam Topics ๐Ÿ“š", expanded=False):
865
+ st.markdown("๐Ÿฉบ **Important**: This section provides a variety of medical topics that are often encountered in medical licensing exams.")
866
+
867
+ # Define medical exam terminology descriptions
868
+ descriptions = {
869
+ "White Blood Cells ๐ŸŒŠ": "3 Questions and Answers with emojis about white blood cells ๐ŸŽฅ",
870
+ "CT Imaging๐Ÿฆ ": "3 Questions and Answers with emojis about CT Imaging of post surgery abscess, hematoma, and cerosanguiness fluid ๐Ÿ’Š",
871
+ "Hematoma ๐Ÿ’‰": "3 Questions and Answers with emojis about hematoma and infection and how heat helps white blood cells ๐Ÿ’ช",
872
+ "Post Surgery Wound Care ๐ŸŒ": "3 Questions and Answers with emojis about wound care and how to help as a caregiver๐Ÿฉธ",
873
+ "Healing and humor ๐Ÿ’Š": "3 Questions and Answers with emojis on the use of stories and humor to help patients and family ๐Ÿš‘",
874
+ "Psychology of bedside manner ๐Ÿงฌ": "3 Questions and Answers with emojis about good bedside manner ๐Ÿ› ",
875
+ "CT scan ๐Ÿ’Š": "3 Questions and Answers with analysis of bacteria and understanding infection using cultures and CT scan ๐Ÿฉบ"
876
+ }
877
+
878
+ # Create columns
879
+ col1, col2, col3, col4 = st.columns([1, 1, 1, 1], gap="small")
880
+
881
+ # Add buttons to columns
882
+ if col1.button("Ultrasound with Doppler ๐ŸŒŠ"):
883
+ StreamLLMChatResponse(descriptions["Ultrasound with Doppler ๐ŸŒŠ"])
884
+
885
+ if col2.button("Oseltamivir ๐Ÿฆ "):
886
+ StreamLLMChatResponse(descriptions["Oseltamivir ๐Ÿฆ "])
887
+
888
+ if col3.button("IM Epinephrine ๐Ÿ’‰"):
889
+ StreamLLMChatResponse(descriptions["IM Epinephrine ๐Ÿ’‰"])
890
+
891
+ if col4.button("Hypokalemia ๐ŸŒ"):
892
+ StreamLLMChatResponse(descriptions["Hypokalemia ๐ŸŒ"])
893
+
894
+ col5, col6, col7, col8 = st.columns([1, 1, 1, 1], gap="small")
895
+
896
+ if col5.button("Succinylcholine ๐Ÿ’Š"):
897
+ StreamLLMChatResponse(descriptions["Succinylcholine ๐Ÿ’Š"])
898
+
899
+ if col6.button("Phosphoinositol System ๐Ÿงฌ"):
900
+ StreamLLMChatResponse(descriptions["Phosphoinositol System ๐Ÿงฌ"])
901
+
902
+ if col7.button("Ramipril ๐Ÿ’Š"):
903
+ StreamLLMChatResponse(descriptions["Ramipril ๐Ÿ’Š"])
904
+
905
+
906
+
907
+ # 17. Main
908
+ def main():
909
+ prompt = f"Write ten funny jokes that are tweet length stories that make you laugh. Show as markdown outline with emojis for each."
910
+ # Add Wit and Humor buttons
911
+ # add_witty_humor_buttons()
912
+ # add_medical_exam_buttons()
913
+
914
+ with st.expander("Prompts ๐Ÿ“š", expanded=False):
915
+ example_input = st.text_input("Enter your prompt text for Llama:", value=prompt, help="Enter text to get a response from DromeLlama.")
916
+ if st.button("Run Prompt With Llama model", help="Click to run the prompt."):
917
+ try:
918
+ response=StreamLLMChatResponse(example_input)
919
+ create_file(filename, example_input, response, should_save)
920
+ except:
921
+ st.write('Llama model is asleep. Starting now on A10 GPU. Please wait one minute then retry. KEDA triggered.')
922
+
923
+ openai.api_key = os.getenv('OPENAI_API_KEY')
924
+ if openai.api_key == None: openai.api_key = st.secrets['OPENAI_API_KEY']
925
+
926
+ menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
927
+ choice = st.sidebar.selectbox("Output File Type:", menu)
928
+
929
+ model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
930
+
931
+ user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
932
+ collength, colupload = st.columns([2,3]) # adjust the ratio as needed
933
+ with collength:
934
+ max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
935
+ with colupload:
936
+ uploaded_file = st.file_uploader("Add a file for context:", type=["pdf", "xml", "json", "xlsx", "csv", "html", "htm", "md", "txt"])
937
+ document_sections = deque()
938
+ document_responses = {}
939
+ if uploaded_file is not None:
940
+ file_content = read_file_content(uploaded_file, max_length)
941
+ document_sections.extend(divide_document(file_content, max_length))
942
+ if len(document_sections) > 0:
943
+ if st.button("๐Ÿ‘๏ธ View Upload"):
944
+ st.markdown("**Sections of the uploaded file:**")
945
+ for i, section in enumerate(list(document_sections)):
946
+ st.markdown(f"**Section {i+1}**\n{section}")
947
+ st.markdown("**Chat with the model:**")
948
+ for i, section in enumerate(list(document_sections)):
949
+ if i in document_responses:
950
+ st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
951
+ else:
952
+ if st.button(f"Chat about Section {i+1}"):
953
+ st.write('Reasoning with your inputs...')
954
+ #response = chat_with_model(user_prompt, section, model_choice)
955
+ st.write('Response:')
956
+ st.write(response)
957
+ document_responses[i] = response
958
+ filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
959
+ create_file(filename, user_prompt, response, should_save)
960
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
961
+
962
+
963
+ if st.button('๐Ÿ’ฌ Chat'):
964
+ st.write('Reasoning with your inputs...')
965
+ user_prompt_sections = divide_prompt(user_prompt, max_length)
966
+ full_response = ''
967
+ for prompt_section in user_prompt_sections:
968
+ response = chat_with_model(prompt_section, ''.join(list(document_sections)), model_choice)
969
+ full_response += response + '\n' # Combine the responses
970
+ response = full_response
971
+ st.write('Response:')
972
+ st.write(response)
973
+ filename = generate_filename(user_prompt, choice)
974
+ create_file(filename, user_prompt, response, should_save)
975
+
976
+ # Compose a file sidebar of markdown md files:
977
+ all_files = glob.glob("*.md")
978
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
979
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
980
+ if st.sidebar.button("๐Ÿ—‘ Delete All Text"):
981
+ for file in all_files:
982
+ os.remove(file)
983
+ st.experimental_rerun()
984
+ if st.sidebar.button("โฌ‡๏ธ Download All"):
985
+ zip_file = create_zip_of_files(all_files)
986
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
987
+ file_contents=''
988
+ next_action=''
989
+ for file in all_files:
990
+ col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
991
+ with col1:
992
+ if st.button("๐ŸŒ", key="md_"+file): # md emoji button
993
+ with open(file, 'r') as f:
994
+ file_contents = f.read()
995
+ next_action='md'
996
+ with col2:
997
+ st.markdown(get_table_download_link(file), unsafe_allow_html=True)
998
+ with col3:
999
+ if st.button("๐Ÿ“‚", key="open_"+file): # open emoji button
1000
+ with open(file, 'r') as f:
1001
+ file_contents = f.read()
1002
+ next_action='open'
1003
+ with col4:
1004
+ if st.button("๐Ÿ”", key="read_"+file): # search emoji button
1005
+ with open(file, 'r') as f:
1006
+ file_contents = f.read()
1007
+ next_action='search'
1008
+ with col5:
1009
+ if st.button("๐Ÿ—‘", key="delete_"+file):
1010
+ os.remove(file)
1011
+ st.experimental_rerun()
1012
+
1013
+
1014
+ if len(file_contents) > 0:
1015
+ if next_action=='open':
1016
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
1017
+ if next_action=='md':
1018
+ st.markdown(file_contents)
1019
+
1020
+ buttonlabel = '๐Ÿ”Run with Llama and GPT.'
1021
+ if st.button(key='RunWithLlamaandGPT', label = buttonlabel):
1022
+ user_prompt = file_contents
1023
+
1024
+ # Llama versus GPT Battle!
1025
+ all=""
1026
+ try:
1027
+ st.write('๐Ÿ”Running with Llama.')
1028
+ response = StreamLLMChatResponse(file_contents)
1029
+ filename = generate_filename(user_prompt, "md")
1030
+ create_file(filename, file_contents, response, should_save)
1031
+ all=response
1032
+ #SpeechSynthesis(response)
1033
+ except:
1034
+ st.markdown('Llama is sleeping. Restart ETA 30 seconds.')
1035
+
1036
+ # gpt
1037
+ try:
1038
+ st.write('๐Ÿ”Running with GPT.')
1039
+ response2 = chat_with_model(user_prompt, file_contents, model_choice)
1040
+ filename2 = generate_filename(file_contents, choice)
1041
+ create_file(filename2, user_prompt, response, should_save)
1042
+ all=all+response2
1043
+ #SpeechSynthesis(response2)
1044
+ except:
1045
+ st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
1046
+
1047
+ SpeechSynthesis(all)
1048
+
1049
+
1050
+ if next_action=='search':
1051
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
1052
+ st.write('๐Ÿ”Running with Llama and GPT.')
1053
+
1054
+ user_prompt = file_contents
1055
+
1056
+ # Llama versus GPT Battle!
1057
+ all=""
1058
+ try:
1059
+ st.write('๐Ÿ”Running with Llama.')
1060
+ response = StreamLLMChatResponse(file_contents)
1061
+ filename = generate_filename(user_prompt, ".md")
1062
+ create_file(filename, file_contents, response, should_save)
1063
+ all=response
1064
+ #SpeechSynthesis(response)
1065
+ except:
1066
+ st.markdown('Llama is sleeping. Restart ETA 30 seconds.')
1067
+
1068
+ # gpt
1069
+ try:
1070
+ st.write('๐Ÿ”Running with GPT.')
1071
+ response2 = chat_with_model(user_prompt, file_contents, model_choice)
1072
+ filename2 = generate_filename(file_contents, choice)
1073
+ create_file(filename2, user_prompt, response, should_save)
1074
+ all=all+response2
1075
+ #SpeechSynthesis(response2)
1076
+ except:
1077
+ st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
1078
+
1079
+ SpeechSynthesis(all)
1080
+
1081
+
1082
+ # Function to encode file to base64
1083
+ def get_base64_encoded_file(file_path):
1084
+ with open(file_path, "rb") as file:
1085
+ return base64.b64encode(file.read()).decode()
1086
+
1087
+ # Function to create a download link
1088
+ def get_audio_download_link(file_path):
1089
+ base64_file = get_base64_encoded_file(file_path)
1090
+ return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">โฌ‡๏ธ Download Audio</a>'
1091
+
1092
+ # Compose a file sidebar of past encounters
1093
+ all_files = glob.glob("*.wav")
1094
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
1095
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
1096
+
1097
+ filekey = 'delall'
1098
+ if st.sidebar.button("๐Ÿ—‘ Delete All Audio", key=filekey):
1099
+ for file in all_files:
1100
+ os.remove(file)
1101
+ st.experimental_rerun()
1102
+
1103
+ for file in all_files:
1104
+ col1, col2 = st.sidebar.columns([6, 1]) # adjust the ratio as needed
1105
+ with col1:
1106
+ st.markdown(file)
1107
+ if st.button("๐ŸŽต", key="play_" + file): # play emoji button
1108
+ audio_file = open(file, 'rb')
1109
+ audio_bytes = audio_file.read()
1110
+ st.audio(audio_bytes, format='audio/wav')
1111
+ #st.markdown(get_audio_download_link(file), unsafe_allow_html=True)
1112
+ #st.text_input(label="", value=file)
1113
+ with col2:
1114
+ if st.button("๐Ÿ—‘", key="delete_" + file):
1115
+ os.remove(file)
1116
+ st.experimental_rerun()
1117
+
1118
+
1119
+
1120
+ # Feedback
1121
+ # Step: Give User a Way to Upvote or Downvote
1122
+ GiveFeedback=False
1123
+ if GiveFeedback:
1124
+ with st.expander("Give your feedback ๐Ÿ‘", expanded=False):
1125
+
1126
+ feedback = st.radio("Step 8: Give your feedback", ("๐Ÿ‘ Upvote", "๐Ÿ‘Ž Downvote"))
1127
+ if feedback == "๐Ÿ‘ Upvote":
1128
+ st.write("You upvoted ๐Ÿ‘. Thank you for your feedback!")
1129
+ else:
1130
+ st.write("You downvoted ๐Ÿ‘Ž. Thank you for your feedback!")
1131
+
1132
+ load_dotenv()
1133
+ st.write(css, unsafe_allow_html=True)
1134
+ st.header("Chat with documents :books:")
1135
+ user_question = st.text_input("Ask a question about your documents:")
1136
+ if user_question:
1137
+ process_user_input(user_question)
1138
+ with st.sidebar:
1139
+ st.subheader("Your documents")
1140
+ docs = st.file_uploader("import documents", accept_multiple_files=True)
1141
+ with st.spinner("Processing"):
1142
+ raw = pdf2txt(docs)
1143
+ if len(raw) > 0:
1144
+ length = str(len(raw))
1145
+ text_chunks = txt2chunks(raw)
1146
+ vectorstore = vector_store(text_chunks)
1147
+ st.session_state.conversation = get_chain(vectorstore)
1148
+ st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
1149
+ filename = generate_filename(raw, 'txt')
1150
+ create_file(filename, raw, '', should_save)
1151
+
1152
+ # Relocated! Hope you like your new space - enjoy!
1153
+ # Display instructions and handle query parameters
1154
+ st.markdown("## Glossary Lookup\nEnter a term in the URL query, like `?q=Nanotechnology` or `?query=Martian Syndicate`.")
1155
+ try:
1156
+ query_params = st.query_params
1157
+ #query = (query_params.get('q') or query_params.get('query') or [''])[0]
1158
+ query = (query_params.get('q') or query_params.get('query') or [''])
1159
+ st.markdown('# Running query: ' + query)
1160
+ if query: search_glossary(query)
1161
+ except:
1162
+ st.markdown('No glossary lookup')
1163
+
1164
+ # Display the glossary grid
1165
+ st.title("Transhuman Space Glossary ๐ŸŒŒ")
1166
+ display_glossary_grid(transhuman_glossary)
1167
+
1168
+ st.title("๐ŸŒŒ๐Ÿš€ Transhuman Space Encyclopedia")
1169
+ st.markdown("## Explore the universe of Transhuman Space through interactive storytelling and encyclopedic knowledge.๐ŸŒ ")
1170
+
1171
+ display_buttons_with_scores()
1172
+
1173
+ display_images_and_wikipedia_summaries()
1174
+
1175
+ # Assuming the transhuman_glossary and other setup code remains the same
1176
+ #st.write("Current Query Parameters:", st.query_params)
1177
+ #st.markdown("### Query Parameters - These Deep Link Map to Remixable Methods, Navigate or Trigger Functionalities")
1178
+
1179
+ # Example: Using query parameters to navigate or trigger functionalities
1180
+ if 'action' in st.query_params:
1181
+ action = st.query_params()['action'][0] # Get the first (or only) 'action' parameter
1182
+ if action == 'show_message':
1183
+ st.success("Showing a message because 'action=show_message' was found in the URL.")
1184
+ elif action == 'clear':
1185
+ clear_query_params()
1186
+ st.experimental_rerun()
1187
+
1188
+ # Handling repeated keys
1189
+ if 'multi' in st.query_params:
1190
+ multi_values = get_all_query_params('multi')
1191
+ st.write("Values for 'multi':", multi_values)
1192
+
1193
+ # Manual entry for demonstration
1194
+ st.write("Enter query parameters in the URL like this: ?action=show_message&multi=1&multi=2")
1195
+
1196
+ if 'query' in st.query_params:
1197
+ query = st.query_params['query'][0] # Get the query parameter
1198
+ # Display content or image based on the query
1199
+ display_content_or_image(query)
1200
+
1201
+ # Add a clear query parameters button for convenience
1202
+ if st.button("Clear Query Parameters", key='ClearQueryParams'):
1203
+ # This will clear the browser URL's query parameters
1204
+ st.experimental_set_query_params
1205
+ st.experimental_rerun()
1206
+
1207
+ # 18. Run AI Pipeline
1208
+ if __name__ == "__main__":
1209
+ whisper_main()
1210
+ main()