CoolFace
Apppublic

Emerging-Tech/GoalsAI

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