yuntian-deng ysharma HF staff commited on
Commit
c8c8070
0 Parent(s):

Duplicate from ysharma/ChatGPT4

Browse files

Co-authored-by: yuvraj sharma <ysharma@users.noreply.huggingface.co>

Files changed (3) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +141 -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,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Chat-with-GPT4
3
+ emoji: 🚀
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.21.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: ysharma/ChatGPT4
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import json
4
+ import requests
5
+
6
+ #Streaming endpoint
7
+ API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
8
+
9
+ #Testing with my Open AI Key
10
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
+
12
+ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
13
+
14
+ payload = {
15
+ "model": "gpt-4",
16
+ "messages": [{"role": "user", "content": f"{inputs}"}],
17
+ "temperature" : 1.0,
18
+ "top_p":1.0,
19
+ "n" : 1,
20
+ "stream": True,
21
+ "presence_penalty":0,
22
+ "frequency_penalty":0,
23
+ }
24
+
25
+ headers = {
26
+ "Content-Type": "application/json",
27
+ "Authorization": f"Bearer {OPENAI_API_KEY}"
28
+ }
29
+
30
+ print(f"chat_counter - {chat_counter}")
31
+ if chat_counter != 0 :
32
+ messages=[]
33
+ for data in chatbot:
34
+ temp1 = {}
35
+ temp1["role"] = "user"
36
+ temp1["content"] = data[0]
37
+ temp2 = {}
38
+ temp2["role"] = "assistant"
39
+ temp2["content"] = data[1]
40
+ messages.append(temp1)
41
+ messages.append(temp2)
42
+ temp3 = {}
43
+ temp3["role"] = "user"
44
+ temp3["content"] = inputs
45
+ messages.append(temp3)
46
+ #messages
47
+ payload = {
48
+ "model": "gpt-4",
49
+ "messages": messages, #[{"role": "user", "content": f"{inputs}"}],
50
+ "temperature" : temperature, #1.0,
51
+ "top_p": top_p, #1.0,
52
+ "n" : 1,
53
+ "stream": True,
54
+ "presence_penalty":0,
55
+ "frequency_penalty":0,
56
+ }
57
+
58
+ chat_counter+=1
59
+
60
+ history.append(inputs)
61
+ print(f"payload is - {payload}")
62
+ # make a POST request to the API endpoint using the requests.post method, passing in stream=True
63
+ response = requests.post(API_URL, headers=headers, json=payload, stream=True)
64
+ print(f"response code - {response}")
65
+ token_counter = 0
66
+ partial_words = ""
67
+
68
+ counter=0
69
+ for chunk in response.iter_lines():
70
+ #Skipping first chunk
71
+ if counter == 0:
72
+ counter+=1
73
+ continue
74
+ #counter+=1
75
+ # check whether each line is non-empty
76
+ if chunk.decode() :
77
+ chunk = chunk.decode()
78
+ # decode each line as response data is in bytes
79
+ if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
80
+ #if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
81
+ # break
82
+ partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
83
+ if token_counter == 0:
84
+ history.append(" " + partial_words)
85
+ else:
86
+ history[-1] = partial_words
87
+ chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
88
+ token_counter+=1
89
+ yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
90
+
91
+
92
+ def reset_textbox():
93
+ return gr.update(value='')
94
+
95
+ title = """<h1 align="center">🔥GPT4 with ChatCompletions API +🚀Gradio-Streaming</h1>"""
96
+ description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
97
+ ```
98
+ User: <utterance>
99
+ Assistant: <utterance>
100
+ User: <utterance>
101
+ Assistant: <utterance>
102
+ ...
103
+ ```
104
+ In this app, you can explore the outputs of a gpt-4 LLM.
105
+ """
106
+
107
+ theme = gr.themes.Default(primary_hue="green")
108
+
109
+ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
110
+ #chatbot {height: 520px; overflow: auto;}""",
111
+ theme=theme) as demo:
112
+ gr.HTML(title)
113
+ gr.HTML("""<h3 align="center">🔥This Huggingface Gradio Demo provides you full access to GPT4 API (4096 token limit). 🎉🥳🎉You don't need any OPENAI API key🙌</h1>""")
114
+ gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
115
+ with gr.Column(elem_id = "col_container"):
116
+ #GPT4 API Key is provided by Huggingface
117
+ #openai_api_key = gr.Textbox(type='password', label="Enter only your GPT4 OpenAI API key here")
118
+ chatbot = gr.Chatbot(elem_id='chatbot') #c
119
+ inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
120
+ state = gr.State([]) #s
121
+ with gr.Row():
122
+ with gr.Column(scale=7):
123
+ b1 = gr.Button().style(full_width=True)
124
+ with gr.Column(scale=3):
125
+ server_status_code = gr.Textbox(label="Status code from OpenAI server", )
126
+
127
+ #inputs, top_p, temperature, top_k, repetition_penalty
128
+ with gr.Accordion("Parameters", open=False):
129
+ top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
130
+ temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
131
+ #top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
132
+ #repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
133
+ chat_counter = gr.Number(value=0, visible=False, precision=0)
134
+
135
+ inputs.submit( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
136
+ b1.click( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
137
+ b1.click(reset_textbox, [], [inputs])
138
+ inputs.submit(reset_textbox, [], [inputs])
139
+
140
+ #gr.Markdown(description)
141
+ demo.queue(max_size=20, concurrency_count=10).launch(debug=True)