Spaces:
Runtime error
Runtime error
fixed the state issue
Browse files
app.py
CHANGED
|
@@ -6,16 +6,16 @@ import requests
|
|
| 6 |
#Streaming endpoint
|
| 7 |
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
| 8 |
|
| 9 |
-
#Open AI Key
|
| 10 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
|
| 12 |
-
def predict(inputs, top_p, temperature, openai_api_key, history=[]):
|
| 13 |
|
| 14 |
payload = {
|
| 15 |
"model": "gpt-3.5-turbo",
|
| 16 |
"messages": [{"role": "user", "content": f"{inputs}"}],
|
| 17 |
-
"temperature" :
|
| 18 |
-
"top_p":
|
| 19 |
"n" : 1,
|
| 20 |
"stream": True,
|
| 21 |
"presence_penalty":0,
|
|
@@ -27,8 +27,38 @@ def predict(inputs, top_p, temperature, openai_api_key, history=[]):
|
|
| 27 |
"Authorization": f"Bearer {openai_api_key}"
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
history.append(inputs)
|
|
|
|
| 32 |
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
| 33 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 34 |
#response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
|
@@ -54,8 +84,9 @@ def predict(inputs, top_p, temperature, openai_api_key, history=[]):
|
|
| 54 |
history[-1] = partial_words
|
| 55 |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
| 56 |
token_counter+=1
|
| 57 |
-
yield chat, history # resembles {chatbot: chat, state: history}
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
def reset_textbox():
|
| 61 |
return gr.update(value='')
|
|
@@ -69,14 +100,12 @@ User: <utterance>
|
|
| 69 |
Assistant: <utterance>
|
| 70 |
...
|
| 71 |
```
|
| 72 |
-
In this app, you can explore the outputs of a
|
| 73 |
"""
|
| 74 |
-
#<a href="https://huggingface.co/spaces/ysharma/ChatGPTwithAPI?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate Space with GPU Upgrade for fast Inference & no queue<br>
|
| 75 |
|
| 76 |
with gr.Blocks(css = """#col_container {width: 700px; margin-left: auto; margin-right: auto;}
|
| 77 |
#chatbot {height: 400px; overflow: auto;}""") as demo:
|
| 78 |
gr.HTML(title)
|
| 79 |
-
gr.HTML()
|
| 80 |
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>''')
|
| 81 |
with gr.Column(elem_id = "col_container"):
|
| 82 |
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
|
@@ -91,10 +120,10 @@ with gr.Blocks(css = """#col_container {width: 700px; margin-left: auto; margin-
|
|
| 91 |
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
| 92 |
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
| 93 |
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
| 94 |
-
|
| 95 |
|
| 96 |
-
inputs.submit( predict, [inputs, top_p, temperature, openai_api_key, state], [chatbot, state],)
|
| 97 |
-
b1.click( predict, [inputs, top_p, temperature, openai_api_key, state], [chatbot, state],)
|
| 98 |
b1.click(reset_textbox, [], [inputs])
|
| 99 |
inputs.submit(reset_textbox, [], [inputs])
|
| 100 |
|
|
|
|
| 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, 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,
|
|
|
|
| 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)
|
|
|
|
| 84 |
history[-1] = partial_words
|
| 85 |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
| 86 |
token_counter+=1
|
| 87 |
+
yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
|
| 88 |
|
| 89 |
+
|
| 90 |
|
| 91 |
def reset_textbox():
|
| 92 |
return gr.update(value='')
|
|
|
|
| 100 |
Assistant: <utterance>
|
| 101 |
...
|
| 102 |
```
|
| 103 |
+
In this app, you can explore the outputs of a gpt-3.5-turbo LLM.
|
| 104 |
"""
|
|
|
|
| 105 |
|
| 106 |
with gr.Blocks(css = """#col_container {width: 700px; margin-left: auto; margin-right: auto;}
|
| 107 |
#chatbot {height: 400px; overflow: auto;}""") as demo:
|
| 108 |
gr.HTML(title)
|
|
|
|
| 109 |
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>''')
|
| 110 |
with gr.Column(elem_id = "col_container"):
|
| 111 |
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
|
|
|
| 120 |
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
| 121 |
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
| 122 |
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
| 123 |
+
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 124 |
|
| 125 |
+
inputs.submit( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
| 126 |
+
b1.click( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
| 127 |
b1.click(reset_textbox, [], [inputs])
|
| 128 |
inputs.submit(reset_textbox, [], [inputs])
|
| 129 |
|