awacke1 commited on
Commit
67609ca
·
verified ·
1 Parent(s): 5660e07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -54
app.py CHANGED
@@ -2,7 +2,7 @@ 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
@@ -12,6 +12,8 @@ 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
 
16
  # Define the markdown variables
17
  Boxing_and_MMA_Commentary_and_Knowledge = """
@@ -91,13 +93,8 @@ def extract_terms(markdown_text):
91
 
92
  # Function to display terms with links
93
  def display_terms_with_links(terms):
94
- def generate_arxiv_link(term):
95
- params = {'term': term}
96
- query_string = urlencode(params)
97
- return f"./?{query_string}"
98
-
99
  search_urls = {
100
- "🚀🌌ArXiv": lambda k: generate_arxiv_link(k),
101
  "📖": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
102
  "🔍": lambda k: f"https://www.google.com/search?q={quote(k)}",
103
  "▶️": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
@@ -109,13 +106,13 @@ def display_terms_with_links(terms):
109
  st.markdown(f"- **{term}** {links_md}", unsafe_allow_html=True)
110
 
111
  # Function to perform AI lookup using Gradio client
112
- def perform_ai_lookup(term):
113
  st.write("Performing AI Lookup...")
114
  # Initialize the Gradio client
115
  client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
116
  # Perform the AI lookup using the Mixtral and Mistral models
117
  result1 = client.predict(
118
- prompt=term,
119
  llm_model_picked="mistralai/Mixtral-8x7B-Instruct-v0.1",
120
  stream_outputs=True,
121
  api_name="/ask_llm"
@@ -123,7 +120,7 @@ def perform_ai_lookup(term):
123
  st.markdown("### Mixtral-8x7B-Instruct-v0.1 Result")
124
  st.markdown(result1)
125
  result2 = client.predict(
126
- prompt=term,
127
  llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
128
  stream_outputs=True,
129
  api_name="/ask_llm"
@@ -163,38 +160,12 @@ def extract_urls(text):
163
  return ''
164
 
165
  # Function to generate filename based on date and content
166
- def generate_filename(prefix, content, ai_output):
167
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
168
  safe_content = re.sub(r'\W+', '_', content[:50])
169
- # Get first few words of AI output
170
- ai_snippet = ' '.join(ai_output.strip().split()[:5])
171
- safe_ai_snippet = re.sub(r'\W+', '_', ai_snippet)
172
- filename = f"{prefix}_{timestamp}_{safe_content}_{safe_ai_snippet}.md"
173
  return filename
174
 
175
- # Function to process query parameters and handle AI lookup
176
- def process_query_parameters():
177
- term = st.query_params.get('term')
178
- if term:
179
- if len(term) > 1:
180
- st.write(f"### Search term received: {term}")
181
- # Perform AI lookup
182
- ai_result = perform_ai_lookup(term)
183
- # Extract URLs from AI result
184
- markdown_text = extract_urls(ai_result)
185
- st.markdown("## Extracted URLs")
186
- st.markdown(markdown_text)
187
- # Save the result as markdown file
188
- filename = generate_filename("AI_Result", term, ai_result)
189
- with open(filename, 'w', encoding='utf-8') as f:
190
- f.write(markdown_text)
191
- st.write(f"Generated file **{filename}** with AI lookup results.")
192
- # Clear the query parameters
193
- st.query_params.clear()
194
- # Update session state
195
- st.session_state.selected_file = filename
196
- st.session_state.view_mode = 'view'
197
-
198
  # Sidebar for file management
199
  def file_management_sidebar():
200
  st.sidebar.title("📁 File Management")
@@ -202,6 +173,7 @@ def file_management_sidebar():
202
  # Get list of .md files excluding README.md
203
  md_files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
204
  md_files.sort()
 
205
 
206
  if md_files:
207
  st.sidebar.markdown("### Markdown Files")
@@ -245,9 +217,6 @@ def file_management_sidebar():
245
  def main():
246
  st.title("Markdown Content with AI Lookup and File Management")
247
 
248
- # Process query parameters for AI lookup
249
- process_query_parameters()
250
-
251
  # Display the original markdown content
252
  st.markdown("## Original Markdown Content")
253
  st.markdown(Boxing_and_MMA_Commentary_and_Knowledge)
@@ -260,20 +229,51 @@ def main():
260
  all_terms = terms1 + terms2
261
  display_terms_with_links(all_terms)
262
 
263
- # Handle 'action' and 'query' parameters (if needed)
264
- params = st.query_params
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  if 'action' in params:
266
- action = params['action']
267
- if action == 'show_message':
268
- st.success("Showing a message because 'action=show_message' was found in the URL.")
269
- elif action == 'clear':
270
- # Clear query parameters
271
- st.query_params.clear()
 
 
272
  if 'query' in params:
273
- query = params['query']
274
- # Display content or image based on the query
275
- st.write(f"Displaying content for query: {query}")
276
- # Implement your display logic here
 
 
277
 
278
  # File management sidebar
279
  file_management_sidebar()
@@ -297,10 +297,12 @@ def main():
297
  with open(selected_file, 'w', encoding='utf-8') as f:
298
  f.write(edited_content)
299
  st.success(f"Changes saved to {selected_file}")
 
 
300
  else:
301
  st.error("Selected file does not exist.")
302
  else:
303
  st.info("No file selected.")
304
 
305
  if __name__ == "__main__":
306
- main()
 
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
 
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 = """
 
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)}",
 
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"
 
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"
 
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")
 
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")
 
