Spaces:
Sleeping
Sleeping
vishanth10
commited on
Commit
·
87c7b9b
1
Parent(s):
1374a6b
Changes on including file features
Browse files
app.py
CHANGED
@@ -7,18 +7,16 @@ import json
|
|
7 |
# Carbon API Key
|
8 |
CARBON_API_KEY = "a38ee1fe5fef56fc8e1ae2afc881378804bb902882442e1554adae4f82ee23ea"
|
9 |
customer_id = "Candid"
|
10 |
-
data_source_id = 1 # Set your actual data_source_id here
|
11 |
|
|
|
12 |
def main():
|
13 |
st.title('Google Drive Data Connector using Carbon SDK')
|
14 |
|
15 |
-
# Authenticate with Carbon API
|
16 |
st.write('### Authenticate with Carbon API')
|
17 |
carbon = Carbon(api_key=CARBON_API_KEY, customer_id=customer_id)
|
18 |
token = carbon.auth.get_access_token()
|
19 |
carbon = Carbon(access_token=token.access_token) # authenticated object
|
20 |
|
21 |
-
# Connect to Google Drive
|
22 |
st.write('### Connect to Google Drive')
|
23 |
get_oauth_url_response = carbon.integrations.get_oauth_url(
|
24 |
service="GOOGLE_DRIVE",
|
@@ -34,7 +32,7 @@ def main():
|
|
34 |
salesforce_domain="string_example",
|
35 |
sync_files_on_connection=True,
|
36 |
set_page_as_boundary=False,
|
37 |
-
data_source_id=Decimal(
|
38 |
connecting_new_account=False,
|
39 |
request_id="b7620173-662c-4ae7-bb61-2e6ffd8619f5",
|
40 |
use_ocr=False,
|
@@ -50,49 +48,43 @@ def main():
|
|
50 |
)
|
51 |
|
52 |
st.write(f"OAuth URL: {get_oauth_url_response.oauth_url}")
|
53 |
-
st.write("Please use the above URL to connect your Google Drive account.")
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
if st.button('List Files'):
|
58 |
-
sync_response = carbon.integrations.sync_data_source_items(data_source_id=Decimal(data_source_id))
|
59 |
-
if sync_response:
|
60 |
-
for item in sync_response['items']:
|
61 |
-
st.write(f"File ID: {item['id']}, File Name: {item['name']}")
|
62 |
-
else:
|
63 |
-
st.write("No files found.")
|
64 |
|
65 |
-
|
|
|
|
|
|
|
66 |
st.write('### Search in the Connected Data Source')
|
67 |
-
query = st.text_input("Enter your query:", value="
|
68 |
-
if
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
st.write("-------------------------------------------------")
|
96 |
|
97 |
# Call the main function
|
98 |
if __name__ == '__main__':
|
|
|
7 |
# Carbon API Key
|
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 |
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 |
)
|
49 |
|
50 |
st.write(f"OAuth URL: {get_oauth_url_response.oauth_url}")
|
|
|
51 |
|
52 |
+
st.write('### Fetch Files and Folders')
|
53 |
+
list_files_response = carbon.data.list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
st.write('Files and Folders:')
|
56 |
+
for item in list_files_response['data']:
|
57 |
+
st.write(f"Name: {item['name']} - Type: {item['type']}")
|
58 |
+
|
59 |
st.write('### Search in the Connected Data Source')
|
60 |
+
query = st.text_input("Enter your query:", value=" ")
|
61 |
+
if query:
|
62 |
+
url = "https://api.carbon.ai/embeddings"
|
63 |
+
payload = {
|
64 |
+
"query": query,
|
65 |
+
"k": 2,
|
66 |
+
"file_ids": [item['id'] for item in list_files_response['data'] if item['type'] == 'file'], # Get all file ids
|
67 |
+
"include_all_children": True,
|
68 |
+
"tags": {},
|
69 |
+
"include_tags": True,
|
70 |
+
"include_vectors": True,
|
71 |
+
"include_raw_file": True,
|
72 |
+
"hybrid_search": False,
|
73 |
+
"media_type": "TEXT",
|
74 |
+
"embedding_model": "OPENAI"
|
75 |
+
}
|
76 |
+
headers = {
|
77 |
+
"authorization": f"Bearer {CARBON_API_KEY}",
|
78 |
+
"customer-id": customer_id,
|
79 |
+
"Content-Type": "application/json"
|
80 |
+
}
|
81 |
+
response_search = requests.post(url, json=payload, headers=headers)
|
82 |
+
response_search_chunks = json.loads(response_search.text)
|
83 |
+
|
84 |
+
for i, doc in enumerate(response_search_chunks['documents']):
|
85 |
+
st.write(f"Document {i+1}")
|
86 |
+
st.write(doc['content'])
|
87 |
+
st.write("-------------------------------------------------")
|
|
|
88 |
|
89 |
# Call the main function
|
90 |
if __name__ == '__main__':
|