ekumsau awacke1 commited on
Commit
8afa0a9
0 Parent(s):

Duplicate from AI-ZTH-03-23/1.ChatGPT-HuggingFace-Spaces-NLP-Transformers-Pipeline

Browse files
Files changed (3) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +132 -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: ChatGPTwithAPI
3
+ emoji: 🚀
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.20.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: AI-ZTH-03-23/1.ChatGPT-HuggingFace-Spaces-NLP-Transformers-Pipeline
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("ChatGPT") # Key 03-23
11
+
12
+ def predict(inputs, top_p, temperature, openai_api_key, chat_counter, chatbot=[], history=[]): #repetition_penalty, top_k
13
+
14
+ payload = {
15
+ "model": "gpt-3.5-turbo",
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-3.5-turbo",
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
+ #response = requests.post(API_URL, headers=headers, json=payload, stream=True)
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 # resembles {chatbot: chat, state: history}
90
+
91
+
92
+ def reset_textbox():
93
+ return gr.update(value='')
94
+
95
+ title = """<h1 align="center">🔥ChatGPT API 🚀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-3.5-turbo LLM.
105
+ """
106
+
107
+ with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
108
+ #chatbot {height: 520px; overflow: auto;}""") as demo:
109
+ gr.HTML(title)
110
+ gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPTwithAPI?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
111
+ with gr.Column(elem_id = "col_container"):
112
+ openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
113
+ chatbot = gr.Chatbot(elem_id='chatbot') #c
114
+ inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
115
+ state = gr.State([]) #s
116
+ b1 = gr.Button()
117
+
118
+ #inputs, top_p, temperature, top_k, repetition_penalty
119
+ with gr.Accordion("Parameters", open=False):
120
+ top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
121
+ temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
122
+ #top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
123
+ #repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
124
+ chat_counter = gr.Number(value=0, visible=False, precision=0)
125
+
126
+ inputs.submit( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
127
+ b1.click( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
128
+ b1.click(reset_textbox, [], [inputs])
129
+ inputs.submit(reset_textbox, [], [inputs])
130
+
131
+ #gr.Markdown(description)
132
+ demo.queue().launch(debug=True)