-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6459f3c
commit c0b516b
Showing
46 changed files
with
555 additions
and
579 deletions.
There are no files selected for viewing
302 changes: 301 additions & 1 deletion
302
...gnitive_services/CognitiveServices - Create a Multilingual Search Engine from Forms.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
165 changes: 165 additions & 0 deletions
165
..._services/CognitiveServices - Create a Multilingual Search Engine from Forms.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
--- | ||
title: CognitiveServices - Create a Multilingual Search Engine from Forms | ||
hide_title: true | ||
status: stable | ||
--- | ||
```python | ||
import os | ||
key = os.environ['VISION_API_KEY'] | ||
search_key = os.environ['AZURE_SEARCH_KEY'] | ||
translator_key = os.environ['TRANSLATOR_KEY'] | ||
|
||
search_service = "mmlspark-azure-search" | ||
search_index = "form-demo-index" | ||
``` | ||
|
||
|
||
```python | ||
from pyspark.sql.functions import udf | ||
from pyspark.sql.types import StringType | ||
|
||
def blob_to_url(blob): | ||
[prefix, postfix] = blob.split("@") | ||
container = prefix.split("/")[-1] | ||
split_postfix = postfix.split("/") | ||
account = split_postfix[0] | ||
filepath = "/".join(split_postfix[1:]) | ||
return "https://{}/{}/{}".format(account, container, filepath) | ||
|
||
|
||
df2 = (spark.read.format("binaryFile") | ||
.load("wasbs://[email protected]/forms/*") | ||
.select("path") | ||
.coalesce(24) | ||
.limit(10) | ||
.select(udf(blob_to_url, StringType())("path").alias("url")) | ||
.cache() | ||
) | ||
|
||
``` | ||
|
||
|
||
```python | ||
display(df2) | ||
``` | ||
|
||
|
||
```python | ||
displayHTML(""" | ||
<embed src="https://mmlsparkdemo.blob.core.windows.net/ignite2021/form_svgs/Invoice11205.svg" width="40%"/> | ||
""") | ||
``` | ||
|
||
|
||
```python | ||
from synapse.ml.cognitive import AnalyzeInvoices | ||
|
||
analyzed_df = (AnalyzeInvoices() | ||
.setSubscriptionKey(key) | ||
.setLocation("eastus") | ||
.setImageUrlCol("url") | ||
.setOutputCol("invoices") | ||
.setErrorCol("errors") | ||
.setConcurrency(5) | ||
.transform(df2) | ||
.cache()) | ||
|
||
``` | ||
|
||
|
||
```python | ||
display(analyzed_df) | ||
``` | ||
|
||
|
||
```python | ||
from synapse.ml.cognitive import FormOntologyLearner | ||
|
||
organized_df = (FormOntologyLearner() | ||
.setInputCol("invoices") | ||
.setOutputCol("extracted") | ||
.fit(analyzed_df.limit(10)) | ||
.transform(analyzed_df) | ||
.select("url", "extracted.*") | ||
.cache()) | ||
``` | ||
|
||
|
||
```python | ||
display(organized_df) | ||
``` | ||
|
||
|
||
```python | ||
from pyspark.sql.functions import explode, col | ||
itemized_df = (organized_df | ||
.select("*", explode(col("Items")).alias("Item")) | ||
.drop("Items") | ||
.select("Item.*", "*") | ||
.drop("Item")) | ||
|
||
``` | ||
|
||
|
||
```python | ||
display(itemized_df) | ||
``` | ||
|
||
|
||
```python | ||
display(itemized_df.where(col("ProductCode") == 6)) | ||
``` | ||
|
||
|
||
```python | ||
from synapse.ml.cognitive import Translate | ||
|
||
translated_df = (Translate() | ||
.setSubscriptionKey(translator_key) | ||
.setLocation("eastus") | ||
.setTextCol("Description") | ||
.setErrorCol("TranslationError") | ||
.setOutputCol("output") | ||
.setToLanguage(["zh-Hans", "fr", "ru", "cy"]) | ||
.setConcurrency(5) | ||
.transform(itemized_df) | ||
.withColumn("Translations", col("output.translations")[0]) | ||
.drop("output", "TranslationError") | ||
.cache()) | ||
|
||
``` | ||
|
||
|
||
```python | ||
display(translated_df) | ||
``` | ||
|
||
|
||
```python | ||
from synapse.ml.cognitive import * | ||
from pyspark.sql.functions import monotonically_increasing_id, lit | ||
|
||
(translated_df | ||
.withColumn("DocID", monotonically_increasing_id().cast("string")) | ||
.withColumn("SearchAction", lit("upload")) | ||
.writeToAzureSearch( | ||
subscriptionKey=search_key, | ||
actionCol="SearchAction", | ||
serviceName=search_service, | ||
indexName=search_index, | ||
keyCol="DocID") | ||
) | ||
|
||
``` | ||
|
||
|
||
```python | ||
import requests | ||
url = 'https://{}.search.windows.net/indexes/{}/docs/search?api-version=2019-05-06'.format(search_service, search_index) | ||
requests.post(url, json={"search": "door"}, headers = {"api-key": search_key}).json() | ||
``` | ||
|
||
|
||
```python | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
48 changes: 0 additions & 48 deletions
48
...cs/version-0.9.1/features/http/HttpOnSpark - Working with Arbitrary Web APIs.md
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.