awacke1 commited on
Commit
f02599c
Β·
verified Β·
1 Parent(s): 9deb822

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -133
app.py CHANGED
@@ -12,7 +12,6 @@ import json
12
  import uuid # 🎲 For generating unique IDs
13
  from urllib.parse import quote # πŸ”— For encoding URLs
14
  from gradio_client import Client # 🌐 For connecting to Gradio apps
15
- from bs4 import BeautifulSoup # 🍲 For parsing HTML content
16
 
17
  # πŸŽ‰ Welcome to our fun-filled Cosmos DB and GitHub Integration app!
18
  st.set_page_config(layout="wide")
@@ -24,9 +23,11 @@ CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
24
  Key = os.environ.get("Key") # πŸ”‘ Don't forget your key!
25
 
26
  # 🏠 Your local app URL (Change this to your app's URL)
27
- #LOCAL_APP_URL = "http://localhost:8501"
28
- LOCAL_APP_URL = "https://huggingface.co/spaces/awacke1/AzureCosmosDBUI"
29
 
 
 
 
30
 
31
  # πŸ™ GitHub configuration
32
  def download_github_repo(url, local_path):
@@ -155,7 +156,7 @@ def archive_current_container(database_name, container_name, client):
155
  return f"An error occurred while archiving data: {str(e)} 😒"
156
 
157
  # πŸ” Search Glossary function
158
- def search_glossary(query, container):
159
  # πŸ•΅οΈβ€β™‚οΈ Searching the glossary for: query
160
  all_results = ""
161
  st.markdown(f"- {query}")
@@ -183,33 +184,10 @@ def search_glossary(query, container):
183
  st.write('πŸ” Run of Multi-Agent System Paper References is Complete')
184
  responseall = response2 + response1[0] + response1[1]
185
  st.markdown(responseall)
186
-
187
- # Save the response to a file
188
- filename = create_and_save_file(responseall, file_type="md", prompt=query, is_image=False, should_save=True)
189
-
190
- # Convert markdown to JSON structure
191
- json_content = markdown_to_json(responseall)
192
-
193
- # Create a record to insert into Cosmos DB
194
- record = {
195
- 'id': generate_unique_id(),
196
- 'query': query,
197
- 'content': json_content,
198
- 'filename': filename,
199
- 'timestamp': datetime.now().isoformat()
200
- }
201
-
202
- # Insert the record into Cosmos DB
203
- success, message = insert_record(container, record)
204
- if success:
205
- st.success("Record inserted into Cosmos DB successfully!")
206
- else:
207
- st.error(f"Failed to insert record into Cosmos DB: {message}")
208
-
209
  return responseall
210
 
211
  # πŸ“ Function to process text input
212
- def process_text(text_input, container):
213
  if text_input:
214
  if 'messages' not in st.session_state:
215
  st.session_state.messages = []
@@ -220,45 +198,27 @@ def process_text(text_input, container):
220
  st.markdown(text_input)
221
 
222
  with st.chat_message("assistant"):
223
- # completion = openai.ChatCompletion.create(
224
- # model=MODEL,
225
- # messages=[
226
- # {"role": m["role"], "content": m["content"]}
227
- # for m in st.session_state.messages
228
- # ],
229
- # stream=False
230
- # )
231
- # return_text = completion.choices[0].message.content
232
- # st.write("Assistant: " + return_text)
233
- # filename = create_and_save_file(return_text, file_type="md", prompt=text_input, is_image=False, should_save=True)
234
- # st.session_state.messages.append({"role": "assistant", "content": return_text})
235
-
236
- # Convert markdown to JSON structure
237
- #json_content = markdown_to_json(return_text)
238
- json_content = markdown_to_json(container)
239
-
240
- # Create a record to insert into Cosmos DB
241
- record = {
242
- 'id': generate_unique_id(),
243
- 'query': text_input,
244
- 'content': json_content,
245
- 'filename': filename,
246
- 'timestamp': datetime.now().isoformat()
247
- }
248
-
249
- # Insert the record into Cosmos DB
250
- success, message = insert_record(container, record)
251
- if success:
252
- st.success("Record inserted into Cosmos DB successfully!")
253
- else:
254
- st.error(f"Failed to insert record into Cosmos DB: {message}")
255
 
256
  # πŸ“„ Function to generate a filename
257
  def generate_filename(text, file_type):
258
  # πŸ“ Generate a filename based on the text input
