RamAnanth1 yizhangliu commited on
Commit
3796a5e
0 Parent(s):

Duplicate from yizhangliu/chatGPT

Browse files

Co-authored-by: yizhangliu <yizhangliu@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +145 -0
  4. requirements.txt +2 -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: ChatGPT
3
+ emoji: 📊
4
+ colorFrom: blue
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.12.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: yizhangliu/chatGPT
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pyChatGPT import ChatGPT
2
+ import gradio as gr
3
+ import os, json
4
+ from loguru import logger
5
+ import random
6
+
7
+ session_token = os.environ.get('SessionToken')
8
+ # logger.info(f"session_token_: {session_token}")
9
+
10
+ def get_response_from_chatbot(text):
11
+ try:
12
+ api = ChatGPT(session_token)
13
+ resp = api.send_message(text)
14
+ api.refresh_auth()
15
+ api.reset_conversation()
16
+ response = resp['message']
17
+ # logger.info(f"response_: {response}")
18
+ except:
19
+ response = "Sorry, I'm tired."
20
+ return response
21
+
22
+ def chat(message, chat_history):
23
+ out_chat = []
24
+ if chat_history != '':
25
+ out_chat = json.loads(chat_history)
26
+ response = get_response_from_chatbot(message)
27
+ out_chat.append((message, response))
28
+ chat_history = json.dumps(out_chat)
29
+ logger.info(f"out_chat_: {len(out_chat)}")
30
+ return out_chat, chat_history
31
+
32
+ start_work = """async() => {
33
+ function isMobile() {
34
+ try {
35
+ document.createEvent("TouchEvent"); return true;
36
+ } catch(e) {
37
+ return false;
38
+ }
39
+ }
40
+ function getClientHeight()
41
+ {
42
+ var clientHeight=0;
43
+ if(document.body.clientHeight&&document.documentElement.clientHeight) {
44
+ var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
45
+ } else {
46
+ var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
47
+ }
48
+ return clientHeight;
49
+ }
50
+
51
+ function setNativeValue(element, value) {
52
+ const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;
53
+ const prototype = Object.getPrototypeOf(element);
54
+ const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
55
+
56
+ if (valueSetter && valueSetter !== prototypeValueSetter) {
57
+ prototypeValueSetter.call(element, value);
58
+ } else {
59
+ valueSetter.call(element, value);
60
+ }
61
+ }
62
+ var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
63
+ if (!gradioEl) {
64
+ gradioEl = document.querySelector('body > gradio-app');
65
+ }
66
+
67
+ if (typeof window['gradioEl'] === 'undefined') {
68
+ window['gradioEl'] = gradioEl;
69
+
70
+ const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
71
+ const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
72
+
73
+ page1.style.display = "none";
74
+ page2.style.display = "block";
75
+
76
+ window['div_count'] = 0;
77
+ window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
78
+ window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
79
+ chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0];
80
+ prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0];
81
+ window['chat_bot1'].children[1].textContent = '';
82
+
83
+ clientHeight = getClientHeight();
84
+ new_height = (clientHeight-300) + 'px';
85
+ chat_row.style.height = new_height;
86
+ window['chat_bot'].style.height = new_height;
87
+ window['chat_bot'].children[2].style.height = new_height;
88
+ window['chat_bot1'].style.height = new_height;
89
+ window['chat_bot1'].children[2].style.height = new_height;
90
+ prompt_row.children[0].style.flex = 'auto';
91
+ prompt_row.children[0].style.width = '100%';
92
+
93
+ window['checkChange'] = function checkChange() {
94
+ try {
95
+ if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {
96
+ new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];
97
+ for (var i = 0; i < new_len; i++) {
98
+ new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);
99
+ window['chat_bot1'].children[2].children[0].appendChild(new_div);
100
+ }
101
+ window['div_count'] = chat_bot.children[2].children[0].children.length;
102
+ }
103
+ if (window['chat_bot'].children[0].children.length > 1) {
104
+ window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
105
+ } else {
106
+ window['chat_bot1'].children[1].textContent = '';
107
+ }
108
+
109
+ } catch(e) {
110
+ }
111
+ }
112
+ window['checkChange_interval'] = window.setInterval("window.checkChange()", 500);
113
+ }
114
+
115
+ return false;
116
+ }"""
117
+
118
+
119
+ with gr.Blocks(title='Talk to chatGPT') as demo:
120
+ gr.HTML("<p>You can duplicating this space and use your own session token: <a style='display:inline-block' href='https://huggingface.co/spaces/yizhangliu/chatGPT?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>")
121
+ gr.HTML("<p> Instruction on how to get session token can be seen in video <a style='display:inline-block' href='https://www.youtube.com/watch?v=TdNSj_qgdFk'><font style='color:blue;weight:bold;'>here</font></a>. Add your session token by going to settings and add under secrets. </p>")
122
+ with gr.Group(elem_id="page_1", visible=True) as page_1:
123
+ with gr.Box():
124
+ with gr.Row():
125
+ start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True)
126
+ start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
127
+
128
+ with gr.Group(elem_id="page_2", visible=False) as page_2:
129
+ with gr.Row(elem_id="chat_row"):
130
+ chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
131
+ chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
132
+ with gr.Row(elem_id="prompt_row"):
133
+ prompt_input = gr.Textbox(lines=2, label="prompt",show_label=False)
134
+ chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
135
+ submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
136
+ margin=True,
137
+ rounded=(True, True, True, True),
138
+ width=100
139
+ )
140
+ submit_btn.click(fn=chat,
141
+ inputs=[prompt_input, chat_history],
142
+ outputs=[chatbot, chat_history],
143
+ )
144
+
145
+ demo.launch(debug = True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pyChatGPT
2
+ loguru