Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
API_URL = "https://kp4xdy196cw81uf3.us-east-1.aws.endpoints.huggingface.cloud"
|
| 5 |
+
headers = {
|
| 6 |
+
"Accept" : "application/json",
|
| 7 |
+
"Authorization": "Bearer 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",
|
| 8 |
+
"Content-Type": "application/json"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def query(payload):
|
| 12 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 13 |
+
return response.json()
|
| 14 |
+
|
| 15 |
+
def generate_response(text):
|
| 16 |
+
input = {
|
| 17 |
+
"inputs": text,
|
| 18 |
+
"parameters": {
|
| 19 |
+
"max_new_tokens" : 128,
|
| 20 |
+
"top_k": 10,
|
| 21 |
+
"top_p": 0.95,
|
| 22 |
+
"typical_p": 0.95,
|
| 23 |
+
"temperature": 0.01,
|
| 24 |
+
"repetition_penalty": 1.03,
|
| 25 |
+
"stop" : ["/nHuman:", "/nUser:", "<end of message>\n"]
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
output = query(input)
|
| 30 |
+
response = output[0]["generated_text"]
|
| 31 |
+
response = response.replace(text, "")
|
| 32 |
+
splitResponse = response.split("Human:")
|
| 33 |
+
return splitResponse[0]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def format_chat_prompt(message, chat_history):
|
| 37 |
+
prompt = """ Do not repeat questions and do not generate answer for user/human.
|
| 38 |
+
|
| 39 |
+
You are a helpful hotel booking asssitant.
|
| 40 |
+
Below is an instruction that describes a task.
|
| 41 |
+
Write a response that appropriately completes the request.
|
| 42 |
+
Reply with the most helpful and logic answer. During the conversation you need to ask the user
|
| 43 |
+
the following questions to complete the hotel booking task.
|
| 44 |
+
1) Where would you like to stay and when?
|
| 45 |
+
2) How many people are staying in the room?
|
| 46 |
+
3) Do you prefer any ammenities like breakfast included or gym?
|
| 47 |
+
4) What is your name, your email address and phone number?
|
| 48 |
+
When name, email and phone number is provided, you, a hotel booking assistant, shouldd respond with a thankful answer.
|
| 49 |
+
When the user says please book the room or Yes, book it, you should repsond with : "Yes,I booked the rooom."
|
| 50 |
+
"""
|
| 51 |
+
for turn in chat_history:
|
| 52 |
+
user_message, bot_message = turn
|
| 53 |
+
prompt = f"{prompt}\nHuman: {user_message}\nAssistant: {bot_message}"
|
| 54 |
+
prompt = f"{prompt}\nHuman: {message}\nAssistant:"
|
| 55 |
+
return prompt
|
| 56 |
+
|
| 57 |
+
def chat_output(message, history):
|
| 58 |
+
print(message)
|
| 59 |
+
prompt = format_chat_prompt(message,history)
|
| 60 |
+
result = generate_response(text = prompt)
|
| 61 |
+
return result
|
| 62 |
+
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
|
| 65 |
+
#chatbot_component = gr.Chatbot(height=300, label = "history")
|
| 66 |
+
#textbox_component = gr.Textbox(placeholder="Can I help you to book a hotel?", container=False, label = "input", scale=7)
|
| 67 |
+
|
| 68 |
+
demo.chatbot_interface = gr.ChatInterface(
|
| 69 |
+
fn=chat_output,
|
| 70 |
+
examples = ["Hello I would like to book a hotel room.", "Hello I want to stay in Nuremberg in 30th of May until 1st of June." , "2 people, me and my partner."
|
| 71 |
+
"2 people, me and my partner. I would like a breakfast included.", " I would like a breakfast included.", "Just me.I am travelling alone."],
|
| 72 |
+
#outputs=chatbot_component,
|
| 73 |
+
title = "Hotel Booking Assistant Chat 🤗",
|
| 74 |
+
description = "I am your hotel booking assistant. Feel free to start chatting with me."
|
| 75 |
+
)
|
| 76 |
+
demo.launch(debug=True)
|