awacke1 commited on
Commit
2dba4ac
ยท
verified ยท
1 Parent(s): a016d7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +202 -138
app.py CHANGED
@@ -12,25 +12,9 @@ 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
- import anthropic
16
- import pytz
17
- import re
18
- from PIL import Image
19
- import glob
20
- from streamlit.components.v1 import html
21
-
22
- # ๐ŸŽ‰ Welcome to our epic Cosmos DB, GitHub, and Claude App! The universe is at your command ๐Ÿš€
23
- st.set_page_config(
24
- page_title="๐Ÿค–Git๐ŸŒŒCosmos๐Ÿ’ซ & Claude๐Ÿง ",
25
- page_icon="๐Ÿค–๐ŸŒŒ๐Ÿ’ซ๐Ÿ“",
26
- layout="wide",
27
- initial_sidebar_state="auto",
28
- menu_items={
29
- 'Get Help': 'https://huggingface.co/awacke1',
30
- 'Report a bug': 'https://huggingface.co/spaces/awacke1',
31
- 'About': '๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and GitHub Agent, Now with Claude!'
32
- }
33
- )
34
 
35
  # ๐ŸŒŒ Cosmos DB configuration
36
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
@@ -38,11 +22,8 @@ DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
38
  CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
39
  Key = os.environ.get("Key") # ๐Ÿ”‘ Don't forget your key!
40
 
41
- # Set up the Anthropic client (Claude time ๐Ÿค–๐Ÿง )
42
- client_anthropic = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
43
-
44
- # Set up Gradio client for ArXiv queries (ArXiv, the scholar's playground ๐ŸŽ“)
45
- client_gradio = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
46
 
47
  # ๐Ÿ™ GitHub configuration
48
  def download_github_repo(url, local_path):
@@ -65,8 +46,6 @@ def push_to_github(local_path, repo, github_token):
65
  repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
66
  local_repo = Repo(local_path)
67
  if 'origin' in [remote.name for remote in local_repo.remotes]:
68
-
69
- #if 'origin' in [remote.name for remote.local_repo.remotes]: --error
70
  origin = local_repo.remote('origin')
71
  origin.set_url(repo_url)
72
  else:
@@ -92,8 +71,23 @@ def get_base64_download_link(file_path, file_name):
92
  base64_encoded = base64.b64encode(contents).decode()
93
  return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">โฌ‡๏ธ Download {file_name}</a>'
94
 
95
- # ๐ŸŒŸ Cosmos DB functions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  def insert_record(container, record):
 
97
  try:
98
  container.create_item(body=record)
99
  return True, "Record inserted successfully! ๐ŸŽ‰"
@@ -102,134 +96,204 @@ def insert_record(container, record):
102
  except Exception as e:
103
  return False, f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ"
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  # ๐ŸŽฒ Function to generate a unique UUID
106
  def generate_unique_id():
 
107
  return str(uuid.uuid4())
108
 
