Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,124 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from langchain.chat_models import ChatOpenAI
|
| 6 |
|
| 7 |
# Set your OpenAI API key
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
theme="compact",
|
| 121 |
-
examples=["list all houses", "provide information on Apartment 2", "execute purchase on apartment 2", "update owner information on apartment"],
|
| 122 |
-
retry_btn=None,
|
| 123 |
-
submit_btn='Send',
|
| 124 |
-
undo_btn=False,
|
| 125 |
-
clear_btn="Clear",
|
| 126 |
-
).launch(share=True)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
import openai
|
| 5 |
import gradio as gr
|
| 6 |
+
from ics import Calendar, Event
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Set your OpenAI API key
|
| 9 |
+
openai.api_key = ("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")
|
| 10 |
+
|
| 11 |
+
# Define a custom calendar class
|
| 12 |
+
class CustomCalendar:
|
| 13 |
+
def __init__(self, file_path):
|
| 14 |
+
self.file_path = file_path
|
| 15 |
+
self.calendar = Calendar()
|
| 16 |
+
|
| 17 |
+
def create_event(self, title, start_time, end_time):
|
| 18 |
+
event = Event()
|
| 19 |
+
event.name = title
|
| 20 |
+
event.begin = start_time
|
| 21 |
+
event.end = end_time
|
| 22 |
+
self.calendar.events.add(event)
|
| 23 |
+
self.save_to_file()
|
| 24 |
+
|
| 25 |
+
def update_event(self, old_title, new_start_time, new_end_time):
|
| 26 |
+
for event in self.calendar.events:
|
| 27 |
+
if event.name == old_title:
|
| 28 |
+
event.begin = new_start_time
|
| 29 |
+
event.end = new_end_time
|
| 30 |
+
self.save_to_file()
|
| 31 |
+
break
|
| 32 |
+
|
| 33 |
+
def get_events(self):
|
| 34 |
+
return list(self.calendar.events)
|
| 35 |
+
|
| 36 |
+
def save_to_file(self):
|
| 37 |
+
with open(self.file_path, "w") as file:
|
| 38 |
+
file.writelines(self.calendar)
|
| 39 |
+
|
| 40 |
+
# Helper function to extract event details from the chatbot's response
|
| 41 |
+
def extract_event_details(response):
|
| 42 |
+
time_pattern = r"(\d{1,2})\s*([ap]m)"
|
| 43 |
+
title_pattern = r"(for|about)\s+(.+)"
|
| 44 |
+
|
| 45 |
+
time_match = re.search(time_pattern, response, re.IGNORECASE)
|
| 46 |
+
title_match = re.search(title_pattern, response, re.IGNORECASE)
|
| 47 |
+
|
| 48 |
+
if time_match:
|
| 49 |
+
hour = int(time_match.group(1))
|
| 50 |
+
meridiem = time_match.group(2).lower()
|
| 51 |
+
if meridiem == "pm" and hour < 12:
|
| 52 |
+
hour += 12
|
| 53 |
+
elif meridiem == "am" and hour == 12:
|
| 54 |
+
hour = 0
|
| 55 |
+
|
| 56 |
+
if title_match:
|
| 57 |
+
title = title_match.group(2)
|
| 58 |
+
|
| 59 |
+
if time_match and title_match:
|
| 60 |
+
start_time = datetime.now().replace(hour=hour, minute=0, second=0, microsecond=0)
|
| 61 |
+
end_time = start_time + timedelta(hours=1)
|
| 62 |
+
return title, start_time, end_time
|
| 63 |
+
|
| 64 |
+
return None, None, None
|
| 65 |
+
|
| 66 |
+
# Function to handle user messages
|
| 67 |
+
def handle_message(texts):
|
| 68 |
+
global calendar
|
| 69 |
+
responses = []
|
| 70 |
+
|
| 71 |
+
for text in texts:
|
| 72 |
+
response = openai.ChatCompletion.create(
|
| 73 |
+
model="gpt-3.5-turbo",
|
| 74 |
+
messages=[{"role": "user", "content": text}],
|
| 75 |
+
temperature=0.7,
|
| 76 |
+
)
|
| 77 |
+
response_text = response.choices[0].message.content
|
| 78 |
+
|
| 79 |
+
# Check for event creation or adjustment
|
| 80 |
+
title, start_time, end_time = extract_event_details(response_text)
|
| 81 |
+
if title and start_time and end_time:
|
| 82 |
+
if "create" in text or "set" in text or "make" in text:
|
| 83 |
+
calendar.create_event(title, start_time, end_time)
|
| 84 |
+
responses.append(f"Appointment created: {title} at {start_time.strftime('%I:%M %p')}")
|
| 85 |
+
elif "change" in text or "adjust" in text or "modify" in text:
|
| 86 |
+
old_title = None
|
| 87 |
+
for event in calendar.get_events():
|
| 88 |
+
if event.name.lower() in response_text.lower():
|
| 89 |
+
old_title = event.name
|
| 90 |
+
break
|
| 91 |
+
if old_title:
|
| 92 |
+
calendar.update_event(old_title, start_time, end_time)
|
| 93 |
+
responses.append(f"Appointment updated: {old_title} to {start_time.strftime('%I:%M %p')}")
|
| 94 |
+
else:
|
| 95 |
+
responses.append("Sorry, I couldn't find the appointment you wanted to adjust.")
|
| 96 |
+
elif "show" in response_text or "list" in response_text or "view" in response_text:
|
| 97 |
+
events = calendar.get_events()
|
| 98 |
+
if events:
|
| 99 |
+
event_list = "\n".join([f"{event.name}: {event.begin.strftime('%I:%M %p')} - {event.end.strftime('%I:%M %p')}" for event in events])
|
| 100 |
+
responses.append(f"Your appointments:\n{event_list}")
|
| 101 |
+
else:
|
| 102 |
+
responses.append("You don't have any appointments scheduled.")
|
| 103 |
else:
|
| 104 |
+
responses.append(response_text)
|
| 105 |
+
|
| 106 |
+
return responses
|
| 107 |
+
|
| 108 |
+
# Define calendar file path
|
| 109 |
+
calendar_file_path = "calendar.ics"
|
| 110 |
+
|
| 111 |
+
# Create a custom calendar instance
|
| 112 |
+
calendar = CustomCalendar(calendar_file_path)
|
| 113 |
+
|
| 114 |
+
# Define Gradio interface
|
| 115 |
+
iface = gr.Interface(
|
| 116 |
+
fn=handle_message,
|
| 117 |
+
inputs=gr.Textbox(lines=3, label="Enter your messages (one per line)"),
|
| 118 |
+
outputs="text",
|
| 119 |
+
title="Calendar Chatbot",
|
| 120 |
+
description="Interact with the chatbot to manage your calendar events.",
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Run the Gradio interface
|
| 124 |
+
iface.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|