-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.py
39 lines (33 loc) · 1.55 KB
/
script.py
1
2
3
4
5
6
7
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
import os
import gradio as gr
from datetime import datetime
params = {
'timecontext': True,
'datecontext': False,
}
def custom_generate_chat_prompt(user_input, state, **kwargs):
'''
This extension will replace all instances of
{{time}} and {{date}} with the current
time and date, respectively
Additionall replaces {{weekday}} with the day of the week.
'''
state['context'] = state['context'].replace('{{time}}',datetime.now().strftime("%I:%M %p"))
state['context'] = state['context'].replace('{{date}}',datetime.now().strftime("%B %d, %Y"))
state['context'] = state['context'].replace('{{weekday}}',("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")[datetime.now().weekday()])
# print(f'<<context, changed>>')
# print(f'{state["context"]}')
return
def input_modifier(string):
"""
This function adds time or date context to each prompt.
"""
if params['timecontext'] : string += f' [Current time: {datetime.now().strftime("%I:%M %p")}]'
if params['datecontext'] : string += f' [Current date: {datetime.now().strftime("%B %d, %Y")}]'
return string
def ui():
with gr.Accordion("Prompt context"):
timecontext = gr.Checkbox(value=params['timecontext'], label='Add current time context to the end of the prompt')
datecontext = gr.Checkbox(value=params['datecontext'], label='Add current date context to the end of the prompt')
timecontext.change(lambda x: params.update({"timecontext": x}), timecontext, None)
datecontext.change(lambda x: params.update({"datecontext": x}), datecontext, None)