Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243import 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 clienttext_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=AzureKeyCredential(azure_api_key))def load_model():# Load a pre-trained HuggingFace pipeline for sentiment analysismodel_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")return model_pipelinedef classify_text(model, text):# Use the loaded model to classify textresult = model(text)return resultdef classify_text_azure(model, text):# Ensure input is in the correct format (list of strings)documents = [text] # Wrap the input string in a listresult = 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 modelmodel = load_model()# Define the Gradio interfaceinterface = gr.Interface(fn=lambda text: classify_text_azure(model, text),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.")# Launch the Gradio app1234567891011121314151617181920212223242526272829303132333435363738394041import gradio as grfrom azure.ai.textanalytics import TextAnalyticsClientfrom azure.core.credentials import AzureKeyCredential# Azure Text Analytics setupazure_endpoint = "https://t6langservice.cognitiveservices.azure.com/"azure_api_key = "your-azure-api-key" # Replace with your actual API keytext_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=AzureKeyCredential(azure_api_key))def 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 responsereturn [{"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 main():"""Launch the Gradio interface for sentiment analysis."""# Define the Gradio interfaceinterface = gr.Interface(fn=classify_text_azure,inputs=gr.Textbox(lines=2, placeholder="Enter Text Here..."),outputs="json",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 app
app.py+24-26