awacke1 commited on
Commit
2e679f3
·
verified ·
1 Parent(s): 6531bd6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +318 -0
app.py ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import glob
4
+ import re
5
+ from urllib.parse import quote, urlencode
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
+
46
+ # References:
47
+ 1. Joe Rogan - Interview #2219
48
+ 2. Donald J Trump
49
+ """
50
+
51
+ Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds = """
52
+ # Multiplayer Simulated Worlds
53
+
54
+ 1. 7 Days To Die PC
55
+ 2. ARK: Survival Evolved PC
56
+ 3. Arma 3 PC
57
+ 4. Atlas PC
58
+ 5. Conan Exiles PC
59
+ 6. Craftopia PC
60
+ 7. DayZ PC
61
+ 8. Eco - Global Survival PC
62
+ 9. Empyrion - Galactic Survival PC
63
+ 10. Factorio PC
64
+ 11. Farming Simulator 19 PC
65
+ 12. Crossplay
66
+ 13. Farming Simulator 22
67
+ 14. Last Oasis PC
68
+ 15. Last Oasis Classic PC
69
+ 16. Minecraft (Vanilla) PC
70
+ 17. Crossplay
71
+ 18. Path of Titans
72
+ 19. Rust PC
73
+ 20. SCP: Secret Laboratory PC
74
+ 21. SCUM PC
75
+ 22. Satisfactory PC
76
+ 23. Satisfactory (Experimental) PC
77
+ 24. Crossplay
78
+ 25. Space Engineers
79
+ 26. Terraria (tShock & Vanilla) PC
80
+ 27. The Forest PC
81
+ 28. Crossplay
82
+ 29. Valheim
83
+ """
84
+
85
+ # Function to parse markdown text and extract terms
86
+ def extract_terms(markdown_text):
87
+ lines = markdown_text.strip().split('\n')
88
+ terms = []
89
+ for line in lines:
90
+ line = re.sub(r'^[#*\->\d\.\s]+', '', line).strip()
91
+ if line:
92
+ terms.append(line)
93
+ return terms
94
+
95
+ # Function to display terms with links
96
+ def display_terms_with_links(terms):
97
+ def generate_arxiv_link(term):
98
+ params = {'term': term}
99
+ url = f"/?{urlencode(params)}"
100
+ return url
101
+
102
+ search_urls = {
103
+ "🚀🌌ArXiv": lambda k: generate_arxiv_link(k),
104
+ "📖": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
105
+ "🔍": lambda k: f"https://www.google.com/search?q={quote(k)}",
106
+ "▶️": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
107
+ "🔎": lambda k: f"https://www.bing.com/search?q={quote(k)}",
108
+ "🐦": lambda k: f"https://twitter.com/search?q={quote(k)}",
109
+ }
110
+ for term in terms:
111
+ links_md = ' '.join([f"[{emoji}]({url(term)})" for emoji, url in search_urls.items()])
112
+ st.markdown(f"- **{term}** {links_md}", unsafe_allow_html=True)
113
+
114
+ # Function to perform AI lookup using Gradio client
115
+ def perform_ai_lookup(term):
116
+ st.write("Performing AI Lookup...")
117
+ # Initialize the Gradio client
118
+ client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
119
+ # Perform the AI lookup using the Mixtral and Mistral models
120
+ result1 = client.predict(
121
+ prompt=term,
122
+ llm_model_picked="mistralai/Mixtral-8x7B-Instruct-v0.1",
123
+ stream_outputs=True,
124
+ api_name="/ask_llm"
125
+ )
126
+ st.markdown("### Mixtral-8x7B-Instruct-v0.1 Result")
127
+ st.markdown(result1)
128
+ result2 = client.predict(
129
+ prompt=term,
130
+ llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
131
+ stream_outputs=True,
132
+ api_name="/ask_llm"
133
+ )
134
+ st.markdown("### Mistral-7B-Instruct-v0.2 Result")
135
+ st.markdown(result2)
136
+ combined_result = f"{result1}\n\n{result2}"
137
+ return combined_result
138
+
139
+ # Function to extract URLs from AI result
140
+ def extract_urls(text):
141
+ try:
142
+ date_pattern = re.compile(r'### (\d{2} \w{3} \d{4})')
143
+ abs_link_pattern = re.compile(r'\[(.*?)\]\((https://arxiv\.org/abs/\d+\.\d+)\)')
144
+ pdf_link_pattern = re.compile(r'\[⬇️\]\((https://arxiv\.org/pdf/\d+\.\d+)\)')
145
+ title_pattern = re.compile(r'### \d{2} \w{3} \d{4} \| \[(.*?)\]')
146
+ date_matches = date_pattern.findall(text)
147
+ abs_link_matches = abs_link_pattern.findall(text)
148
+ pdf_link_matches = pdf_link_pattern.findall(text)
149
+ title_matches = title_pattern.findall(text)
150
+
151
+ markdown_text = ""
152
+ for i in range(len(date_matches)):
153
+ date = date_matches[i]
154
+ title = title_matches[i]
155
+ abs_link = abs_link_matches[i][1]
156
+ pdf_link = pdf_link_matches[i]
157
+ markdown_text += f"**Date:** {date}\n\n"
158
+ markdown_text += f"**Title:** {title}\n\n"
159
+ markdown_text += f"**Abstract Link:** [{abs_link}]({abs_link})\n\n"
160
+ markdown_text += f"**PDF Link:** [{pdf_link}]({pdf_link})\n\n"
161
+ markdown_text += "---\n\n"
162
+ return markdown_text
163
+
164
+ except Exception as e:
165
+ st.write(f"An error occurred in extract_urls: {e}")
166
+ return ''
167
+
168
+ # Function to generate filename based on date and content
169
+ def generate_filename(prefix, content, ai_output):
170
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
171
+ safe_content = re.sub(r'\W+', '_', content[:50])
172
+ # Get first few words of AI output
173
+ ai_snippet = ' '.join(ai_output.strip().split()[:5])
174
+ safe_ai_snippet = re.sub(r'\W+', '_', ai_snippet)
175
+ filename = f"{prefix}_{timestamp}_{safe_content}_{safe_ai_snippet}.md"
176
+ return filename
177
+
178
+ # Function to process query parameters and handle AI lookup
179
+ def process_query_parameters():
180
+ query_params = st.query_params
181
+ term = query_params.get('term', None)
182
+ if term:
183
+ term = term[0] # Get the first value
184
+ if len(term) > 1:
185
+ st.write(f"### Search term received: {term}")
186
+ # Perform AI lookup
187
+ ai_result = perform_ai_lookup(term)
188
+ # Extract URLs from AI result
189
+ markdown_text = extract_urls(ai_result)
190
+ st.markdown("## Extracted URLs")
191
+ st.markdown(markdown_text)
192
+ # Save the result as markdown file
193
+ filename = generate_filename("AI_Result", term, ai_result)
194
+ with open(filename, 'w', encoding='utf-8') as f:
195
+ f.write(markdown_text)
196
+ st.write(f"Generated file **{filename}** with AI lookup results.")
197
+ # Clear the query parameters
198
+ st.set_query_params()
199
+ # Update session state
200
+ st.session_state.selected_file = filename
201
+ st.session_state.view_mode = 'view'
202
+
203
+ # Sidebar for file management
204
+ def file_management_sidebar():
205
+ st.sidebar.title("📁 File Management")
206
+
207
+ # Get list of .md files excluding README.md
208
+ md_files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
209
+ md_files.sort()
210
+ st.session_state.files = md_files # Update session state
211
+
212
+ if md_files:
213
+ st.sidebar.markdown("### Markdown Files")
214
+ for idx, file in enumerate(md_files):
215
+ # Create a unique key for each file
216
+ key_base = f"file_{idx}_{file}"
217
+ col1, col2, col3 = st.sidebar.columns([6, 1, 1])
218
+ with col1:
219
+ st.write(file)
220
+ with col2:
221
+ if st.sidebar.button("📄", key=f"view_{key_base}"):
222
+ st.session_state.selected_file = file
223
+ st.session_state.view_mode = 'view'
224
+ with col3:
225
+ if st.sidebar.button("✏️", key=f"edit_{key_base}"):
226
+ st.session_state.selected_file = file
227
+ st.session_state.view_mode = 'edit'
228
+ # Option to create a new markdown file
229
+ if st.sidebar.button("Create New Markdown File"):
230
+ # Generate automatic filename
231
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
232
+ new_filename = f"note_{timestamp}.md"
233
+ with open(new_filename, 'w', encoding='utf-8') as f:
234
+ f.write("# New Markdown File\n")
235
+ st.sidebar.success(f"Created new file: {new_filename}")
236
+ st.session_state.selected_file = new_filename
237
+ st.session_state.view_mode = 'edit'
238
+ else:
239
+ st.sidebar.write("No markdown files found.")
240
+ if st.sidebar.button("Create New Markdown File"):
241
+ # Generate automatic filename
242
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
243
+ new_filename = f"note_{timestamp}.md"
244
+ with open(new_filename, 'w', encoding='utf-8') as f:
245
+ f.write("# New Markdown File\n")
246
+ st.sidebar.success(f"Created new file: {new_filename}")
247
+ st.session_state.selected_file = new_filename
248
+ st.session_state.view_mode = 'edit'
249
+
250
+ # Main application logic
251
+ def main():
252
+ st.title("Markdown Content with AI Lookup and File Management")
253
+
254
+ # Process query parameters for AI lookup
255
+ process_query_parameters()
256
+
257
+ # Display the original markdown content
258
+ st.markdown("## Original Markdown Content")
259
+ st.markdown(Boxing_and_MMA_Commentary_and_Knowledge)
260
+ st.markdown(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
261
+
262
+ # Parse and display terms with links
263
+ st.markdown("## Terms with Links")
264
+ terms1 = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge)
265
+ terms2 = extract_terms(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
266
+ all_terms = terms1 + terms2
267
+ display_terms_with_links(all_terms)
268
+
269
+ # Handle 'action' and 'query' parameters (if needed)
270
+ params = st.query_params
271
+ if 'action' in params:
272
+ action_list = params['action']
273
+ if action_list:
274
+ action = action_list[0]
275
+ if action == 'show_message':
276
+ st.success("Showing a message because 'action=show_message' was found in the URL.")
277
+ elif action == 'clear':
278
+ # Clear query parameters
279
+ st.set_query_params()
280
+ if 'query' in params:
281
+ query_list = params['query']
282
+ if query_list:
283
+ query = query_list[0]
284
+ # Display content or image based on the query
285
+ st.write(f"Displaying content for query: {query}")
286
+ # Implement your display logic here
287
+
288
+ # File management sidebar
289
+ file_management_sidebar()
290
+
291
+ # Display the selected file
292
+ selected_file = st.session_state.get('selected_file')
293
+ if selected_file:
294
+ view_mode = st.session_state.get('view_mode', 'view')
295
+ if os.path.exists(selected_file):
296
+ if view_mode == 'view':
297
+ st.markdown(f"### Viewing {selected_file}")
298
+ with open(selected_file, 'r', encoding='utf-8') as f:
299
+ file_content = f.read()
300
+ st.markdown(file_content)
301
+ elif view_mode == 'edit':
302
+ st.markdown(f"### Editing {selected_file}")
303
+ with open(selected_file, 'r', encoding='utf-8') as f:
304
+ file_content = f.read()
305
+ edited_content = st.text_area("Edit the markdown content", file_content, height=400)
306
+ if st.button("Save Changes"):
307
+ with open(selected_file, 'w', encoding='utf-8') as f:
308
+ f.write(edited_content)
309
+ st.success(f"Changes saved to {selected_file}")
310
+ # Update the file list in session state
311
+ st.session_state.files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
312
+ else:
313
+ st.error("Selected file does not exist.")
314
+ else:
315
+ st.info("No file selected.")
316
+
317
+ if __name__ == "__main__":
318
+ main()