217
  def main():
218
  st.title("Markdown Content with AI Lookup and File Management")
219
 
 
 
 
220
  # Display the original markdown content
221
  st.markdown("## Original Markdown Content")
222
  st.markdown(Boxing_and_MMA_Commentary_and_Knowledge)
 
229
  all_terms = terms1 + terms2
230
  display_terms_with_links(all_terms)
231
 
232
+ # Process 'q' query parameter from the URL
233
+ try:
234
+ query_params = st.experimental_get_query_params()
235
+ query_list = query_params.get('q') or query_params.get('query') or []
236
+ if query_list:
237
+ search_query = query_list[0]
238
+ if len(search_query) > 1:
239
+ st.write(f"### Search query received: {search_query}")
240
+ # Perform AI lookup
241
+ ai_result = perform_ai_lookup(search_query)
242
+ # Extract URLs from AI result
243
+ markdown_text = extract_urls(ai_result)
244
+ st.markdown("## Extracted URLs")
245
+ st.markdown(markdown_text)
246
+ # Save the result as markdown file
247
+ filename = generate_filename("AI_Result", search_query)
248
+ with open(filename, 'w', encoding='utf-8') as f:
249
+ f.write(markdown_text)
250
+ st.write(f"Generated file **{filename}** with AI lookup results.")
251
+ # Clear the query parameters
252
+ st.experimental_set_query_params()
253
+ # Update session state
254
+ st.session_state.selected_file = filename
255
+ st.session_state.view_mode = 'view'
256
+ except Exception as e:
257
+ st.write(f"An error occurred while processing query parameters: {e}")
258
+
259
+ # Handle 'action' and 'query' parameters
260
+ params = st.experimental_get_query_params()
261
  if 'action' in params:
262
+ action_list = params['action']
263
+ if action_list:
264
+ action = action_list[0]
265
+ if action == 'show_message':
266
+ st.success("Showing a message because 'action=show_message' was found in the URL.")
267
+ elif action == 'clear':
268
+ # Clear query parameters
269
+ st.experimental_set_query_params()
270
  if 'query' in params:
271
+ query_list = params['query']
272
+ if query_list:
273
+ query = query_list[0]
274
+ # Display content or image based on the query
275
+ st.write(f"Displaying content for query: {query}")
276
+ # Implement your display logic here
277
 
278
  # File management sidebar
279
  file_management_sidebar()
 
297
  with open(selected_file, 'w', encoding='utf-8') as f:
298
  f.write(edited_content)
299
  st.success(f"Changes saved to {selected_file}")
300
+ # Update the file list in session state
301
+ st.session_state.files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
302
  else:
303
  st.error("Selected file does not exist.")
304
  else:
305
  st.info("No file selected.")
306
 
307
  if __name__ == "__main__":
308
+ main()