Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import time
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
|
| 11 |
+
# Clear the console
|
| 12 |
+
os.system('cls' if os.name=='nt' else 'clear')
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
|
| 16 |
+
# Get the business card schema
|
| 17 |
+
with open("biz-card.json", "r") as file:
|
| 18 |
+
schema_json = json.load(file)
|
| 19 |
+
|
| 20 |
+
card_schema = json.dumps(schema_json)
|
| 21 |
+
|
| 22 |
+
# Get config settings
|
| 23 |
+
load_dotenv()
|
| 24 |
+
ai_svc_endpoint = os.getenv('ENDPOINT')
|
| 25 |
+
ai_svc_key = os.getenv('KEY')
|
| 26 |
+
analyzer = os.getenv('ANALYZER_NAME')
|
| 27 |
+
|
| 28 |
+
# Create the analyzer
|
| 29 |
+
create_analyzer (card_schema, analyzer, ai_svc_endpoint, ai_svc_key)
|
| 30 |
+
|
| 31 |
+
print("\n")
|
| 32 |
+
|
| 33 |
+
except Exception as ex:
|
| 34 |
+
print(ex)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def create_analyzer (schema, analyzer, endpoint, key):
|
| 39 |
+
|
| 40 |
+
# Create a Content Understanding analyzer
|
| 41 |
+
print (f"Creating {analyzer}")
|
| 42 |
+
|
| 43 |
+
# Set the API version
|
| 44 |
+
CU_VERSION = "2025-05-01-preview"
|
| 45 |
+
|
| 46 |
+
# initiate the analyzer creation operation
|
| 47 |
+
headers = { "Ocp-Apim-Subscription-Key": key, "Content-Type": "application/json"}
|
| 48 |
+
url = f"{endpoint}/contentunderstanding/analyzers/{analyzer}?api-version={CU_VERSION}"
|
| 49 |
+
|
| 50 |
+
# Delete the analyzer if it already exists
|
| 51 |
+
response = requests.delete(url, headers=headers)
|
| 52 |
+
print(response.status_code)
|
| 53 |
+
time.sleep(1)
|
| 54 |
+
|
| 55 |
+
# Now create it
|
| 56 |
+
response = requests.put(url, headers=headers, data=(schema))
|
| 57 |
+
print(response.status_code)
|
| 58 |
+
|
| 59 |
+
# Get the response and extract the callback URL
|
| 60 |
+
callback_url = response.headers["Operation-Location"]
|
| 61 |
+
|
| 62 |
+
# Check the status of the operation
|
| 63 |
+
time.sleep(1)
|
| 64 |
+
result_response = requests.get(callback_url, headers=headers)
|
| 65 |
+
|
| 66 |
+
# Keep polling until the operation is no longer running
|
| 67 |
+
status = result_response.json().get("status")
|
| 68 |
+
while status == "Running":
|
| 69 |
+
time.sleep(1)
|
| 70 |
+
result_response = requests.get(callback_url, headers=headers)
|
| 71 |
+
status = result_response.json().get("status")
|
| 72 |
+
|
| 73 |
+
result = result_response.json().get("status")
|
| 74 |
+
print(result)
|
| 75 |
+
|
| 76 |
+
if result == "Succeeded":
|
| 77 |
+
print(f"Analyzer '{analyzer}' created successfully.")
|
| 78 |
+
else:
|
| 79 |
+
print("Analyzer creation failed.")
|
| 80 |
+
print(result_response.json())
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
main()
|