Spaces:
Sleeping
Sleeping
Update app.py
Browse files12344568978912131415101112131415161718192021222324252627171819202122232425262728292831323329303132333634353639403738394041import gradio as grfrom azure.ai.textanalytics import TextAnalyticsClientfrom azure.core.credentials import AzureKeyCredentialfrom transformers import pipeline
# Azure Text Analytics setupazure_endpoint = "https://t6langservice.cognitiveservices.azure.com/"azure_api_key = "3CU1gpUszvl7pLb90Ivdp1Kd5WZc56savzdXOK5GV40JHebnxnxoJQQJ99BAACYeBjFXJ3w3AAAaACOGkRT5"# Authenticate clientazure_api_key = "your-azure-api-key" # Replace with your actual API keytext_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=AzureKeyCredential(azure_api_key))
def load_model(): # Load a pre-trained HuggingFace pipeline for sentiment analysis model_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") return model_pipelinedef classify_text_azure(text): try: # Ensure input is in the correct format (list of strings) documents = [text] result = text_analytics_client.analyze_sentiment(documents=documents) # Format the response return [ { "id": i, "sentiment": doc.sentiment, "confidence_scores": doc.confidence_scores } for i, doc in enumerate(result) ] except Exception as e: return {"error": str(e)}
def classify_text(model, text): # Use the loaded model to classify text result = model(text) return result
def classify_text_azure(model, text): # Ensure input is in the correct format (list of strings) documents = [text] # Wrap the input string in a list result = text_analytics_client.analyze_sentiment(documents=documents) return [{"id": i, "sentiment": doc.sentiment, "confidence_scores": doc.confidence_scores} for i, doc in enumerate(result)]
def main(): # Load the model model = load_model()
""" Launch the Gradio interface for sentiment analysis. """ # Define the Gradio interface interface = gr.Interface( fn=lambda text: classify_text_azure(model, text), fn=classify_text_azure, inputs=gr.Textbox(lines=2, placeholder="Enter Text Here..."), outputs="json", title="Text Classification with HuggingFace", description="This interface uses a HuggingFace model to classify text sentiments. Enter a sentence to see its classification." title="Text Classification with Azure Text Analytics", description="This interface uses Azure Text Analytics to classify text sentiments. Enter a sentence to see its classification." )
# Launch the Gradio appapp.py+24-26