109
- # Claude ๐Ÿง  Chat handling (Wise responses guaranteed! ๐Ÿค–)
110
- def chat_with_claude(user_input):
111
- response = client_anthropic.messages.create(
112
- model="claude-3-sonnet-20240229",
113
- max_tokens=1000,
114
- messages=[
115
- {"role": "user", "content": user_input}
116
- ]
117
- )
118
- return response.content[0].text
119
-
120
- # File handling (Save that wisdom in files! ๐Ÿ’พ)
121
- def generate_filename(prompt, file_type):
122
- central = pytz.timezone('US/Central')
123
- safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
124
- safe_prompt = re.sub(r'\W+', '_', prompt)[:90]
125
- return f"{safe_date_time}_{safe_prompt}.{file_type}"
126
-
127
- def create_file(filename, prompt, response, should_save=True):
128
- if not should_save:
129
- return
130
- with open(filename, 'w', encoding='utf-8') as file:
131
- file.write(prompt + "\n\n" + response)
132
-
133
- def load_file(file_name):
134
- with open(file_name, "r", encoding='utf-8') as file:
135
- content = file.read()
136
- return content
137
-
138
- # ๐ŸŽจ Image and media handling (Let's look at some visuals! ๐Ÿ“ธ๐ŸŽฌ๐ŸŽถ)
139
- def get_video_html(video_path, width="100%"):
140
- video_url = f"data:video/mp4;base64,{base64.b64encode(open(video_path, 'rb').read()).decode()}"
141
- return f'''
142
- <video width="{width}" controls autoplay muted loop>
143
- <source src="{video_url}" type="video/mp4">
144
- Your browser does not support the video tag.
145
- </video>
146
- '''
147
-
148
- def get_audio_html(audio_path, width="100%"):
149
- audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
150
- return f'''
151
- <audio controls style="width: {width};">
152
- <source src="{audio_url}" type="audio/mpeg">
153
- Your browser does not support the audio element.
154
- </audio>
155
- '''
156
-
157
- # Streamlit layout (because a clean UI is key! ๐Ÿ )
158
  def main():
159
- st.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ & Claude๐Ÿง  - The Ultimate App")
 
160
 
161
- # Sidebar navigation (The command center ๐Ÿงญ)
162
- st.sidebar.title("๐Ÿง Claude๐Ÿ“ & Cosmos Explorer")
163
-
164
- # Cosmos DB Section
165
- st.sidebar.header("Cosmos DB Controls")
 
 
 
 
 
 
166
  if Key:
167
  st.session_state.primary_key = Key
168
  st.session_state.logged_in = True
 
 
 
169
 
170
- if st.session_state.logged_in:
171
- st.sidebar.write("๐Ÿ” Connected to Cosmos DB!")
 
 
 
 
 
 
 
 
 
172
 
173
- # Fetch documents
174
- cosmos_client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
175
- databases = [db['id'] for db in cosmos_client.list_databases()]
176
- selected_db = st.sidebar.selectbox("Select Database", databases)
177
 
178
- if selected_db:
179
- db_client = cosmos_client.get_database_client(selected_db)
180
- containers = [container['id'] for container in db_client.list_containers()]
181
- selected_container = st.sidebar.selectbox("Select Container", containers)
182
 
183
- if selected_container:
184
- container = db_client.get_container_client(selected_container)
185
- documents = list(container.read_all_items())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  if documents:
187
- st.write("๐Ÿ“ Document List:")
188
  df = pd.DataFrame(documents)
189
  st.dataframe(df)
190
  else:
191
- st.write("๐Ÿ“ญ No documents in this container.")
192
-
193
- # Claude Chat Section
194
- st.header("Chat with Claude ๐Ÿค–๐Ÿง ")
195
- user_input = st.text_area("Ask Claude anything:")
196
- if st.button("Send"):
197
- if user_input:
198
- response = chat_with_claude(user_input)
199
- st.write(f"Claude says: {response}")
200
-
201
- filename = generate_filename(user_input, "md")
202
- create_file(filename, user_input, response)
203
-
204
- if 'chat_history' not in st.session_state:
205
- st.session_state.chat_history = []
206
- st.session_state.chat_history.append({"user": user_input, "claude": response})
207
-
208
- if "chat_history" in st.session_state:
209
- st.subheader("Past Conversations ๐Ÿ“œ")
210
- for chat in st.session_state.chat_history:
211
- st.text_area("You said ๐Ÿ’ฌ:", chat["user"], height=100, disabled=True)
212
- st.text_area("Claude replied ๐Ÿค–:", chat["claude"], height=200, disabled=True)
213
- st.markdown("---")
214
-
215
- # Media Galleries (For those who prefer images and videos ๐Ÿ–ผ๐ŸŽฅ๐ŸŽถ)
216
- st.subheader("Image Gallery ๐Ÿ–ผ")
217
- image_files = glob.glob("*.png") + glob.glob("*.jpg")
218
- cols = st.columns(3)
219
- for idx, image_file in enumerate(image_files):
220
- with cols[idx % 3]:
221
- img = Image.open(image_file)
222
- st.image(img)
223
-
224
- st.subheader("Video Gallery ๐ŸŽฅ")
225
- video_files = glob.glob("*.mp4")
226
- for video_file in video_files:
227
- st.markdown(get_video_html(video_file), unsafe_allow_html=True)
228
-
229
- st.subheader("Audio Gallery ๐ŸŽถ")
230
- audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
231
- for audio_file in audio_files:
232
- st.markdown(get_audio_html(audio_file), unsafe_allow_html=True)
 
 
 
