awacke1 commited on
Commit
4451326
·
1 Parent(s): fa3ca47

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
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
+ # OPENAI_API_KEY = os.getenv("ChatGPT") # Key 03-23
9
+
10
+ OPENAI_API_KEY= os.environ["HF_TOKEN"] # Add a token to this space . Then copy it to the repository secret in this spaces settings panel. os.environ reads from there.
11
+ # Keys for Open AI ChatGPT API usage are created from here: https://platform.openai.com/account/api-keys
12
+
13
+ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]): #repetition_penalty, top_k
14
+
15
+ payload = {
16
+ "model": "gpt-3.5-turbo",
17
+ "messages": [{"role": "user", "content": f"{inputs}"}],
18
+ "temperature" : 1.0,
19
+ "top_p":1.0,
20
+ "n" : 1,
21
+ "stream": True,
22
+ "presence_penalty":0,
23
+ "frequency_penalty":0,
24
+ }
25
+
26
+ headers = {
27
+ "Content-Type": "application/json",
28
+ "Authorization": f"Bearer {OPENAI_API_KEY}"
29
+ }
30
+
31
+ print(f"chat_counter - {chat_counter}")
32
+ if chat_counter != 0 :
33
+ messages=[]
34
+ for data in chatbot:
35
+ temp1 = {}
36
+ temp1["role"] = "user"
37
+ temp1["content"] = data[0]
38
+ temp2 = {}
39
+ temp2["role"] = "assistant"
40
+ temp2["content"] = data[1]
41
+ messages.append(temp1)
42
+ messages.append(temp2)
43
+ temp3 = {}
44
+ temp3["role"] = "user"
45
+ temp3["content"] = inputs
46
+ messages.append(temp3)
47
+ #messages
48
+ payload = {
49
+ "model": "gpt-3.5-turbo",
50
+ "messages": messages, #[{"role": "user", "content": f"{inputs}"}],
51
+ "temperature" : temperature, #1.0,
52
+ "top_p": top_p, #1.0,
53
+ "n" : 1,
54
+ "stream": True,
55
+ "presence_penalty":0,
56
+ "frequency_penalty":0,
57
+ }
58
+
59
+ chat_counter+=1
60
+
61
+ history.append(inputs)
62
+ print(f"payload is - {payload}")
63
+ # make a POST request to the API endpoint using the requests.post method, passing in stream=True
64
+ response = requests.post(API_URL, headers=headers, json=payload, stream=True)
65
+ #response = requests.post(API_URL, headers=headers, json=payload, stream=True)
66
+ token_counter = 0
67
+ partial_words = ""
68
+
69
+ counter=0
70
+ for chunk in response.iter_lines():
71
+ #Skipping first chunk
72
+ if counter == 0:
73
+ counter+=1
74
+ continue
75
+ #counter+=1
76
+ # check whether each line is non-empty
77
+ if chunk.decode() :
78
+ chunk = chunk.decode()
79
+ # decode each line as response data is in bytes
80
+ if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
81
+ #if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
82
+ # break
83
+ partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
84
+ if token_counter == 0:
85
+ history.append(" " + partial_words)
86
+ else:
87
+ history[-1] = partial_words
88
+ chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
89
+ token_counter+=1
90
+ yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
91
+
92
+
93
+ def reset_textbox():
94
+ return gr.update(value='')
95
+
96
+ title = """<h1 align="center">🔥ChatGPT API 🚀Streaming🚀</h1>"""
97
+ description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
98
+ ```
99
+ User: <utterance>
100
+ Assistant: <utterance>
101
+ User: <utterance>
102
+ Assistant: <utterance>
103
+ ...
104
+ ```
105
+ In this app, you can explore the outputs of a gpt-3.5-turbo LLM.
106
+ """
107
+
108
+ with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
109
+ #chatbot {height: 520px; overflow: auto;}""") as demo:
110
+ gr.HTML(title)
111
+ 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>''')
112
+ with gr.Column(elem_id = "col_container"):
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,chat_counter, chatbot, state], [chatbot, state, chat_counter],)
127
+ b1.click( predict, [inputs, top_p, temperature, 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)