Upload model_api_demo.py
Browse files- model_api_demo.py +48 -0
model_api_demo.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
chat_history = [] # Stores the conversation history
|
4 |
+
|
5 |
+
API_URL = "API_ENDPOINT_URL"
|
6 |
+
headers = {
|
7 |
+
"Accept" : "application/json",
|
8 |
+
"Authorization": "Bearer API_KEY",
|
9 |
+
"Content-Type": "application/json"
|
10 |
+
}
|
11 |
+
|
12 |
+
def query(payload):
|
13 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
14 |
+
return response.json()
|
15 |
+
|
16 |
+
def chat(user_q):
|
17 |
+
try:
|
18 |
+
chat_history.append(user_q) # Append user query to chat history
|
19 |
+
output = query({
|
20 |
+
"inputs": chat_history, # Send chat history as input data
|
21 |
+
"parameters": {}
|
22 |
+
})
|
23 |
+
generated_reply = output[0]['generated_reply'] # Extract generated reply
|
24 |
+
chat_history.append(generated_reply) # Append generated reply to chat history
|
25 |
+
except Exception as ex:
|
26 |
+
print(ex)
|
27 |
+
chat(user_q) # Re-run the chat function with the same user query
|
28 |
+
|
29 |
+
return generated_reply # Return the generated reply
|
30 |
+
|
31 |
+
|
32 |
+
user_q = '<user>: What are the most visited places in Dublin?' # User query
|
33 |
+
reply = chat(user_q) # Initiate conversation
|
34 |
+
|
35 |
+
user_q = '<user>: Which place is the oldest?' # Next user query
|
36 |
+
reply = chat(user_q) # Continue conversation
|
37 |
+
|
38 |
+
user_q = '<user>: Who built it?' # Next user query
|
39 |
+
reply = chat(user_q) # Continue conversation
|
40 |
+
|
41 |
+
user_q = '<user>: Do you recommend any other places to visit?'
|
42 |
+
reply = chat(user_q)
|
43 |
+
|
44 |
+
user_q = '<user>: What is the average budget for a one-week visit?'
|
45 |
+
reply = chat(user_q)
|
46 |
+
|
47 |
+
|
48 |
+
print(chat_history) # Print the entire conversation history
|