233
 
234
  if __name__ == "__main__":
235
  main()
 
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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # ๐ŸŒŒ Cosmos DB configuration
20
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
 
22
  CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
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 = "https://huggingface.co/spaces/awacke1/AzureCosmosDBUI"
 
 
 
27
 
28
  # ๐Ÿ™ GitHub configuration
29
  def download_github_repo(url, local_path):
 
46
  repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
47
  local_repo = Repo(local_path)
48
  if 'origin' in [remote.name for remote in local_repo.remotes]:
 
 
49
  origin = local_repo.remote('origin')
50
  origin.set_url(repo_url)
51
  else:
 
71
  base64_encoded = base64.b64encode(contents).decode()
72
  return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">โฌ‡๏ธ Download {file_name}</a>'
73
 
74
+ # ๐Ÿงญ Cosmos DB Functions
75
+ def get_databases(client):
76
+ # ๐Ÿ“š Fetching list of databases!
77
+ return [db['id'] for db in client.list_databases()]
78
+
79
+ def get_containers(database):
80
+ # ๐Ÿ“‚ Listing containers within the selected database
81
+ return [container['id'] for container in database.list_containers()]
82
+
83
+ def get_documents(container, limit=None):
84
+ # ๐Ÿ“ Fetching documents from Cosmos DB
85
+ query = "SELECT * FROM c ORDER BY c._ts DESC"
86
+ items = list(container.query_items(query=query, enable_cross_partition_query=True, max_item_count=limit))
87
+ return items
88
+
89
  def insert_record(container, record):
90
+ # ๐Ÿ“ฅ Inserting a record into the Cosmos DB
91
  try:
92
  container.create_item(body=record)
93
  return True, "Record inserted successfully! ๐ŸŽ‰"
 
96
  except Exception as e:
97
  return False, f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ"
98
 
99
+ def update_record(container, updated_record):
100
+ # ๐Ÿ› ๏ธ Updating a record in the Cosmos DB
101
+ try:
102
+ container.upsert_item(body=updated_record)
103
+ return True, f"Record with id {updated_record['id']} successfully updated. ๐Ÿ› ๏ธ"
104
+ except exceptions.CosmosHttpResponseError as e:
105
+ return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
106
+ except Exception as e:
107
+ return False, f"An unexpected error occurred: {traceback.format_exc()} ๐Ÿ˜ฑ"
108
+
109
+ def delete_record(container, name, id):
110
+ # ๐Ÿ—‘๏ธ Deleting a record in Cosmos DB
111
+ try:
112
+ container.delete_item(item=id, partition_key=id)
113
+ return True, f"Successfully deleted record with name: {name} and id: {id} ๐Ÿ—‘๏ธ"
114
+ except exceptions.CosmosResourceNotFoundError:
115
+ return False, f"Record with id {id} not found. It may have been already deleted. ๐Ÿ•ต๏ธโ€โ™‚๏ธ"
116
+ except exceptions.CosmosHttpResponseError as e:
117
+ return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
118
+ except Exception as e:
119
+ return False, f"An unexpected error occurred: {traceback.format_exc()} ๐Ÿ˜ฑ"
120
+
121
+ def archive_current_container(database_name, container_name, client):
122
+ # ๐Ÿ“ฆ Archiving the container data from Cosmos DB
123
+ try:
124
+ base_dir = "./cosmos_archive_current_container"
125
+ if os.path.exists(base_dir):
126
+ shutil.rmtree(base_dir)
127
+ os.makedirs(base_dir)
128
+
129
+ db_client = client.get_database_client(database_name)
130
+ container_client = db_client.get_container_client(container_name)
131
+ items = list(container_client.read_all_items())
132
+
133
+ container_dir = os.path.join(base_dir, container_name)
134
+ os.makedirs(container_dir)
135
+
136
+ for item in items:
137
+ item_id = item.get('id', f"unknown_{datetime.now().strftime('%Y%m%d%H%M%S')}")
138
+ with open(os.path.join(container_dir, f"{item_id}.json"), 'w') as f:
139
+ json.dump(item, f, indent=2)
140
+
141
+ archive_name = f"{container_name}_archive_{datetime.now().strftime('%Y%m%d%H%M%S')}"
142
+ shutil.make_archive(archive_name, 'zip', base_dir)
143
+
144
+ return get_base64_download_link(f"{archive_name}.zip", f"{archive_name}.zip")
145
+ except Exception as e:
146
+ return f"An error occurred while archiving data: {str(e)} ๐Ÿ˜ข"
147
+
148
  # ๐ŸŽฒ Function to generate a unique UUID
