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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from groq import Groq
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import random
|
6 |
+
from getvalues import getValues
|
7 |
+
from datetime import datetime, timedelta
|
8 |
+
import uuid
|
9 |
+
import re
|
10 |
+
|
11 |
+
# Set your API key
|
12 |
+
# Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
|
13 |
+
# GOOGLE_API_KEY=os.getenv('GEMINI_KEY')
|
14 |
+
|
15 |
+
# genai.configure(api_key=GOOGLE_API_KEY)
|
16 |
+
# model = genai.GenerativeModel('gemini-pro')
|
17 |
+
|
18 |
+
# Select the PaLM 2 model
|
19 |
+
# model = 'models/text-bison-001'
|
20 |
+
|
21 |
+
|
22 |
+
api1 = os.getenv("Groq_key")
|
23 |
+
api2 = os.getenv("GRoq_key")
|
24 |
+
|
25 |
+
apis = [
|
26 |
+
api1,
|
27 |
+
api2,
|
28 |
+
]
|
29 |
+
|
30 |
+
|
31 |
+
# contx = """Act as a friendly personal assistant.
|
32 |
+
# You help users manage their daily tasks by providing them with a variety of features.
|
33 |
+
# Below are the list of features you have: \n
|
34 |
+
# - You can create goals\n
|
35 |
+
# - You can share goals with friends\n
|
36 |
+
# - You can create reminders\n
|
37 |
+
# - You can create routines\n
|
38 |
+
# - You can share reminders with users friends\n
|
39 |
+
# - You can share routines with users friends\n
|
40 |
+
# - You can create todo lists\n
|
41 |
+
# - You can share todo lists with users friends\n
|
42 |
+
# - You can create groups\n
|
43 |
+
# - You can share groups with users friends\n
|
44 |
+
# - You can create communities\n
|
45 |
+
# - You can create notes for user\n
|
46 |
+
# - You can share notes with users friends\n
|
47 |
+
# - You can publish posts on timeline\n
|
48 |
+
# - You can invite friends to this(CosmoAI) app\n
|
49 |
+
# - You can help users purchase coins\n
|
50 |
+
# - You can view your friends\n
|
51 |
+
# - You can view your groups\n
|
52 |
+
# - You can view your communities\n
|
53 |
+
# - You can view your shared reminders\n
|
54 |
+
# - You can view your shared routines\n
|
55 |
+
# - You can view your todo lists\n
|
56 |
+
# - You can view your shared todo lists\n
|
57 |
+
# - You can view your shared notes\n
|
58 |
+
# - You can start a timer or a stopwatch\n\n"""
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
def responsenew(data):
|
64 |
+
idval = random.randint(1, 1000000000)
|
65 |
+
print(f"\n\n{data}")
|
66 |
+
newdata = data.replace("'", '"')
|
67 |
+
items = json.loads(newdata)
|
68 |
+
query = items['text']
|
69 |
+
query = query.lower()
|
70 |
+
# try:
|
71 |
+
# items = json.loads(newdata)
|
72 |
+
# query = items['text']
|
73 |
+
# query = query.lower()
|
74 |
+
# print(query)
|
75 |
+
# except json.JSONDecodeError as e:
|
76 |
+
# print("Invalid JSON:", e)
|
77 |
+
while True:
|
78 |
+
for api in apis:
|
79 |
+
client = Groq(
|
80 |
+
api_key=api,
|
81 |
+
) # Configure the model with the API key
|
82 |
+
# query = st.text_input("Enter your query")
|
83 |
+
# prmptquery= f"Answer this query with wisdom, love and compassion, in context to bhagwat geeta, provide references of shloks from chapters of bhagwat geeta which is relevant to the query. Query= {query}"
|
84 |
+
try:
|
85 |
+
response = client.chat.completions.create(
|
86 |
+
messages=[
|
87 |
+
{
|
88 |
+
"role": "user",
|
89 |
+
"content": query,
|
90 |
+
}
|
91 |
+
],
|
92 |
+
model="mixtral-8x7b-32768",
|
93 |
+
)
|
94 |
+
answer = response.choices[0].message.content
|
95 |
+
except Exception as e:
|
96 |
+
print(f"API call failed for: {e}")
|
97 |
+
if answer:
|
98 |
+
break
|
99 |
+
if answer:
|
100 |
+
break
|
101 |
+
print(f"\n{answer}")
|
102 |
+
if query is not None:
|
103 |
+
if "remind me" in query:
|
104 |
+
values = getValues(query)
|
105 |
+
if values[0] is not None:
|
106 |
+
msg = values[0]
|
107 |
+
else:
|
108 |
+
msg = "Reminder Alert"
|
109 |
+
if values[1] is not None:
|
110 |
+
time = values[1]
|
111 |
+
else:
|
112 |
+
time = "5:00 PM"
|
113 |
+
# time = time1 + timedelta(hours=1)
|
114 |
+
# time = time2.strftime("%d-%m-%Y %H:%M:%S.%f")
|
115 |
+
if values[2] is not None:
|
116 |
+
day = values[2]
|
117 |
+
else:
|
118 |
+
day = "today"
|
119 |
+
if values[3] is not None:
|
120 |
+
date = values[3]
|
121 |
+
else:
|
122 |
+
date = datetime.today()
|
123 |
+
if values[4] is not None:
|
124 |
+
reps = values[4]
|
125 |
+
else:
|
126 |
+
reps = "Once"
|
127 |
+
respo = {
|
128 |
+
'message': f"Message: {msg} \nTime: {time} \nDay: {day} \nCreated Successfully.",
|
129 |
+
'action': "create_reminder",
|
130 |
+
'function': {
|
131 |
+
'id': idval,
|
132 |
+
'sound': 'General',
|
133 |
+
'subTitle': 'Task',
|
134 |
+
'type': 'Note',
|
135 |
+
'title': msg,
|
136 |
+
'description': '',
|
137 |
+
'time': time.upper(),
|
138 |
+
'timestamp' : datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f"),
|
139 |
+
'enable': False,
|
140 |
+
'report': [],
|
141 |
+
'icon': 'https://firebasestorage.googleapis.com/v0/b/cosmo-f5007.appspot.com/o/categories%2FIcons%2Ftaskicon.svg?alt=media&token=56f3fc55-8eda-4463-bceb-7bf3198dff3c',
|
142 |
+
'color': 'FFD700',
|
143 |
+
'sharedToMe': [],
|
144 |
+
'sharedByMe': [],
|
145 |
+
'repeat': reps,
|
146 |
+
'reminders': [{
|
147 |
+
'time': time.upper(),
|
148 |
+
'enable': False,
|
149 |
+
'repeat': reps,
|
150 |
+
'title': msg,
|
151 |
+
'id': idval,
|
152 |
+
'note': '',
|
153 |
+
'dates': [],
|
154 |
+
}],
|
155 |
+
}
|
156 |
+
}
|
157 |
+
|
158 |
+
elif "add to do" in query:
|
159 |
+
replaced_string = text.replace("add todo", "").replace("add to do","")
|
160 |
+
# 1. Replace "note down" with an empty string:
|
161 |
+
replaced_string = text.replace("note down", "")
|
162 |
+
|
163 |
+
# 2. Split the string into words:
|
164 |
+
words = replaced_string.split()
|
165 |
+
|
166 |
+
# 3. Extract the first two words:
|
167 |
+
first_two_words = words[:2] # Get the first two elements of the list
|
168 |
+
|
169 |
+
# 4. Join the words back into a string (if needed):
|
170 |
+
result_string = " ".join(first_two_words)
|
171 |
+
respo = {
|
172 |
+
'message': "Todo added!",
|
173 |
+
'action': "create_todo",
|
174 |
+
'function': {
|
175 |
+
'name': result_string,
|
176 |
+
'id': idval,
|
177 |
+
'subTasks': [{
|
178 |
+
'task':replaced_string,
|
179 |
+
'done': False
|
180 |
+
}],
|
181 |
+
'shared': [],
|
182 |
+
'sharedByMe': [],
|
183 |
+
},
|
184 |
+
}
|
185 |
+
elif "note down" in query:
|
186 |
+
# 1. Replace "note down" with an empty string:
|
187 |
+
replaced_string = text.replace("note down", "")
|
188 |
+
|
189 |
+
# 2. Split the string into words:
|
190 |
+
words = replaced_string.split()
|
191 |
+
|
192 |
+
# 3. Extract the first two words:
|
193 |
+
first_two_words = words[:2] # Get the first two elements of the list
|
194 |
+
|
195 |
+
# 4. Join the words back into a string (if needed):
|
196 |
+
result_string = " ".join(first_two_words)
|
197 |
+
respo = {
|
198 |
+
'message': "Got it! Saved to your notes.",
|
199 |
+
'action': "create_note",
|
200 |
+
'function': {
|
201 |
+
'title': result_string,
|
202 |
+
'id': idval,
|
203 |
+
'type': 'Note',
|
204 |
+
'description': replaced_string,
|
205 |
+
'time': datetime.now().strftime("%d/%m/%Y"),
|
206 |
+
'mainTime': datetime.now().strftime("%I:%M %p"),
|
207 |
+
'complete': False,
|
208 |
+
'shared': [],
|
209 |
+
'sharedByMe': [],
|
210 |
+
}
|
211 |
+
}
|
212 |
+
elif "add coins" in query:
|
213 |
+
respo = {
|
214 |
+
"message": "Click the button below to view Premium Services and Coin Recharge options: ",
|
215 |
+
"action": "payment",
|
216 |
+
"function": "nothing",
|
217 |
+
}
|
218 |
+
elif "show my friends" in query:
|
219 |
+
respo = {
|
220 |
+
"message": "Here's the list of your friends: ",
|
221 |
+
"action": "show_friends",
|
222 |
+
"function": "nothing",
|
223 |
+
}
|
224 |
+
elif "show my groups" in query:
|
225 |
+
respo = {
|
226 |
+
"message": "You are member of following groups: ",
|
227 |
+
"action": "show_mygroups",
|
228 |
+
"function": "nothing",
|
229 |
+
}
|
230 |
+
elif "show my communities" in query:
|
231 |
+
respo = {
|
232 |
+
"message": "You are part of following communities🫶: ",
|
233 |
+
"action": "show_mycommunities",
|
234 |
+
"function": "nothing",
|
235 |
+
}
|
236 |
+
elif "show shared reminders" in query:
|
237 |
+
respo = {
|
238 |
+
"message": "Here's the list of your shared reminders: ",
|
239 |
+
"action": "shared_reminders",
|
240 |
+
"function": "nothing",
|
241 |
+
}
|
242 |
+
elif "create a post" in query:
|
243 |
+
respo = {
|
244 |
+
"message": "Sure!",
|
245 |
+
"action": "create_post",
|
246 |
+
"function": "nothing",
|
247 |
+
}
|
248 |
+
elif "show my routines" in query:
|
249 |
+
respo = {
|
250 |
+
"message": "Here's the list of your routines: ",
|
251 |
+
"action": "myroutines",
|
252 |
+
"function": "nothing",
|
253 |
+
}
|
254 |
+
# elif "what is your name" or "what's your name" or "who are you" in data:
|
255 |
+
# respo = {
|
256 |
+
# "message": "My name is Cosmo. I am your friendly personal assistant.",
|
257 |
+
# "action": "nothing",
|
258 |
+
# "function": "nothing",
|
259 |
+
# }
|
260 |
+
elif "notify" in query:
|
261 |
+
respo = {
|
262 |
+
"message": "Select your friends to notify",
|
263 |
+
"action": "send_notify",
|
264 |
+
"function": "nothing",
|
265 |
+
}
|
266 |
+
elif "show actions" in query:
|
267 |
+
respo = {
|
268 |
+
"message": "Here is list of actions you can use:",
|
269 |
+
"action": "show_actions",
|
270 |
+
"function": "nothing",
|
271 |
+
}
|
272 |
+
else:
|
273 |
+
respo = {
|
274 |
+
"message": answer,
|
275 |
+
"action": "nothing",
|
276 |
+
"function": "nothing",
|
277 |
+
}
|
278 |
+
else:
|
279 |
+
respo = {
|
280 |
+
"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.",
|
281 |
+
"action": "nothing",
|
282 |
+
"function": "nothing",
|
283 |
+
}
|
284 |
+
return json.dumps(respo)
|
285 |
+
|
286 |
+
# intent = palm.chat(
|
287 |
+
# messages=f"""
|
288 |
+
|
289 |
+
# Identify the user's intent from text_data below:\n
|
290 |
+
# Arguments: text_data = {data}\n\n
|
291 |
+
# Return the intent as one-word string representing the user's intent, as mentioned below:
|
292 |
+
# * if intent = viewing user's routine return = "my_routines"
|
293 |
+
# * if intent = viewing user's notes return = "view_notes"
|
294 |
+
# * if intent = viewing user's posts return = "view_posts"
|
295 |
+
# * if intent = viewing user's shared notes return = "view_shared_notes"
|
296 |
+
# * if intent = viewing user's todo lists return = "view_todo_lists"
|
297 |
+
# * if intent = viewing user's shared todo lists return = "view_shared_todo_lists"
|
298 |
+
# * if intent = viewing user's shared routines return = "view_shared_routines"
|
299 |
+
# * if intent = creating a goal return = "create_goal"
|
300 |
+
# * if intent = creating a reminder return = "create_reminder"
|
301 |
+
# * if intent = creating a routine return = "create_routine"
|
302 |
+
# * if intent = creating a group return = "create_group"
|
303 |
+
# * if intent = creating a community return = "create_community"
|
304 |
+
# * if intent = creating a note return = "create_note"
|
305 |
+
# * if intent = creating a post return = "create_post"
|
306 |
+
# * if intent = creating a todo list return = "create_todo_list"
|
307 |
+
# * if intent = sharing a reminder return = "share_reminder"
|
308 |
+
# * if intent = sharing a routine return = "share_routine"
|
309 |
+
# * if intent = sharing a group return = "share_group"
|
310 |
+
# * if intent = sharing a todo list return = "share_todo_list"
|
311 |
+
# * if intent = sharing a note return = "share_note"
|
312 |
+
# * if intent = notify then return = "send_notify"
|
313 |
+
# * if intent = show actions then return = "show_actions"
|
314 |
+
|
315 |
+
# """,
|
316 |
+
# )
|
317 |
+
|
318 |
+
# respo = {"message": intent.last, "action": "nothing", "function": "nothing"}
|
319 |
+
# if intent.last is not None:
|
320 |
+
# if "purchase_coins" in intent.last:
|
321 |
+
# respo = {
|
322 |
+
# "message": "Click the button below to view Premium Services and Coin Recharge options: ",
|
323 |
+
# "action": "payment",
|
324 |
+
# "function": "nothing",
|
325 |
+
# }
|
326 |
+
# elif "view_friends" in intent.last:
|
327 |
+
# respo = {
|
328 |
+
# "message": "Here's the list of your friends: ",
|
329 |
+
# "action": "show_friends",
|
330 |
+
# "function": "nothing",
|
331 |
+
# }
|
332 |
+
# elif "view_groups" in intent.last:
|
333 |
+
# respo = {
|
334 |
+
# "message": "You are member of following groups: ",
|
335 |
+
# "action": "show_mygroups",
|
336 |
+
# "function": "nothing",
|
337 |
+
# }
|
338 |
+
# elif "view_communities" in intent.last:
|
339 |
+
# respo = {
|
340 |
+
# "message": "You are part of following communities🫶: ",
|
341 |
+
# "action": "show_mycommunities",
|
342 |
+
# "function": "nothing",
|
343 |
+
# }
|
344 |
+
# elif "shared_reminders" in intent.last:
|
345 |
+
# respo = {
|
346 |
+
# "message": "Here's the list of your shared reminders: ",
|
347 |
+
# "action": "shared_reminders",
|
348 |
+
# "function": "nothing",
|
349 |
+
# }
|
350 |
+
# elif "my_routines" in intent.last:
|
351 |
+
# respo = {
|
352 |
+
# "message": "Here's the list of your routines: ",
|
353 |
+
# "action": "myroutines",
|
354 |
+
# "function": "nothing",
|
355 |
+
# }
|
356 |
+
# # elif "cosmo_name" in intent.last:
|
357 |
+
# # respo = {
|
358 |
+
# # "message": "My name is Cosmo. I am your friendly personal assistant.",
|
359 |
+
# # "action": "nothing",
|
360 |
+
# # "function": "nothing",
|
361 |
+
# # }
|
362 |
+
# elif "send_notify" in intent.last:
|
363 |
+
# respo = {
|
364 |
+
# "message": "Select your friends to notify",
|
365 |
+
# "action": "send_notify",
|
366 |
+
# "function": "nothing",
|
367 |
+
# }
|
368 |
+
# elif "show_actions" in intent.last:
|
369 |
+
# respo = {
|
370 |
+
# "message": "Here is list of actions you can use:",
|
371 |
+
# "action": "show_actions",
|
372 |
+
# "function": "nothing",
|
373 |
+
# }
|
374 |
+
# else:
|
375 |
+
# respo = {
|
376 |
+
# "message": response.last,
|
377 |
+
# "action": "nothing",
|
378 |
+
# "function": "nothing",
|
379 |
+
# }
|
380 |
+
# else:
|
381 |
+
# respo = {
|
382 |
+
# "message": response.last,
|
383 |
+
# "action": "nothing",
|
384 |
+
# "function": "nothing",
|
385 |
+
# }
|
386 |
+
# return json.dumps(respo)
|
387 |
+
|
388 |
+
|
389 |
+
gradio_interface = gr.Interface(fn=responsenew, inputs="text", outputs="text")
|
390 |
+
gradio_interface.launch()
|