259
  safe_text = "".join(c if c.isalnum() or c in (' ', '.', '_') else '_' for c in text)
260
  safe_text = "_".join(safe_text.strip().split())
261
- filename = f"{safe_text}"
262
  return filename
263
 
264
  # 🏷️ Function to extract markdown title
@@ -270,26 +230,6 @@ def extract_markdown_title(content):
270
  return line.lstrip('#').strip()
271
  return None
272
 
273
- # πŸ“„ Function to convert markdown to JSON structure
274
- def markdown_to_json(md_content):
275
- html = markdown.markdown(md_content)
276
- soup = BeautifulSoup(html, 'html.parser')
277
- json_content = []
278
- for element in soup.descendants:
279
- if element.name == 'h1':
280
- json_content.append({'type': 'heading', 'level': 1, 'text': element.get_text()})
281
- elif element.name == 'h2':
282
- json_content.append({'type': 'heading', 'level': 2, 'text': element.get_text()})
283
- elif element.name == 'h3':
284
- json_content.append({'type': 'heading', 'level': 3, 'text': element.get_text()})
285
- elif element.name == 'p':
286
- json_content.append({'type': 'paragraph', 'text': element.get_text()})
287
- elif element.name == 'a':
288
- json_content.append({'type': 'link', 'href': element.get('href'), 'text': element.get_text()})
289
- elif element.name == 'img':
290
- json_content.append({'type': 'image', 'src': element.get('src'), 'alt': element.get('alt')})
291
- return json_content
292
-
293
  # πŸ’Ύ Function to create and save a file
294
  def create_and_save_file(content, file_type="md", prompt=None, is_image=False, should_save=True):
