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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import glob
|
4 |
+
import re
|
5 |
+
from urllib.parse import quote
|
6 |
+
from gradio_client import Client
|
7 |
+
import json
|
8 |
+
from datetime import datetime
|
9 |
+
|
10 |
+
# Initialize session state variables
|
11 |
+
if 'selected_file' not in st.session_state:
|
12 |
+
st.session_state.selected_file = None
|
13 |
+
if 'view_mode' not in st.session_state:
|
14 |
+
st.session_state.view_mode = 'view'
|
15 |
+
if 'files' not in st.session_state:
|
16 |
+
st.session_state.files = []
|
17 |
+
|
18 |
+
# Define the markdown variables
|
19 |
+
Boxing_and_MMA_Commentary_and_Knowledge = """
|
20 |
+
# Boxing and UFC Study of 1971 - 2024 The Greatest Fights History
|
21 |
+
|
22 |
+
1. In Boxing, the most heart breaking fight in Boxing was the Boom Boom Mancini fight with Duku Kim.
|
23 |
+
2. After changes to Boxing made it more safe due to the heart break.
|
24 |
+
3. Rehydration of the brain after weight ins loss preparation for a match is life saving change.
|
25 |
+
4. Fighting went from 15 rounds to 12.
|
26 |
+
|
27 |
+
# UFC By Contrast..
|
28 |
+
1. 5 Rounds of 5 Minutes each.
|
29 |
+
2. Greatest UFC Fighters:
|
30 |
+
- Jon Jones could be the greatest of all time (GOAT) since he never lost.
|
31 |
+
- George St. Pierre
|
32 |
+
- BJ Penn
|
33 |
+
- Anderson Silva
|
34 |
+
- Mighty Mouse MMA's heart at 125 pounds
|
35 |
+
- Kabib retired 29 and 0
|
36 |
+
- Fedor Milliano
|
37 |
+
- Alex Pereira
|
38 |
+
- James Tony
|
39 |
+
- Randy Couture
|
40 |
+
3. You have to Judge them in their Championship Peak
|
41 |
+
4. Chris Weidman
|
42 |
+
5. Connor McGregor
|
43 |
+
6. Leg Breaking - Shin calcification and breaking baseball bats
|
44 |
+
|
45 |
+
# References:
|
46 |
+
1. Joe Rogan - Interview #2219
|
47 |
+
2. Donald J Trump
|
48 |
+
"""
|
49 |
+
|
50 |
+
Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds = """
|
51 |
+
# Multiplayer Simulated Worlds
|
52 |
+
|
53 |
+
1. 7 Days To Die PC
|
54 |
+
2. ARK: Survival Evolved PC
|
55 |
+
3. Arma 3 PC
|
56 |
+
4. Atlas PC
|
57 |
+
5. Conan Exiles PC
|
58 |
+
6. Craftopia PC
|
59 |
+
7. DayZ PC
|
60 |
+
8. Eco - Global Survival PC
|
61 |
+
9. Empyrion - Galactic Survival PC
|
62 |
+
10. Factorio PC
|
63 |
+
11. Farming Simulator 19 PC
|
64 |
+
12. Crossplay
|
65 |
+
13. Farming Simulator 22
|
66 |
+
14. Last Oasis PC
|
67 |
+
15. Last Oasis Classic PC
|
68 |
+
16. Minecraft (Vanilla) PC
|
69 |
+
17. Crossplay
|
70 |
+
18. Path of Titans
|
71 |
+
19. Rust PC
|
72 |
+
20. SCP: Secret Laboratory PC
|
73 |
+
21. SCUM PC
|
74 |
+
22. Satisfactory PC
|
75 |
+
23. Satisfactory (Experimental) PC
|
76 |
+
24. Crossplay
|
77 |
+
25. Space Engineers
|
78 |
+
26. Terraria (tShock & Vanilla) PC
|
79 |
+
27. The Forest PC
|
80 |
+
28. Crossplay
|
81 |
+
29. Valheim
|
82 |
+
"""
|
83 |
+
|
84 |
+
# Function to parse markdown text and extract terms
|
85 |
+
def extract_terms(markdown_text):
|
86 |
+
lines = markdown_text.strip().split('\n')
|
87 |
+
terms = []
|
88 |
+
for line in lines:
|
89 |
+
line = re.sub(r'^[#*\->\d\.\s]+', '', line).strip()
|
90 |
+
if line:
|
91 |
+
terms.append(line)
|
92 |
+
return terms
|
93 |
+
|
94 |
+
# Function to display terms with links
|
95 |
+
def display_terms_with_links(terms):
|
96 |
+
search_urls = {
|
97 |
+
"🚀🌌ArXiv": lambda k: f"/?q={quote(k)}",
|
98 |
+
"📖": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
|
99 |
+
"🔍": lambda k: f"https://www.google.com/search?q={quote(k)}",
|
100 |
+
"▶️": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
|
101 |
+
"🔎": lambda k: f"https://www.bing.com/search?q={quote(k)}",
|
102 |
+
"🐦": lambda k: f"https://twitter.com/search?q={quote(k)}",
|
103 |
+
}
|
104 |
+
for term in terms:
|
105 |
+
links_md = ' '.join([f"[{emoji}]({url(term)})" for emoji, url in search_urls.items()])
|
106 |
+
st.markdown(f"- **{term}** {links_md}", unsafe_allow_html=True)
|
107 |
+
|
108 |
+
# Function to perform AI lookup using Gradio client
|
109 |
+
def perform_ai_lookup(query):
|
110 |
+
st.write("Performing AI Lookup...")
|
111 |
+
# Initialize the Gradio client
|
112 |
+
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
113 |
+
# Perform the AI lookup using the Mixtral and Mistral models
|
114 |
+
result1 = client.predict(
|
115 |
+
prompt=query,
|
116 |
+
llm_model_picked="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
117 |
+
stream_outputs=True,
|
118 |
+
api_name="/ask_llm"
|
119 |
+
)
|
120 |
+
st.markdown("### Mixtral-8x7B-Instruct-v0.1 Result")
|
121 |
+
st.markdown(result1)
|
122 |
+
result2 = client.predict(
|
123 |
+
prompt=query,
|
124 |
+
llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
|
125 |
+
stream_outputs=True,
|
126 |
+
api_name="/ask_llm"
|
127 |
+
)
|
128 |
+
st.markdown("### Mistral-7B-Instruct-v0.2 Result")
|
129 |
+
st.markdown(result2)
|
130 |
+
combined_result = f"{result1}\n\n{result2}"
|
131 |
+
return combined_result
|
132 |
+
|
133 |
+
# Function to extract URLs from AI result
|
134 |
+
def extract_urls(text):
|
135 |
+
try:
|
136 |
+
date_pattern = re.compile(r'### (\d{2} \w{3} \d{4})')
|
137 |
+
abs_link_pattern = re.compile(r'\[(.*?)\]\((https://arxiv\.org/abs/\d+\.\d+)\)')
|
138 |
+
pdf_link_pattern = re.compile(r'\[⬇️\]\((https://arxiv\.org/pdf/\d+\.\d+)\)')
|
139 |
+
title_pattern = re.compile(r'### \d{2} \w{3} \d{4} \| \[(.*?)\]')
|
140 |
+
date_matches = date_pattern.findall(text)
|
141 |
+
abs_link_matches = abs_link_pattern.findall(text)
|
142 |
+
pdf_link_matches = pdf_link_pattern.findall(text)
|
143 |
+
title_matches = title_pattern.findall(text)
|
144 |
+
|
145 |
+
markdown_text = ""
|
146 |
+
for i in range(len(date_matches)):
|
147 |
+
date = date_matches[i]
|
148 |
+
title = title_matches[i]
|
149 |
+
abs_link = abs_link_matches[i][1]
|
150 |
+
pdf_link = pdf_link_matches[i]
|
151 |
+
markdown_text += f"**Date:** {date}\n\n"
|
152 |
+
markdown_text += f"**Title:** {title}\n\n"
|
153 |
+
markdown_text += f"**Abstract Link:** [{abs_link}]({abs_link})\n\n"
|
154 |
+
markdown_text += f"**PDF Link:** [{pdf_link}]({pdf_link})\n\n"
|
155 |
+
markdown_text += "---\n\n"
|
156 |
+
return markdown_text
|
157 |
+
|
158 |
+
except Exception as e:
|
159 |
+
st.write(f"An error occurred in extract_urls: {e}")
|
160 |
+
return ''
|
161 |
+
|
162 |
+
# Function to generate filename based on date and content
|
163 |
+
def generate_filename(prefix, content):
|
164 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
165 |
+
safe_content = re.sub(r'\W+', '_', content[:50])
|
166 |
+
filename = f"{prefix}_{timestamp}_{safe_content}.md"
|
167 |
+
return filename
|
168 |
+
|
169 |
+
# Sidebar for file management
|
170 |
+
def file_management_sidebar():
|
171 |
+
st.sidebar.title("📁 File Management")
|
172 |
+
|
173 |
+
# Get list of .md files excluding README.md
|
174 |
+
md_files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
|
175 |
+
md_files.sort()
|
176 |
+
st.session_state.files = md_files # Update session state
|
177 |
+
|
178 |
+
if md_files:
|
179 |
+
st.sidebar.markdown("### Markdown Files")
|
180 |
+
for idx, file in enumerate(md_files):
|
181 |
+
# Create a unique key for each file
|
182 |
+
key_base = f"file_{idx}_{file}"
|
183 |
+
col1, col2, col3 = st.sidebar.columns([6, 1, 1])
|
184 |
+
with col1:
|
185 |
+
st.write(file)
|
186 |
+
with col2:
|
187 |
+
if st.sidebar.button("📄", key=f"view_{key_base}"):
|
188 |
+
st.session_state.selected_file = file
|
189 |
+
st.session_state.view_mode = 'view'
|
190 |
+
with col3:
|
191 |
+
if st.sidebar.button("✏️", key=f"edit_{key_base}"):
|
192 |
+
st.session_state.selected_file = file
|
193 |
+
st.session_state.view_mode = 'edit'
|
194 |
+
# Option to create a new markdown file
|
195 |
+
if st.sidebar.button("Create New Markdown File"):
|
196 |
+
# Generate automatic filename
|
197 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
198 |
+
new_filename = f"note_{timestamp}.md"
|
199 |
+
with open(new_filename, 'w', encoding='utf-8') as f:
|
200 |
+
f.write("# New Markdown File\n")
|
201 |
+
st.sidebar.success(f"Created new file: {new_filename}")
|
202 |
+
st.session_state.selected_file = new_filename
|
203 |
+
st.session_state.view_mode = 'edit'
|
204 |
+
else:
|
205 |
+
st.sidebar.write("No markdown files found.")
|
206 |
+
if st.sidebar.button("Create New Markdown File"):
|
207 |
+
# Generate automatic filename
|
208 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
209 |
+
new_filename = f"note_{timestamp}.md"
|
210 |
+
with open(new_filename, 'w', encoding='utf-8') as f:
|
211 |
+
f.write("# New Markdown File\n")
|
212 |
+
st.sidebar.success(f"Created new file: {new_filename}")
|
213 |
+
st.session_state.selected_file = new_filename
|
214 |
+
st.session_state.view_mode = 'edit'
|
215 |
+
|
216 |
+
|
217 |
+
# Main application logic
|
218 |
+
def main():
|
219 |
+
st.title("Markdown Content with AI Lookup and File Management")
|
220 |
+
|
221 |
+
# Display the original markdown content
|
222 |
+
st.markdown("## Original Markdown Content")
|
223 |
+
st.markdown(Boxing_and_MMA_Commentary_and_Knowledge)
|
224 |
+
st.markdown(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
|
225 |
+
|
226 |
+
# Parse and display terms with links
|
227 |
+
st.markdown("## Terms with Links")
|
228 |
+
terms1 = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge)
|
229 |
+
terms2 = extract_terms(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
|
230 |
+
all_terms = terms1 + terms2
|
231 |
+
display_terms_with_links(all_terms)
|
232 |
+
|
233 |
+
# Process 'q' query parameter from the URL
|
234 |
+
try:
|
235 |
+
query_params = st.query_params
|
236 |
+
query = (query_params.get('q') or query_params.get('query') or [''])
|
237 |
+
if len(query) > 1:
|
238 |
+
st.write(f"### Search query received: {query}")
|
239 |
+
ai_result = perform_ai_lookup(query)
|
240 |
+
markdown_text = extract_urls(ai_result)
|
241 |
+
st.markdown("## Extracted URLs")
|
242 |
+
st.markdown(markdown_text)
|
243 |
+
filename = generate_filename(ai_result, query)
|
244 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
245 |
+
f.write(markdown_text)
|
246 |
+
st.write(f"Generated file **{filename}** with AI results for {query}")
|
247 |
+
st.query_params.clear()
|
248 |
+
st.session_state.selected_file = filename
|
249 |
+
st.session_state.view_mode = 'view'
|
250 |
+
|
251 |
+
except Exception as e:
|
252 |
+
st.write(f"An error occurred while processing query parameters: {e}")
|
253 |
+
|
254 |
+
# Handle 'action' and 'query' parameters
|
255 |
+
params = st.query_params
|
256 |
+
if 'action' in params:
|
257 |
+
action = st.query_params()['action'][0]
|
258 |
+
if action == 'show_message':
|
259 |
+
st.success("Showing a message because 'action=show_message' was found in the URL.")
|
260 |
+
elif action == 'clear':
|
261 |
+
# Clear query parameters
|
262 |
+
st.query_params.clear()
|
263 |
+
|
264 |
+
if 'query' in st.query_params:
|
265 |
+
query = st.query_params['query'][0] # Get the query parameter
|
266 |
+
# Display content or image based on the query
|
267 |
+
#display_content_or_image(query)
|
268 |
+
st.markdown("To Implement:")
|
269 |
+
st.markdown(query)
|
270 |
+
|
271 |
+
# File management sidebar
|
272 |
+
file_management_sidebar()
|
273 |
+
|
274 |
+
# Display the selected file
|
275 |
+
selected_file = st.session_state.get('selected_file')
|
276 |
+
if selected_file:
|
277 |
+
view_mode = st.session_state.get('view_mode', 'view')
|
278 |
+
if os.path.exists(selected_file):
|
279 |
+
if view_mode == 'view':
|
280 |
+
st.markdown(f"### Viewing {selected_file}")
|
281 |
+
with open(selected_file, 'r', encoding='utf-8') as f:
|
282 |
+
file_content = f.read()
|
283 |
+
st.markdown(file_content)
|
284 |
+
elif view_mode == 'edit':
|
285 |
+
st.markdown(f"### Editing {selected_file}")
|
286 |
+
with open(selected_file, 'r', encoding='utf-8') as f:
|
287 |
+
file_content = f.read()
|
288 |
+
edited_content = st.text_area("Edit the markdown content", file_content, height=400)
|
289 |
+
if st.button("Save Changes"):
|
290 |
+
with open(selected_file, 'w', encoding='utf-8') as f:
|
291 |
+
f.write(edited_content)
|
292 |
+
st.success(f"Changes saved to {selected_file}")
|
293 |
+
# Update the file list in session state
|
294 |
+
st.session_state.files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
|
295 |
+
else:
|
296 |
+
st.error("Selected file does not exist.")
|
297 |
+
else:
|
298 |
+
st.info("No file selected.")
|
299 |
+
|
300 |
+
if __name__ == "__main__":
|
301 |
+
main()
|