149
  def generate_unique_id():
150
+ # ๐Ÿ”ฎ Generate a unique ID for new records
151
  return str(uuid.uuid4())
152
 
153
+ # ๐Ÿ”— Helper function for hyperlink extraction
154
+ def extract_hyperlinks(responses):
155
+ # ๐Ÿ”— Extracting hyperlinks from response
156
+ hyperlinks = []
157
+ for response in responses:
158
+ parsed_response = json.loads(response)
159
+ links = [value for key, value in parsed_response.items() if isinstance(value, str) and value.startswith("http")]
160
+ hyperlinks.extend(links)
161
+ return hyperlinks
162
+
163
+ # ๐Ÿ“‹ Helper function to format text with line numbers
164
+ def format_with_line_numbers(text):
165
+ # ๐Ÿ“‹ Organizing lines with numbers
166
+ lines = text.splitlines()
167
+ formatted_text = '\n'.join(f"{i+1}: {line}" for i, line in enumerate(lines))
168
+ return formatted_text
169
+
170
+ # ๐ŸŽค Function to process text input
171
+ def process_text(text_input):
172
+ # ๐ŸŽค Processing input for queries or searches
173
+ if text_input:
174
+ if 'messages' not in st.session_state:
175
+ st.session_state.messages = []
176
+
177
+ st.session_state.messages.append({"role": "user", "content": text_input})
178
+
179
+ with st.chat_message("user"):
180
+ st.markdown(text_input)
181
+
182
+ with st.chat_message("assistant"):
183
+ search_glossary(text_input)
184
+
185
+ # ๐ŸŽˆ Main function to drive the application
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  def main():
187
+ # ๐ŸŽˆ Let's kickstart the app with a fun introduction!
188
+ st.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent")
189
 
190
+ # ๐Ÿšฆ Initialize session state for managing components
191
+ if 'logged_in' not in st.session_state:
192
+ st.session_state.logged_in = False
193
+ if 'client' not in st.session_state:
194
+ st.session_state.client = None
195
+ if 'selected_database' not in st.session_state:
196
+ st.session_state.selected_database = None
197
+ if 'selected_container' not in st.session_state:
198
+ st.session_state.selected_container = None
199
+
200
+ # ๐Ÿ” Login management
201
  if Key:
202
  st.session_state.primary_key = Key
203
  st.session_state.logged_in = True
204
+ else:
205
+ st.error("Cosmos DB Key is not set in environment variables. ๐Ÿ”‘โŒ")
206
+ return
207
 
