ragha108 commited on
Commit
f26a920
·
0 Parent(s):

Duplicate from ragha108/aiyogi

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +156 -0
  4. requirements.txt +1 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Aiyogi
3
+ emoji: 🌍
4
+ colorFrom: indigo
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 3.23.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: ragha108/aiyogi
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ import os
4
+
5
+ # configure OpenAI
6
+
7
+ openai.api_key = os.environ["OPENAI_API_KEY"]
8
+
9
+ INSTRUCTIONS = "I want you to respond to users strictly with knowledge from Nisargadatta Maharaj, " \
10
+ "Ramana Maharishi, Swami Vivekananda, Ashtavakra, Adi Shankaracharya and Sage Vasishta among other great Advaita Vedanta Masters " \
11
+ "I want you to answer users with questions, as if you were their therapist, in a dialogue between " \
12
+ "student and teacher, guru and disciple." \
13
+ "Try not to provide or give away a full answers in the beginning. Have a conversation. Ask questions. " \
14
+ "Inspire the user to get to the answer of his/her own question " \
15
+ "Act as of one of these great Vedanta Masters when responding" \
16
+ "Limit your answers to no more than 100 words"
17
+
18
+
19
+ TEMPERATURE = 0.5
20
+ MAX_TOKENS = 500
21
+ FREQUENCY_PENALTY = 0
22
+ PRESENCE_PENALTY = 0.6
23
+ # limits how many questions we include in the prompt
24
+ MAX_CONTEXT_QUESTIONS = 10
25
+
26
+
27
+ def get_response(instructions, previous_questions_and_answers, new_question):
28
+ """Get a response from ChatCompletion
29
+
30
+ Args:
31
+ instructions: The instructions for the chat bot - this determines how it will behave
32
+ previous_questions_and_answers: Chat history
33
+ new_question: The new question to ask the bot
34
+
35
+ Returns:
36
+ The response text
37
+ """
38
+ # build the messages
39
+ messages = [
40
+ { "role": "system", "content": instructions },
41
+ ]
42
+ # add the previous questions and answers
43
+ for question, answer in previous_questions_and_answers[-MAX_CONTEXT_QUESTIONS:]:
44
+ messages.append({ "role": "user", "content": question })
45
+ messages.append({ "role": "assistant", "content": answer })
46
+ # add the new question
47
+ messages.append({ "role": "user", "content": new_question })
48
+
49
+ completion = openai.ChatCompletion.create(
50
+ model="gpt-3.5-turbo",
51
+ messages=messages,
52
+ temperature=TEMPERATURE,
53
+ max_tokens=MAX_TOKENS,
54
+ top_p=1,
55
+ frequency_penalty=FREQUENCY_PENALTY,
56
+ presence_penalty=PRESENCE_PENALTY,
57
+ )
58
+ return completion.choices[0].message.content
59
+
60
+
61
+
62
+ def get_moderation(question):
63
+ """
64
+ Check the question is safe to ask the model
65
+
66
+ Parameters:
67
+ question (str): The question to check
68
+
69
+ Returns a list of errors if the question is not safe, otherwise returns None
70
+ """
71
+
72
+ errors = {
73
+ "hate": "Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste.",
74
+ "hate/threatening": "Hateful content that also includes violence or serious harm towards the targeted group.",
75
+ "self-harm": "Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.",
76
+ "sexual": "Content meant to arouse sexual excitement, such as the description of sexual activity, or that promotes sexual services (excluding sex education and wellness).",
77
+ "sexual/minors": "Sexual content that includes an individual who is under 18 years old.",
78
+ "violence": "Content that promotes or glorifies violence or celebrates the suffering or humiliation of others.",
79
+ "violence/graphic": "Violent content that depicts death, violence, or serious physical injury in extreme graphic detail.",
80
+ }
81
+ response = openai.Moderation.create(input=question)
82
+ if response.results[0].flagged:
83
+ # get the categories that are flagged and generate a message
84
+ result = [
85
+ error
86
+ for category, error in errors.items()
87
+ if response.results[0].categories[category]
88
+ ]
89
+ return result
90
+ return None
91
+
92
+
93
+ # def main():
94
+ # os.system("cls" if os.name == "nt" else "clear")
95
+ # # keep track of previous questions and answers
96
+ # previous_questions_and_answers = []
97
+ # while True:
98
+ # # ask the user for their question
99
+ # new_question = input(
100
+ # Fore.GREEN + Style.BRIGHT + "wwww?: " + Style.RESET_ALL
101
+ # )
102
+ # # check the question is safe
103
+ # errors = get_moderation(new_question)
104
+ # if errors:
105
+ # print(
106
+ # Fore.RED
107
+ # + Style.BRIGHT
108
+ # + "Sorry, you're question didn't pass the moderation check:"
109
+ # )
110
+ # for error in errors:
111
+ # print(error)
112
+ # print(Style.RESET_ALL)
113
+ # continue
114
+ # response = get_response(INSTRUCTIONS, previous_questions_and_answers, new_question)
115
+
116
+ # # add the new question and answer to the list of previous questions and answers
117
+ # previous_questions_and_answers.append((new_question, response))
118
+
119
+
120
+ def delete_chat_history(previous_questions_and_answers):
121
+ previous_questions_and_answers.clear()
122
+ return previous_questions_and_answers,""
123
+
124
+ def chatgpt_clone(input, previous_questions_and_answers):
125
+ previous_questions_and_answers = previous_questions_and_answers or []
126
+ s = list(sum(previous_questions_and_answers, ()))
127
+ s.append(input)
128
+ inp = ' '.join(s)
129
+ moderation_errors = get_moderation(input)
130
+ if moderation_errors is not None:
131
+ return "\n".join(moderation_errors)
132
+ output = get_response(INSTRUCTIONS, previous_questions_and_answers, inp)
133
+ previous_questions_and_answers.append((input, output))
134
+ return previous_questions_and_answers, previous_questions_and_answers
135
+
136
+
137
+ block = gr.Blocks(theme=gr.themes.Monochrome(secondary_hue="neutral").set(button_primary_background_fill="*primary_400",
138
+ button_primary_background_fill_hover="*primary_300"))
139
+
140
+ with block:
141
+ # gr.Markdown("""<h1><center>_/\_ AI YOGI _/\_ </center></h1>""")
142
+ chatbot = gr.Chatbot(label="Ai Yogi:")
143
+ message = gr.Textbox(label="Namaste! How may I serve you?",placeholder="What concerns you now?")
144
+ # message.change(fn=lambda value: gr.update(value=""))
145
+ state = gr.State()
146
+ submit = gr.Button("SEND")
147
+ submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
148
+ clear = gr.Button("CLEAR")
149
+ clear.click(delete_chat_history, inputs=[state], outputs=[chatbot, state])
150
+ clear.click(lambda x: gr.update(value='',placeholder="What concerns you now?",label="Namaste! How may I serve you?"), [],[message])
151
+ submit.click(lambda x: gr.update(value='',placeholder="",label="You may continue with the conversation below"), [],[message])
152
+ submit.click(lambda x: gr.update(label='Ai Yogi:'), [],[chatbot])
153
+ clear.click(lambda x: gr.update(label='Ai Yogi:'), [],[chatbot])
154
+
155
+ message.submit(lambda x: gr.update(value='',placeholder="",label=""), [],[message])
156
+ block.launch(show_api=False)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai