musaashaikh commited on
Commit
cd4b046
·
verified ·
1 Parent(s): 7aa11f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -1,43 +1,41 @@
1
  import gradio as gr
2
  from azure.ai.textanalytics import TextAnalyticsClient
3
  from azure.core.credentials import AzureKeyCredential
4
- from transformers import pipeline
5
 
6
  # Azure Text Analytics setup
7
  azure_endpoint = "https://t6langservice.cognitiveservices.azure.com/"
8
- azure_api_key = "3CU1gpUszvl7pLb90Ivdp1Kd5WZc56savzdXOK5GV40JHebnxnxoJQQJ99BAACYeBjFXJ3w3AAAaACOGkRT5"
9
- # Authenticate client
10
  text_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=AzureKeyCredential(azure_api_key))
11
 
12
- def load_model():
13
- # Load a pre-trained HuggingFace pipeline for sentiment analysis
14
- model_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
15
- return model_pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def classify_text(model, text):
18
- # Use the loaded model to classify text
19
- result = model(text)
20
- return result
21
-
22
-
23
- def classify_text_azure(model, text):
24
- # Ensure input is in the correct format (list of strings)
25
- documents = [text] # Wrap the input string in a list
26
- result = text_analytics_client.analyze_sentiment(documents=documents)
27
- return [{"id": i, "sentiment": doc.sentiment, "confidence_scores": doc.confidence_scores} for i, doc in enumerate(result)]
28
-
29
-
30
  def main():
31
- # Load the model
32
- model = load_model()
33
-
34
  # Define the Gradio interface
35
  interface = gr.Interface(
36
- fn=lambda text: classify_text_azure(model, text),
37
  inputs=gr.Textbox(lines=2, placeholder="Enter Text Here..."),
38
  outputs="json",
39
- title="Text Classification with HuggingFace",
40
- description="This interface uses a HuggingFace model to classify text sentiments. Enter a sentence to see its classification."
41
  )
42
 
43
  # Launch the Gradio app
 
1
  import gradio as gr
2
  from azure.ai.textanalytics import TextAnalyticsClient
3
  from azure.core.credentials import AzureKeyCredential
 
4
 
5
  # Azure Text Analytics setup
6
  azure_endpoint = "https://t6langservice.cognitiveservices.azure.com/"
7
+ azure_api_key = "your-azure-api-key" # Replace with your actual API key
 
8
  text_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=AzureKeyCredential(azure_api_key))
9
 
10
+ def classify_text_azure(text):
11
+
12
+ try:
13
+ # Ensure input is in the correct format (list of strings)
14
+ documents = [text]
15
+ result = text_analytics_client.analyze_sentiment(documents=documents)
16
+
17
+ # Format the response
18
+ return [
19
+ {
20
+ "id": i,
21
+ "sentiment": doc.sentiment,
22
+ "confidence_scores": doc.confidence_scores
23
+ } for i, doc in enumerate(result)
24
+ ]
25
+ except Exception as e:
26
+ return {"error": str(e)}
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def main():
29
+ """
30
+ Launch the Gradio interface for sentiment analysis.
31
+ """
32
  # Define the Gradio interface
33
  interface = gr.Interface(
34
+ fn=classify_text_azure,
35
  inputs=gr.Textbox(lines=2, placeholder="Enter Text Here..."),
36
  outputs="json",
37
+ title="Text Classification with Azure Text Analytics",
38
+ description="This interface uses Azure Text Analytics to classify text sentiments. Enter a sentence to see its classification."
39
  )
40
 
41
  # Launch the Gradio app