Spaces:
Runtime error
Runtime error
application file
Browse files- app.py +14 -0
- available_slots.json +9 -0
- requirements.txt +3 -0
- utils.py +74 -0
app.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils import chatbot
|
2 |
+
import gradio as gr
|
3 |
+
import logging
|
4 |
+
import traceback
|
5 |
+
|
6 |
+
try:
|
7 |
+
gr.ChatInterface(fn=chatbot,
|
8 |
+
chatbot=gr.Chatbot(height=600),
|
9 |
+
textbox=gr.Textbox(placeholder="Ask me a question related to jewellery.", container=False, scale=7),
|
10 |
+
title="Jewelli Chatbot",
|
11 |
+
).launch()
|
12 |
+
|
13 |
+
except Exception as e:
|
14 |
+
logging.error(f"Error occurred: {e}\n{traceback.format_exc()}")
|
available_slots.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"monday": "9am - 5pm",
|
3 |
+
"tuesday": "9am - 5pm",
|
4 |
+
"wednesday": "9am - 5pm",
|
5 |
+
"thursday": "9am - 5pm",
|
6 |
+
"friday": "9am - 5pm",
|
7 |
+
"saturday": "9am - 5pm",
|
8 |
+
"sunday": "9am - 5pm"
|
9 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.46.1
|
2 |
+
python-dotenv==1.0.0
|
3 |
+
openai==0.28.1
|
utils.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from datetime import datetime
|
5 |
+
import logging
|
6 |
+
import traceback
|
7 |
+
import json
|
8 |
+
|
9 |
+
with open('available_slots.json', 'r') as file:
|
10 |
+
available_slots = str(json.load(file))
|
11 |
+
|
12 |
+
now = datetime.now()
|
13 |
+
date_strng = now.strftime("%d-%m-%Y_%H-%M-%S")
|
14 |
+
|
15 |
+
if not os.path.exists('logs'):
|
16 |
+
os.makedirs('logs')
|
17 |
+
|
18 |
+
|
19 |
+
logging.basicConfig(filename='logs/{}.log'.format(date_strng), level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
|
20 |
+
logging.info('Starting the application')
|
21 |
+
|
22 |
+
load_dotenv()
|
23 |
+
|
24 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
25 |
+
|
26 |
+
context = f"""\
|
27 |
+
You will act as a booking agent, who will book appointments for customers for home repair. \
|
28 |
+
The customer needs to provide a day and time slot for the appointment. If the conversation \
|
29 |
+
strays from booking system, tactfully steer it back. Keep your responses succinct and engaging. \
|
30 |
+
When you determine the customer's preferences. The time slots available from the company are \
|
31 |
+
provided under the "Available Slots" section. The customer can choose from these slots. \
|
32 |
+
|
33 |
+
Available Slots:
|
34 |
+
{available_slots}
|
35 |
+
|
36 |
+
When you think the customer has provided a day and time slot, you can end the conversation by \
|
37 |
+
returning your final response in the JSON format below.
|
38 |
+
|
39 |
+
JSON Format:
|
40 |
+
{{
|
41 |
+
"day": "customer's preferred day goes here",
|
42 |
+
"time": "customer's preferred time goes here
|
43 |
+
}}
|
44 |
+
"""
|
45 |
+
messages = []
|
46 |
+
messages.append({"role": "system", "content": context})
|
47 |
+
|
48 |
+
def json_format(response):
|
49 |
+
dict_start = response.find('{')
|
50 |
+
dict_end = response.rfind('}') + 1
|
51 |
+
json_string = response[dict_start:dict_end]
|
52 |
+
products_dict = json.loads(json_string)
|
53 |
+
return products_dict["productCategory"]
|
54 |
+
|
55 |
+
def chatbot(user_message, history):
|
56 |
+
messages.append({"role": "user", "content": user_message})
|
57 |
+
logging.info("Getting response from gpt-3.5-turbo")
|
58 |
+
response = openai.ChatCompletion.create(
|
59 |
+
model="gpt-3.5-turbo-16k",
|
60 |
+
messages=messages,
|
61 |
+
temperature=0,
|
62 |
+
)
|
63 |
+
messages.append({"role": "assistant", "content": response.choices[0].message.content})
|
64 |
+
|
65 |
+
# try:
|
66 |
+
# product_category = json_format(response.choices[0].message.content)
|
67 |
+
# # image_url = get_product_details(product_category)
|
68 |
+
# # if image_url:
|
69 |
+
# # return (image_url, product_category)
|
70 |
+
# except:
|
71 |
+
# pass
|
72 |
+
|
73 |
+
logging.info("Messages list: {}".format(messages))
|
74 |
+
return response.choices[0].message.content
|