Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from azure.core.credentials import AzureKeyCredential
|
| 3 |
+
from azure.ai.textanalytics import TextAnalyticsClient
|
| 4 |
+
|
| 5 |
+
def AnalyzeSentiment(endpoint, key, statements):
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
if (endpoint == '' or key == ''):
|
| 9 |
+
output = "*** Please provide EndPoint URL and API_KEY ***"
|
| 10 |
+
|
| 11 |
+
else:
|
| 12 |
+
try:
|
| 13 |
+
endpoint = endpoint
|
| 14 |
+
key = key
|
| 15 |
+
|
| 16 |
+
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
|
| 17 |
+
|
| 18 |
+
documents = tuple(statements.split("\n"))
|
| 19 |
+
|
| 20 |
+
result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True)
|
| 21 |
+
docs = [doc for doc in result if not doc.is_error]
|
| 22 |
+
|
| 23 |
+
reviews = ()
|
| 24 |
+
for idx, doc in enumerate(docs):
|
| 25 |
+
reviews = reviews + (f"Document text: {documents[idx]}",)
|
| 26 |
+
reviews = reviews + (f"Overall sentiment: {doc.sentiment} {doc.confidence_scores}",)
|
| 27 |
+
|
| 28 |
+
output = "\n\n".join(reviews)
|
| 29 |
+
|
| 30 |
+
except:
|
| 31 |
+
output = "*** Please check EndPoint URL and API_KEY ***"
|
| 32 |
+
return output
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface( fn = AnalyzeSentiment, inputs= [ gr.Textbox("https://t9xt.cognitiveservices.azure.com/"), gr.Textbox("46758f0003684f1b8c6fb73dfd7f98db"), gr.Textbox()], outputs= gr.Textbox()).launch()
|