Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364from azure.ai.textanalytics import TextAnalyticsClientfrom azure.core.credentials import AzureKeyCredentialfrom transformers import AutoModelForCausalLM, AutoTokenizerimport gradio as gr# 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))# Load Hugging Face chatbot modelmodel_name = "microsoft/DialoGPT-medium"tokenizer = AutoTokenizer.from_pretrained(model_name)chat_model = AutoModelForCausalLM.from_pretrained(model_name)print ("Defining funtion")def respond(message,history: list[tuple[str, str]],system_message,max_tokens):print ("Response: ")messages = [{"role": "system", "content": system_message}]messages.append({"role": "user", "content": message})print ("Getting response", messages)# response = chat_model(messages)response = text_analytics_client.analyze_sentiment([messages])[0]print ("Got response", response)return response[-1]['generated_text'][-1]['content']"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")],)if __name__ == "__main__":demo.launch()12345678910111213141516171819202122232425262728293031import gradio as grfrom transformers import pipelinedef 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 main():# Load the modelmodel = load_model()# Define the Gradio interfaceinterface = gr.Interface(fn=lambda text: classify_text(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 appinterface.launch()if __name__ == "__main__":main()
app.py+28-61