Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,85 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
|
| 8 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
("What is AI?", "Artificial Intelligence (AI) is the simulation of human intelligence in machines.")
|
| 14 |
-
# Add more FAQ pairs...
|
| 15 |
-
]
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
corpus_embeddings = model.encode(corpus, convert_to_tensor=True)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
query_embedding = model.encode(query, convert_to_tensor=True).cpu().numpy()
|
| 39 |
-
|
| 40 |
-
# Use FAISS for nearest neighbor search
|
| 41 |
-
faiss_results = index.search(query_embedding, k=1)
|
| 42 |
-
faiss_top_result_idx = faiss_results[1][0][0]
|
| 43 |
-
faiss_top_score = faiss_results[0][0][0]
|
| 44 |
-
|
| 45 |
-
# Use Chroma for semantic search
|
| 46 |
-
chroma_results = collection.query(query_embeddings=[query_embedding], n_results=1)
|
| 47 |
-
chroma_top_result = chroma_results['documents'][0]
|
| 48 |
-
|
| 49 |
-
# Combining results from FAISS and Chroma
|
| 50 |
-
if faiss_top_score > 0.5:
|
| 51 |
-
return answers[corpus[faiss_top_result_idx]]
|
| 52 |
else:
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
import requests
|
| 3 |
+
import oci
|
| 4 |
+
import json
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
|
|
|
| 8 |
|
| 9 |
+
# Oracle Cloud Configurations (Ensure your OCI config file is set up correctly)
|
| 10 |
+
config = oci.config.from_file("~/.oci/config", "DEFAULT")
|
| 11 |
+
signer = oci.auth.signers.get_resource_principals_signer()
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Endpoint URL for Oracle AI Service (Replace region appropriately)
|
| 14 |
+
oci_endpoint = "https://anomalydetection.aiservice.{{region}}.oci.oraclecloud.com/20210101/aiPrivateEndpoints"
|
|
|
|
| 15 |
|
| 16 |
+
# Request headers including authentication (ensure the correct signing/authentication process)
|
| 17 |
+
headers = {
|
| 18 |
+
"Content-Type": "application/json",
|
| 19 |
+
"Authorization": "Bearer {access_token}" # Use the appropriate method to authenticate
|
| 20 |
+
}
|
| 21 |
|
| 22 |
+
@app.post("/create-private-endpoint")
|
| 23 |
+
async def create_private_endpoint(compartment_id: str, subnet_id: str, display_name: str, dns_zones: list):
|
| 24 |
+
"""Creates a private reverse connection endpoint in Oracle Cloud"""
|
| 25 |
+
|
| 26 |
+
# Prepare the request body
|
| 27 |
+
request_body = {
|
| 28 |
+
"compartmentId": compartment_id,
|
| 29 |
+
"subnetId": subnet_id,
|
| 30 |
+
"displayName": display_name,
|
| 31 |
+
"dnsZones": dns_zones,
|
| 32 |
+
"definedTags": {},
|
| 33 |
+
"freeformTags": {}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
response = requests.post(
|
| 37 |
+
oci_endpoint,
|
| 38 |
+
headers=headers,
|
| 39 |
+
data=json.dumps(request_body)
|
| 40 |
)
|
| 41 |
+
|
| 42 |
+
if response.status_code == 202:
|
| 43 |
+
return {"message": "Private endpoint created successfully", "status": response.status_code}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
+
raise HTTPException(status_code=response.status_code, detail=response.json())
|
| 46 |
|
| 47 |
+
@app.put("/update-private-endpoint/{endpoint_id}")
|
| 48 |
+
async def update_private_endpoint(endpoint_id: str, display_name: str, dns_zones: list):
|
| 49 |
+
"""Updates a private reverse connection endpoint"""
|
| 50 |
+
|
| 51 |
+
update_url = f"{oci_endpoint}/{endpoint_id}"
|
| 52 |
+
|
| 53 |
+
request_body = {
|
| 54 |
+
"displayName": display_name,
|
| 55 |
+
"dnsZones": dns_zones,
|
| 56 |
+
"definedTags": {},
|
| 57 |
+
"freeformTags": {}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
response = requests.put(
|
| 61 |
+
update_url,
|
| 62 |
+
headers=headers,
|
| 63 |
+
data=json.dumps(request_body)
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if response.status_code == 202:
|
| 67 |
+
return {"message": "Private endpoint updated successfully", "status": response.status_code}
|
| 68 |
+
else:
|
| 69 |
+
raise HTTPException(status_code=response.status_code, detail=response.json())
|
| 70 |
|
| 71 |
+
@app.get("/get-private-endpoint/{endpoint_id}")
|
| 72 |
+
async def get_private_endpoint(endpoint_id: str):
|
| 73 |
+
"""Retrieves a private reverse connection endpoint by its ID"""
|
| 74 |
+
|
| 75 |
+
get_url = f"{oci_endpoint}/{endpoint_id}"
|
| 76 |
+
|
| 77 |
+
response = requests.get(
|
| 78 |
+
get_url,
|
| 79 |
+
headers=headers
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if response.status_code == 200:
|
| 83 |
+
return response.json()
|
| 84 |
+
else:
|
| 85 |
+
raise HTTPException(status_code=response.status_code, detail=response.json())
|