Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,25 +14,50 @@ text_analytics_client = TextAnalyticsClient(endpoint=azure_endpoint, credential=
|
|
| 14 |
# Load Hugging Face chatbot model
|
| 15 |
model_name = "microsoft/DialoGPT-medium"
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
-
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Load Hugging Face chatbot model
|
| 15 |
model_name = "microsoft/DialoGPT-medium"
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
chat_model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
print ("Defining funtion")
|
| 23 |
|
| 24 |
+
def respond(
|
| 25 |
+
message,
|
| 26 |
+
history: list[tuple[str, str]],
|
| 27 |
+
system_message,
|
| 28 |
+
max_tokens
|
| 29 |
+
):
|
| 30 |
+
print ("Response: ")
|
| 31 |
|
| 32 |
+
messages = [{"role": "system", "content": system_message}]
|
| 33 |
+
|
| 34 |
+
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
print ("Getting response", messages)
|
| 37 |
+
|
| 38 |
+
# response = chat_model(messages)
|
| 39 |
+
|
| 40 |
+
response = text_analytics_client.analyze_sentiment([messages])[0]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
print ("Got response", response)
|
| 44 |
+
|
| 45 |
+
return response[-1]['generated_text'][-1]['content']
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 52 |
+
"""
|
| 53 |
+
demo = gr.ChatInterface(
|
| 54 |
+
respond,
|
| 55 |
+
additional_inputs=[
|
| 56 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 57 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
| 58 |
+
],
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|