Spaces:
Sleeping
Sleeping
vishanth10
commited on
Commit
·
6f3dbfd
1
Parent(s):
87c7b9b
new UI changes
Browse files
app.py
CHANGED
@@ -8,15 +8,16 @@ import json
|
|
8 |
CARBON_API_KEY = "a38ee1fe5fef56fc8e1ae2afc881378804bb902882442e1554adae4f82ee23ea"
|
9 |
customer_id = "Candid"
|
10 |
|
11 |
-
# Define your main function here
|
12 |
def main():
|
13 |
st.title('Google Drive Data Connector using Carbon SDK')
|
14 |
|
|
|
15 |
st.write('### Authenticate with Carbon API')
|
16 |
carbon = Carbon(api_key=CARBON_API_KEY, customer_id=customer_id)
|
17 |
token = carbon.auth.get_access_token()
|
18 |
carbon = Carbon(access_token=token.access_token) # authenticated object
|
19 |
|
|
|
20 |
st.write('### Connect to Google Drive')
|
21 |
get_oauth_url_response = carbon.integrations.get_oauth_url(
|
22 |
service="GOOGLE_DRIVE",
|
@@ -32,7 +33,7 @@ def main():
|
|
32 |
salesforce_domain="string_example",
|
33 |
sync_files_on_connection=True,
|
34 |
set_page_as_boundary=False,
|
35 |
-
data_source_id=Decimal(1),
|
36 |
connecting_new_account=False,
|
37 |
request_id="b7620173-662c-4ae7-bb61-2e6ffd8619f5",
|
38 |
use_ocr=False,
|
@@ -48,43 +49,97 @@ def main():
|
|
48 |
)
|
49 |
|
50 |
st.write(f"OAuth URL: {get_oauth_url_response.oauth_url}")
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
st.write('### Search in the Connected Data Source')
|
60 |
-
query = st.text_input("Enter your query:", value=" ")
|
61 |
-
if
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
# Call the main function
|
90 |
if __name__ == '__main__':
|
|
|
8 |
CARBON_API_KEY = "a38ee1fe5fef56fc8e1ae2afc881378804bb902882442e1554adae4f82ee23ea"
|
9 |
customer_id = "Candid"
|
10 |
|
|
|
11 |
def main():
|
12 |
st.title('Google Drive Data Connector using Carbon SDK')
|
13 |
|
14 |
+
# Authenticate with Carbon API
|
15 |
st.write('### Authenticate with Carbon API')
|
16 |
carbon = Carbon(api_key=CARBON_API_KEY, customer_id=customer_id)
|
17 |
token = carbon.auth.get_access_token()
|
18 |
carbon = Carbon(access_token=token.access_token) # authenticated object
|
19 |
|
20 |
+
# Connect to Google Drive
|
21 |
st.write('### Connect to Google Drive')
|
22 |
get_oauth_url_response = carbon.integrations.get_oauth_url(
|
23 |
service="GOOGLE_DRIVE",
|
|
|
33 |
salesforce_domain="string_example",
|
34 |
sync_files_on_connection=True,
|
35 |
set_page_as_boundary=False,
|
36 |
+
data_source_id=Decimal(1), # Using static data_source_id initially
|
37 |
connecting_new_account=False,
|
38 |
request_id="b7620173-662c-4ae7-bb61-2e6ffd8619f5",
|
39 |
use_ocr=False,
|
|
|
49 |
)
|
50 |
|
51 |
st.write(f"OAuth URL: {get_oauth_url_response.oauth_url}")
|
52 |
+
st.write("Please use the above URL to connect your Google Drive account.")
|
53 |
|
54 |
+
# Fetch the data source ID for the connected Google Drive
|
55 |
+
st.write('### List Files in Connected Google Drive')
|
56 |
+
if st.button('List Files'):
|
57 |
+
with st.spinner('Fetching files...'):
|
58 |
+
try:
|
59 |
+
# Fetch data sources
|
60 |
+
query_user_data_sources_response = carbon.data_sources.query_user_data_sources(
|
61 |
+
pagination={"limit": 100, "offset": 0},
|
62 |
+
order_by="created_at",
|
63 |
+
order_dir="desc",
|
64 |
+
filters={"source": "GOOGLE_DRIVE"},
|
65 |
+
)
|
66 |
+
|
67 |
+
# Filter and display relevant information from data sources response
|
68 |
+
st.write("Data sources:")
|
69 |
+
for ds in query_user_data_sources_response.results:
|
70 |
+
st.write(f"- ID: {ds.id}, External ID: {ds.data_source_external_id}, Sync Status: {ds.sync_status}")
|
71 |
|
72 |
+
data_sources = query_user_data_sources_response.results
|
73 |
+
if data_sources:
|
74 |
+
data_source_id = int(data_sources[0].id) # Convert to int
|
75 |
+
st.write(f"Using data_source_id: {data_source_id}")
|
76 |
+
sync_response = carbon.integrations.sync_data_source_items(data_source_id=data_source_id)
|
77 |
+
|
78 |
+
# Filter and display relevant information from sync response
|
79 |
+
st.write("Files in Google Drive:")
|
80 |
+
if hasattr(sync_response, 'items'):
|
81 |
+
for item in sync_response.items:
|
82 |
+
st.write(f"File ID: {item.id}, File Name: {item.name}, File Size: {item.size if hasattr(item, 'size') else 'N/A'}, Last Modified: {item.last_modified if hasattr(item, 'last_modified') else 'N/A'}")
|
83 |
+
else:
|
84 |
+
st.write("No files found.")
|
85 |
+
else:
|
86 |
+
st.write("No Google Drive data sources found for the user.")
|
87 |
+
except Exception as e:
|
88 |
+
st.error(f"An error occurred: {e}")
|
89 |
+
|
90 |
+
# Search in the connected data source
|
91 |
st.write('### Search in the Connected Data Source')
|
92 |
+
query = st.text_input("Enter your query:", value="Type here...")
|
93 |
+
if st.button('Search'):
|
94 |
+
if query:
|
95 |
+
with st.spinner('Searching...'):
|
96 |
+
try:
|
97 |
+
url = "https://api.carbon.ai/embeddings"
|
98 |
+
payload = {
|
99 |
+
"query": query,
|
100 |
+
"k": 2,
|
101 |
+
"file_ids": [], # Modify to include relevant file IDs if needed
|
102 |
+
"include_all_children": True,
|
103 |
+
"tags": {},
|
104 |
+
"include_tags": True,
|
105 |
+
"include_vectors": True,
|
106 |
+
"include_raw_file": True,
|
107 |
+
"hybrid_search": False,
|
108 |
+
"media_type": "TEXT",
|
109 |
+
"embedding_model": "OPENAI"
|
110 |
+
}
|
111 |
+
headers = {
|
112 |
+
"authorization": f"Bearer {CARBON_API_KEY}",
|
113 |
+
"customer-id": customer_id,
|
114 |
+
"Content-Type": "application/json"
|
115 |
+
}
|
116 |
+
response_search = requests.post(url, json=payload, headers=headers)
|
117 |
+
response_search_chunks = json.loads(response_search.text)
|
118 |
+
|
119 |
+
st.write("Search results:")
|
120 |
+
for i, doc in enumerate(response_search_chunks['documents']):
|
121 |
+
st.write(f"Document {i+1}:")
|
122 |
+
st.write(doc['content'])
|
123 |
+
if 'file_url' in doc:
|
124 |
+
st.markdown(f"[Download {doc['filename']}]({doc['file_url']})")
|
125 |
+
st.write("-------------------------------------------------")
|
126 |
+
except Exception as e:
|
127 |
+
st.error(f"An error occurred: {e}")
|
128 |
+
else:
|
129 |
+
st.write("Please enter a query to search.")
|
130 |
+
|
131 |
+
# Display search history
|
132 |
+
st.write('### Search History')
|
133 |
+
if 'search_history' not in st.session_state:
|
134 |
+
st.session_state['search_history'] = []
|
135 |
+
|
136 |
+
if query and st.button('Add to Search History'):
|
137 |
+
st.session_state['search_history'].append(query)
|
138 |
+
|
139 |
+
if st.session_state['search_history']:
|
140 |
+
st.write("Past Searches:")
|
141 |
+
for past_query in st.session_state['search_history']:
|
142 |
+
st.write(past_query)
|
143 |
|
144 |
# Call the main function
|
145 |
if __name__ == '__main__':
|