king007 commited on
Commit
17f7e3c
·
0 Parent(s):

Duplicate from king007/chatgpt-prompt

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +177 -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: SlothGPT
3
+ emoji: 🐨
4
+ colorFrom: red
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.19.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: king007/chatgpt-prompt
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import openai
4
+ import requests
5
+ import json
6
+
7
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
8
+
9
+ prompt_templates = {"Sloth Alchemist": os.environ.get("PROMPT_KEY")}
10
+ question_templates = {"Ask me": "what's your name?"}
11
+
12
+ def get_empty_state():
13
+ return {"total_tokens": 0, "messages": []}
14
+
15
+ def download_prompt_templates():
16
+ url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
17
+ response = requests.get(url)
18
+
19
+ for line in response.text.splitlines()[1:]:
20
+ act, prompt = line.split('","')
21
+ prompt_templates[act.replace('"', '')] = prompt.replace('"', '')
22
+
23
+ choices = list(prompt_templates.keys())
24
+ return gr.update(value=choices[0], choices=choices)
25
+
26
+ def download_question_templates():
27
+ url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
28
+ response = requests.get(url)
29
+
30
+ for line in response.text.splitlines()[1:]:
31
+ act, prompt = line.split('","')
32
+ question_templates[act.replace('"', '')] = prompt.replace('"', '')
33
+
34
+ choices = list(question_templates.keys())
35
+ return gr.update(value=choices[0], choices=choices)
36
+
37
+ def on_token_change(user_token):
38
+ openai.api_key = user_token or os.environ.get("OPENAI_API_KEY")
39
+
40
+ def on_prompt_template_change(prompt_template):
41
+ if not isinstance(prompt_template, str): return
42
+ return prompt_templates[prompt_template]
43
+
44
+ def submit_message(user_token, prompt, prompt_template, temperature, max_tokens, state):
45
+
46
+ history = state['messages']
47
+
48
+ if not prompt:
49
+ return gr.update(value='', visible=state['total_tokens'] < 1_000), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], f"Total tokens used: {state['total_tokens']} / 3000", state
50
+
51
+ prompt_template = prompt_templates[prompt_template]
52
+
53
+ system_prompt = []
54
+ if prompt_template:
55
+ system_prompt = [{ "role": "system", "content": prompt_template }]
56
+
57
+ prompt_msg = { "role": "user", "content": prompt }
58
+
59
+ try:
60
+ completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history + [prompt_msg], temperature=temperature, max_tokens=max_tokens)
61
+
62
+ history.append(prompt_msg)
63
+ history.append(completion.choices[0].message.to_dict())
64
+
65
+ state['total_tokens'] += completion['usage']['total_tokens']
66
+
67
+ except Exception as e:
68
+ history.append(prompt_msg)
69
+ history.append({
70
+ "role": "system",
71
+ "content": f"Error: {e}"
72
+ })
73
+
74
+ total_tokens_used_msg = f"Total tokens used: {state['total_tokens']} / 3000" if not user_token else ""
75
+ chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
76
+ input_visibility = user_token or state['total_tokens'] < 3000
77
+
78
+ return gr.update(value='', visible=input_visibility), chat_messages, total_tokens_used_msg, state
79
+
80
+ def clear_conversation():
81
+ return gr.update(value=None, visible=True), None, "", get_empty_state()
82
+
83
+ def copy():
84
+ question_template = question_templates[question_template]
85
+ selected_question = question_templates
86
+ copy(selected_question)
87
+
88
+ css = """
89
+ #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
90
+ #chatbox {min-height: 400px;}
91
+ #header {text-align: center;}
92
+ #question_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px;}
93
+ #prompt_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px;}
94
+ #total_tokens_str {text-align: right; font-size: 0.8em; color: #666; height: 1em;}
95
+ #label {font-size: 0.8em; padding: 0.5em; margin: 0;}
96
+ .message { font-size: 1.2em; }
97
+ """
98
+
99
+ with gr.Blocks(css=css) as demo:
100
+
101
+ state = gr.State(get_empty_state())
102
+
103
+
104
+ with gr.Column(elem_id="col-container"):
105
+ gr.Markdown("""## chatgpt prompt
106
+ an Ai MBTI Expert and Teacher using the official API (gpt-3.5-turbo model)<br>
107
+ Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).<br>
108
+ Current limit is 3000 tokens per conversation.""",
109
+ elem_id="header")
110
+
111
+ with gr.Row():
112
+ with gr.Column():
113
+ chatbot = gr.Chatbot(elem_id="chatbox")
114
+ input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True).style(container=False)
115
+ btn_submit = gr.Button("Submit")
116
+ total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
117
+ btn_clear_conversation = gr.Button("🔃 Start New Conversation")
118
+ question_template = gr.Dropdown(label="Select a question to ask and copy/paste it:", choices=list(question_templates.keys()))
119
+ question_template_preview = gr.Markdown(elem_id="question_template_preview")
120
+ btn_copy = gr.Button("Copy")
121
+ prompt_template = gr.Dropdown(label="Set a custom insruction for the chatbot:", choices=list(prompt_templates.keys()))
122
+ prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
123
+ gr.Markdown("Enter your own OpenAI API Key to remove the 3000 token limit. You can get it [here](https://platform.openai.com/account/api-keys).", elem_id="label")
124
+ user_token = gr.Textbox(placeholder="OpenAI API Key", type="password", show_label=False)
125
+ with gr.Accordion("Advanced parameters", open=False):
126
+ temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, interactive=True, label="Temperature (higher = more creative/chaotic)")
127
+ max_tokens = gr.Slider(minimum=100, maximum=4096, value=1000, step=1, interactive=True, label="Max tokens per response")
128
+
129
+ gr.HTML('''<br><br><br><center><p><img src="https://visitor-badge.glitch.me/badge?page_id=sloth-alchemist.chatgpt_api_demo_hf" alt="visitors"></p></center>''')
130
+
131
+ btn_submit.click(submit_message, [user_token, input_message, prompt_template, temperature, max_tokens, state], [input_message, chatbot, total_tokens_str, state])
132
+ input_message.submit(submit_message, [user_token, input_message, prompt_template, temperature, max_tokens, state], [input_message, chatbot, total_tokens_str, state])
133
+ btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str, state])
134
+ #btn_copy.click(copy, [], [question_templates])
135
+ prompt_template.change(on_prompt_template_change, inputs=[prompt_template], outputs=[prompt_template_preview])
136
+ user_token.change(on_token_change, inputs=[user_token], outputs=[])
137
+
138
+
139
+ demo.load(download_question_templates, inputs=None, outputs=[question_template])
140
+ demo.load(download_prompt_templates, inputs=None, outputs=[prompt_template])
141
+
142
+
143
+ # demo.launch(debug=True, height='800px')
144
+
145
+ def download_prompt_templates2():
146
+ url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
147
+ response = requests.get(url)
148
+
149
+ for line in response.text.splitlines()[1:]:
150
+ act, prompt = line.split('","')
151
+ prompt_templates[act.replace('"', '')] = prompt.replace('"', '')
152
+
153
+ choices = list(prompt_templates.keys())
154
+ return choices
155
+
156
+
157
+ def download_question_templates2():
158
+ url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
159
+ response = requests.get(url)
160
+
161
+ for line in response.text.splitlines()[1:]:
162
+ act, prompt = line.split('","')
163
+ question_templates[act.replace('"', '')] = prompt.replace('"', '')
164
+
165
+ choices = list(question_templates.keys())
166
+ return choices
167
+
168
+ def getQuestionAndPrompt():
169
+ choice1=download_question_templates2()
170
+ choice2=download_prompt_templates2()
171
+ return choice1,choice2
172
+
173
+ question_textbox = gr.Textbox(label="", lines=10)
174
+ prompt_textbox = gr.Textbox(label="", lines=10)
175
+ gr.Interface(fn=getQuestionAndPrompt,
176
+ inputs=[],
177
+ outputs=[question_textbox, prompt_textbox]).launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai==0.27.0