295
  """
@@ -308,25 +248,14 @@ def create_and_save_file(content, file_type="md", prompt=None, is_image=False, s
308
  if title_from_content:
309
  filename = generate_filename(title_from_content, file_type)
310
 
311
- # Add date time to filename
312
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
313
- filename = f"{filename}_{timestamp}.{file_type}"
314
-
315
- # Ensure the directory exists
316
- save_dir = "saved_files"
317
- os.makedirs(save_dir, exist_ok=True)
318
-
319
- # Full path
320
- filepath = os.path.join(save_dir, filename)
321
-
322
  # Step 3: Save the file
323
- with open(filepath, "w", encoding="utf-8") as f:
324
  if is_image:
325
  f.write(content)
326
  else:
327
- f.write(content)
328
 
329
- return filename # Return just the filename
330
 
331
  # 🎈 Let's modify the main app to be more fun!
332
  def main():
@@ -353,16 +282,10 @@ def main():
353
  # βš™οΈ q= Run ArXiv search from query parameters
354
  try:
355
  query_params = st.query_params
356
- query = (query_params.get('q') or query_params.get('query') or [''])
357
  if query:
358
  # πŸ•΅οΈβ€β™‚οΈ We have a query! Let's process it!
359
- # Ensure the client and container are initialized
360
- if 'client' in st.session_state and st.session_state.client:
361
- database = st.session_state.client.get_database_client(DATABASE_NAME)
362
- container = database.get_container_client(CONTAINER_NAME)
363
- process_text(query, container)
364
- else:
365
- st.error("Database client not initialized.")
366
  st.stop() # Stop further execution
367
  except Exception as e:
368
  st.markdown(' ')
@@ -610,36 +533,6 @@ def main():
610
  else:
611
  st.info("No documents to display. 🧐")
612
 
613
- # πŸ—‚οΈ List saved files
614
- st.sidebar.title("πŸ—‚οΈ Saved Files")
615
-
616
- # Get list of saved files
617
- def get_saved_files():
618
- save_dir = "saved_files"
619
- if not os.path.exists(save_dir):
620
- return []
621
- files = [f for f in os.listdir(save_dir) if os.path.isfile(os.path.join(save_dir, f))]
622
- # Sort files descending by date (assuming filenames include timestamp)
623
- files.sort(reverse=True)
624
- return files
625
-
626
- saved_files = get_saved_files()
627
- if saved_files:
628
- selected_file = st.sidebar.radio("Select a saved file", saved_files)
629
- st.session_state.selected_file = selected_file
630
- else:
631
- st.sidebar.info("No saved files available.")
632
-
633
- # In the main area, display the selected file content
634
- if 'selected_file' in st.session_state:
635
- file_path = os.path.join("saved_files", st.session_state.selected_file)
636
- if os.path.exists(file_path):
637
- with open(file_path, "r", encoding="utf-8") as f:
638
- file_content = f.read()
639
- st.markdown(file_content)
640
- else:
641
- st.error("File not found.")
642
-
643
  # πŸ™ GitHub section
644
  st.subheader("πŸ™ GitHub Operations")
645
  github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
 
12
  import uuid # 🎲 For generating unique IDs
13
  from urllib.parse import quote # πŸ”— For encoding URLs
14
  from gradio_client import Client # 🌐 For connecting to Gradio apps
 
15
 
16
  # πŸŽ‰ Welcome to our fun-filled Cosmos DB and GitHub Integration app!
17
  st.set_page_config(layout="wide")
 
23
  Key = os.environ.get("Key") # πŸ”‘ Don't forget your key!
24
 
25
  # 🏠 Your local app URL (Change this to your app's URL)
26
+ LOCAL_APP_URL = "http://localhost:8501"
 
27
 
28
+ # πŸ€– OpenAI configuration
29
+ #openai.api_key = os.environ.get("OPENAI_API_KEY")
30
+ #MODEL = "gpt-3.5-turbo" # Replace with your desired model
31
 
32
  # πŸ™ GitHub configuration
33
  def download_github_repo(url, local_path):
 
156
  return f"An error occurred while archiving data: {str(e)} 😒"
157
 
158
  # πŸ” Search Glossary function
159
+ def search_glossary(query):
160
  # πŸ•΅οΈβ€β™‚οΈ Searching the glossary for: query
161
  all_results = ""
162
  st.markdown(f"- {query}")
 
184
  st.write('πŸ” Run of Multi-Agent System Paper References is Complete')
185
  responseall = response2 + response1[0] + response1[1]
186
  st.markdown(responseall)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  return responseall
188
 
189
  # πŸ“ Function to process text input
190
+ def process_text(text_input):
191
  if text_input:
192
  if 'messages' not in st.session_state:
193
  st.session_state.messages = []
 
198
  st.markdown(text_input)
199
 
200
  with st.chat_message("assistant"):
201
+ completion = openai.ChatCompletion.create(
202
+ model=MODEL,
203
+ messages=[
204
+ {"role": m["role"], "content": m["content"]}
205
+ for m in st.session_state.messages
206
+ ],
207
+ stream=False
208
+ )
209
+ return_text = completion.choices[0].message.content
210
+ st.write("Assistant: " + return_text)
211
+ filename = generate_filename(text_input, "md")
212
+
213
+ create_and_save_file(return_text, file_type="md", prompt=text_input, is_image=False, should_save=True)
214
+ st.session_state.messages.append({"role": "assistant", "content": return_text})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
  # πŸ“„ Function to generate a filename
217
  def generate_filename(text, file_type):
218
  # πŸ“ Generate a filename based on the text input
219
  safe_text = "".join(c if c.isalnum() or c in (' ', '.', '_') else '_' for c in text)
220
  safe_text = "_".join(safe_text.strip().split())
221
+ filename = f"{safe_text}.{file_type}"
222
  return filename
223
 
224
  # 🏷️ Function to extract markdown title
 
230
  return line.lstrip('#').strip()
231
  return None
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  # πŸ’Ύ Function to create and save a file
234
  def create_and_save_file(content, file_type="md", prompt=None, is_image=False, should_save=True):
235
  """
 
248
  if title_from_content:
249
  filename = generate_filename(title_from_content, file_type)
250
 
 
 
 
 
 
 
 
 
 
 
 
251
  # Step 3: Save the file
252
+ with open(filename, "w", encoding="utf-8") as f:
253
  if is_image:
254
  f.write(content)
255
  else:
256
+ f.write(prompt + "\n\n" + content)
257
 
258
+ return filename
259
 
260
  # 🎈 Let's modify the main app to be more fun!
261
  def main():
 
282
  # βš™οΈ q= Run ArXiv search from query parameters
283
  try:
284
  query_params = st.query_params
285
+ query = (query_params.get('q') or query_params.get('query') or [''])[0]
286
  if query:
287
  # πŸ•΅οΈβ€β™‚οΈ We have a query! Let's process it!
288
+ process_text(query)
 
 
 
 
 
 
289
  st.stop() # Stop further execution
290
  except Exception as e:
291
  st.markdown(' ')
 
533
  else:
534
  st.info("No documents to display. 🧐")
535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536
  # πŸ™ GitHub section
537
  st.subheader("πŸ™ GitHub Operations")
538
  github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable