musaashaikh commited on
Commit
7bef27d
·
verified ·
1 Parent(s): ce872fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from azure.ai.textanalytics import TextAnalyticsClient
2
+ from azure.core.credentials import AzureKeyCredential
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+
6
+ # Azure Text Analytics setup
7
+ azure_endpoint = "https://t6langservice.cognitiveservices.azure.com/"
8
+ azure_api_key = "3CU1gpUszvl7pLb90Ivdp1Kd5WZc56savzdXOK5GV40JHebnxnxoJQQJ99BAACYeBjFXJ3w3AAAaACOGkRT5"
9
+
10
+ # Authenticate client
11
+ text_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=AzureKeyCredential(azure_api_key))
12
+
13
+
14
+ # Load Hugging Face chatbot model
15
+ model_name = "microsoft/DialoGPT-medium"
16
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
17
+ model = AutoModelForCausalLM.from_pretrained(model_name)
18
+
19
+ # Chatbot logic
20
+ def chatbot_response(input_text):
21
+ # Hugging Face response
22
+ input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
23
+ chat_output = model.generate(input_ids, max_length=50, pad_token_id=tokenizer.eos_token_id)
24
+ hf_response = tokenizer.decode(chat_output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
25
+
26
+ # Azure Text Analytics: Analyze sentiment of user input
27
+ try:
28
+ response = text_analytics_client.analyze_sentiment([input_text])[0]
29
+ sentiment = response.sentiment
30
+ azure_analysis = f"The sentiment of your input is: {sentiment}."
31
+ except Exception as e:
32
+ azure_analysis = f"Error analyzing sentiment: {str(e)}"
33
+
34
+ # Combine responses
35
+ return f"Hugging Face response: {hf_response}\nAzure analysis: {azure_analysis}"
36
+
37
+ # Chat loop
38
+ while True:
39
+ user_input = input("You: ")
40
+ if user_input.lower() in ["exit", "quit"]:
41
+ print("Exiting chatbot. Goodbye!")
42
+ break
43
+ print(chatbot_response(user_input))
44
+