Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
16 |
-
|
17 |
-
|
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 |
-
#
|
42 |
-
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
110 |
-
def
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
def
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
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 |
-
|
|
|
160 |
|
161 |
-
#
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
if Key:
|
167 |
st.session_state.primary_key = Key
|
168 |
st.session_state.logged_in = True
|
|
|
|
|
|
|
169 |
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
|
178 |
-
if
|
179 |
-
|
180 |
-
containers =
|
181 |
-
selected_container = st.sidebar.selectbox("Select Container", containers)
|
182 |
|
183 |
-
if selected_container:
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
if documents:
|
187 |
-
st.write("๐ Document List:")
|
188 |
df = pd.DataFrame(documents)
|
189 |
st.dataframe(df)
|
190 |
else:
|
191 |
-
st.
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
st.
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
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()
|