208
+ if st.session_state.logged_in:
209
+ try:
210
+ # ๐ŸŒŒ Initialize Cosmos DB client
211
+ if st.session_state.client is None:
212
+ st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
213
+
214
+ # ๐Ÿ—„๏ธ Sidebar for database and container selection
215
+ st.sidebar.title("๐Ÿ—„๏ธ Cosmos DB Navigator")
216
+
217
+ databases = get_databases(st.session_state.client)
218
+ selected_db = st.sidebar.selectbox("๐Ÿ—ƒ๏ธ Select Database", databases)
219
 
220
+ if selected_db != st.session_state.selected_database:
221
+ st.session_state.selected_database = selected_db
222
+ st.session_state.selected_container = None
223
+ st.rerun()
224
 
225
+ if st.session_state.selected_database:
226
+ database = st.session_state.client.get_database_client(st.session_state.selected_database)
227
+ containers = get_containers(database)
228
+ selected_container = st.sidebar.selectbox("๐Ÿ“ Select Container", containers)
229
 
230
+ if selected_container != st.session_state.selected_container:
231
+ st.session_state.selected_container = selected_container
232
+ st.rerun()
233
+
234
+ if st.session_state.selected_container:
235
+ container = database.get_container_client(st.session_state.selected_container)
236
+
237
+ # ๐Ÿ“ฆ Export container data
238
+ if st.button("๐Ÿ“ฆ Export Container Data"):
239
+ download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
240
+ if download_link.startswith('<a'):
241
+ st.markdown(download_link, unsafe_allow_html=True)
242
+ else:
243
+ st.error(download_link)
244
+
245
+ # ๐Ÿ“ Display documents
246
+ documents = get_documents(container)
247
+ st.subheader(f"๐Ÿ“Š Container: {st.session_state.selected_container}")
248
  if documents:
 
249
  df = pd.DataFrame(documents)
250
  st.dataframe(df)
251
  else:
252
+ st.info("No documents to display. ๐Ÿง")
253
+
254
+ # ๐Ÿ™ GitHub operations
255
+ st.subheader("๐Ÿ™ GitHub Operations")
256
+ github_token = os.environ.get("GITHUB") # GitHub token for authentication
257
+ source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
258
+ new_repo_name = st.text_input("New Repository Name", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
259
+
260
+ col1, col2 = st.columns(2)
261
+ with col1:
262
+ if st.button("๐Ÿ“ฅ Clone Repository"):
263
+ if github_token and source_repo:
264
+ local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
265
+ download_github_repo(source_repo, local_path)
266
+ zip_filename = f"{new_repo_name}.zip"
267
+ create_zip_file(local_path, zip_filename[:-4])
268
+ st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
269
+ st.success("Repository cloned successfully! ๐ŸŽ‰")
270
+ shutil.rmtree(local_path)
271
+ else:
272
+ st.error("Please provide GitHub token and source repo URL.")
273
+
274
+ with col2:
275
+ if st.button("๐Ÿ“ค Push to New Repository"):
276
+ if github_token and source_repo:
277
+ g = Github(github_token)
278
+ new_repo = create_repo(g, new_repo_name)
279
+ local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
280
+ download_github_repo(source_repo, local_path)
281
+ push_to_github(local_path, new_repo, github_token)
282
+ st.success(f"Repository pushed successfully to {new_repo.html_url} ๐Ÿš€")
283
+ shutil.rmtree(local_path)
284
+ else:
285
+ st.error("Please provide GitHub token and source repo URL.")
286
+
287
+ except exceptions.CosmosHttpResponseError as e:
288
+ st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} ๐Ÿšจ")
289
+ except Exception as e:
290
+ st.error(f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ")
291
+
292
+ # ๐Ÿšช Logout option
293
+ if st.session_state.logged_in and st.sidebar.button("๐Ÿšช Logout"):
294
+ st.session_state.logged_in = False
295
+ st.session_state.client = None
296
+ st.rerun()
297
 
298
  if __name__ == "__main__":
299
  main()