File size: 7,372 Bytes
d0020e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec91473
b896b78
11c3841
b896b78
 
100826d
b020f8f
384b634
 
 
 
b896b78
84ab7d1
 
100826d
84ab7d1
 
 
b896b78
 
01bef6f
 
 
 
 
495a4d7
100826d
384b634
 
 
 
 
 
 
84ab7d1
5cf9740
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b642a68
6930051
 
 
 
 
384b634
 
 
 
 
 
5cf9740
 
 
f03da32
 
f50380b
f03da32
 
 
 
 
84ab7d1
 
 
 
 
 
 
 
 
 
 
 
b04bdf0
 
 
 
 
b896b78
f03da32
 
 
 
310bca6
f03da32
 
 
 
 
310bca6
f03da32
310bca6
f03da32
 
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
 # elif response[1] == "create_reminder":
        #     # if values[4] == 'notime':
        #     #     return JsonResponse({
        #     #         'messages': 'At what time do you want me to remind you?',
        #     #     })
        #     # else:
        #     return JsonResponse({
        #         'messages': response[0],
        #         'action': response[1],
        #         'function': {
        #                 'id': idval,
        #                 'sound': 'General',
        #                 'subTitle': 'Task',
        #                 'type': 'Note',
        #                 'title': values[0],
        #                 'description': '',
        #                 'time': values[1].upper(),
        #                 'timestamp' : datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f"),
        #                 'enable': False,
        #                 'report': [],
        #                 'icon': 'https://firebasestorage.googleapis.com/v0/b/cosmo-f5007.appspot.com/o/categories%2FIcons%2Ftaskicon.svg?alt=media&token=56f3fc55-8eda-4463-bceb-7bf3198dff3c',
        #                 'color': 'FFD700',
        #                 'sharedToMe': [],
        #                 'sharedByMe': [],
        #                 'repeat': 'Once',
        #                 'reminders': [{
        #                     'time': values[1].upper(),
        #                     'enable': False,
        #                     'repeat': 'Once',
        #                     'title': values[0],
        #                     'id': idval,
        #                     'note': '',
        #                     'dates': [],
        #                     }],
        #             }
        #     })
            
        # elif response[1] == 'create_todo':
        #     return JsonResponse({
        #     'messages': response[0],
        #     'action': response[1],
        #     'function': {
        #         'name': 'defaulttodo',
        #         'id': idval,
        #         'subTasks': [{
        #             'task':values[0].replace("create a todo", ""),
        #             'done': False
        #         }],
        #         'shared': [],
        #         'sharedByMe': [],
        #             },
        #     })
            
        # elif response[1] == 'create_note':
        #     return JsonResponse({
        #     'messages': response[0],
        #     'action': response[1],
        #     'function': {
        #         'title': 'defaultnote',
        #         'id': idval,
        #         'type': 'Note',
        #         'description': values[0],
        #         'time': datetime.now().strftime("%d/%m/%Y"),
        #         'mainTime': datetime.now().strftime("%I:%M %p"),
        #         'complete': False,
        #         'shared': [],
        #         'sharedByMe': [],
        #         }
        #     })


import gradio as gr
import requests
import time 
import os
import json
import google.generativeai as genai
import random
from getvalues import getValues
from datetime import datetime, timedelta
import uuid
import re

genai.configure(
    api_key=os.environ['API_KEY'])

model = genai.GenerativeModel(
    model_name='gemini-pro')




    
    
def responsenew(data):
    
    # query = data
    
    # respo = model.generate_content(
    # query,
    # generation_config={
    #     'temperature': 0,
    #     'max_output_tokens': 100
    # }
    # )

    idval = random.randint(1, 1000000000)
    print(f"\n\n{data}")
    newdata = data.replace("'", '"')
    items = json.loads(newdata)
    query = items['text']
    query = query.lower()
    # try:
    #     items = json.loads(newdata)
    #     query = items['text']
    #     query = query.lower()
    #     print(query)
    # except json.JSONDecodeError as e:
    #     print("Invalid JSON:", e)
    response = model.generate_content(query)
    print(f"\n{response.text}")
    if query is not None:
        if "remind me" in query:
            values = getValues(query)
            if values[0] is not None:
                msg = values[0]
            else:
                msg = "Reminder Alert"
            if values[1] is not None:
                time = values[1]
            else:
                time = "5:00 PM"
                # time = time1 + timedelta(hours=1)
                # time = time2.strftime("%d-%m-%Y %H:%M:%S.%f")
            if values[2] is not None:
                day = values[2]
            else:
                day = "today"
            if values[3] is not None:
                date = values[3]
            else:
                date = datetime.today()
            if values[4] is not None:
                reps = values[4]
            else:
                reps = "Once"
            respo = {
                'message': f"Message: {msg} \nTime: {time} \nDay: {day} \nCreated Successfully.",
                'action': "create_reminder",
                'function': {
                        'id': idval,
                        'sound': 'General',
                        'subTitle': 'Task',
                        'type': 'Note',
                        'title': msg,
                        'description': '',
                        'time': time.upper(),
                        'timestamp' : datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f"),
                        'enable': False,
                        'report': [],
                        'icon': 'https://firebasestorage.googleapis.com/v0/b/cosmo-f5007.appspot.com/o/categories%2FIcons%2Ftaskicon.svg?alt=media&token=56f3fc55-8eda-4463-bceb-7bf3198dff3c',
                        'color': 'FFD700',
                        'sharedToMe': [],
                        'sharedByMe': [],
                        'repeat': reps,
                        'reminders': [{
                            'time': time.upper(),
                            'enable': False,
                            'repeat': reps,
                            'title': msg,
                            'id': idval,
                            'note': '',
                            'dates': [],
                            }],
                    }
            }
        else:
            respo = {
                "message": response.text,
                "action": "nothing",
                "function": "nothing",
            }
    else:
        respo = {
            "message": "Whoops, seems like we're a bit overloaded! Don't worry, your request is in the queue and we'll get back to you as soon as possible.",
            "action": "nothing",
            "function": "nothing",
        }


    return json.dumps(respo)


gradio_interface = gr.Interface(
  fn = responsenew,
  inputs = "text",
  outputs = "text" 
)
gradio_interface.launch()












    # remind_val = ["create a reminder", "create reminder", "remind me"]
    # if remind_val in data:
    #     return "Reminder created!"
    # else:
    #     return bardChat(data)

# with gr.Blocks() as demo:
#     chatbot = gr.Chatbot()
#     msg = gr.Textbox()
#     clear = gr.ClearButton([msg, chatbot])

#     def respond(message, chat_history):
#         bot_message = responsenew(message)
#         chat_history.append((message, bot_message))
#         time.sleep(2)
#         return "", chat_history

#     msg.submit(respond, [msg, chatbot], [msg, chatbot])

# if __name__ == "__main__":
#     demo.launch()