Spaces:
Runtime error
Runtime error
Duplicate from ysharma/ChatGPTwithAPI
Browse filesCo-authored-by: yuvraj sharma <ysharma@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +130 -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: ysharma/ChatGPTwithAPI
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, 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 |
+
if counter == 0:
|
71 |
+
counter+=1
|
72 |
+
continue
|
73 |
+
counter+=1
|
74 |
+
# check whether each line is non-empty
|
75 |
+
if chunk :
|
76 |
+
# decode each line as response data is in bytes
|
77 |
+
if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
78 |
+
break
|
79 |
+
#print(json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"])
|
80 |
+
partial_words = partial_words + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
|
81 |
+
if token_counter == 0:
|
82 |
+
history.append(" " + partial_words)
|
83 |
+
else:
|
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 |
+
def reset_textbox():
|
91 |
+
return gr.update(value='')
|
92 |
+
|
93 |
+
title = """<h1 align="center">🔥ChatGPT API 🚀Streaming🚀</h1>"""
|
94 |
+
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
|
95 |
+
```
|
96 |
+
User: <utterance>
|
97 |
+
Assistant: <utterance>
|
98 |
+
User: <utterance>
|
99 |
+
Assistant: <utterance>
|
100 |
+
...
|
101 |
+
```
|
102 |
+
In this app, you can explore the outputs of a gpt-3.5-turbo LLM.
|
103 |
+
"""
|
104 |
+
|
105 |
+
with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
106 |
+
#chatbot {height: 520px; overflow: auto;}""") as demo:
|
107 |
+
gr.HTML(title)
|
108 |
+
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>''')
|
109 |
+
with gr.Column(elem_id = "col_container"):
|
110 |
+
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
111 |
+
chatbot = gr.Chatbot(elem_id='chatbot') #c
|
112 |
+
inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
|
113 |
+
state = gr.State([]) #s
|
114 |
+
b1 = gr.Button()
|
115 |
+
|
116 |
+
#inputs, top_p, temperature, top_k, repetition_penalty
|
117 |
+
with gr.Accordion("Parameters", open=False):
|
118 |
+
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
119 |
+
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
120 |
+
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
121 |
+
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
122 |
+
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
123 |
+
|
124 |
+
inputs.submit( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
125 |
+
b1.click( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
126 |
+
b1.click(reset_textbox, [], [inputs])
|
127 |
+
inputs.submit(reset_textbox, [], [inputs])
|
128 |
+
|
129 |
+
#gr.Markdown(description)
|
130 |
+
demo.queue().launch(debug=True)
|